diff --git a/sourcing/model.py b/sourcing/model.py index 8ff3ec4..8c55023 100644 --- a/sourcing/model.py +++ b/sourcing/model.py @@ -123,21 +123,22 @@ class User(TimeStampedModel, UserMixin): return user class Item(TimeStampedModel): - __versioned__ = {} __tablename__ = 'item' + __versioned__ = {'base_classes': (TimeStampedModel,)} id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('user.id')) published = Column(DateTime) - type = Column(Enum('sourcedoc', 'xanadoc', 'xanalink', name='item_type'), nullable=False) + type = Column(Enum('sourcedoc', 'xanadoc', 'xanalink', name='item_type'), + nullable=False) filename = Column(Unicode) text = Column(UnicodeText) user = relationship('User', backref='items') __mapper_args__ = { - 'polymorphic_identity': 'item', 'polymorphic_on': type, + 'with_polymorphic': '*', } @property @@ -179,11 +180,34 @@ class Item(TimeStampedModel): def edit_url(self): return url_for('.edit_item', username=self.user.username, hashid=self.hashid) + @property + def add_title_url(self): + return url_for('.add_title', username=self.user.username, hashid=self.hashid) + def title(self, titles=None): if not titles: titles = XanaLink.get_all_titles() return self.type + ": " + titles.get(self, self.hashid) + def has_title(self): + titles = XanaLink.get_all_titles() + print(titles) + return self in titles + + def add_title(self, title, user): + title_source_doc = SourceDoc(text=title, user=user) + session.add(title_source_doc) + session.commit() + link_text = '''type=title + +facet= +sourcedoc: {} +facet= +span: {},start=0,length={}'''.format(self.external_url, title_source_doc.external_url, len(title)) + title_link = XanaLink(text=link_text, user=user) + session.add(title_link) + session.commit() + @classmethod def from_external(cls, url): home = url_for('.home', _external=True) @@ -200,16 +224,16 @@ class Item(TimeStampedModel): class XanaDoc(Item): __tablename__ = 'xanadoc' - id = Column(Integer, ForeignKey('item.id'), primary_key=True) - __mapper_args__ = {'polymorphic_identity': 'xanadoc'} + id = Column(Integer, ForeignKey(Item.id), primary_key=True) + class XanaLink(Item): __tablename__ = 'xanalink' - id = Column(Integer, ForeignKey('item.id'), primary_key=True) - __mapper_args__ = {'polymorphic_identity': 'xanalink'} + id = Column(Integer, ForeignKey(Item.id), primary_key=True) + def parse(self): return parse_link(self.text) @@ -250,7 +274,9 @@ class XanaLink(Item): class SourceDoc(Item): __tablename__ = 'sourcedoc' - id = Column(Integer, ForeignKey('item.id'), primary_key=True) + __mapper_args__ = {'polymorphic_identity': 'sourcedoc'} + + id = Column(Integer, ForeignKey(Item.id), primary_key=True) db_price_per_character = Column(Integer) db_document_price = Column(Integer) @@ -262,7 +288,6 @@ class SourceDoc(Item): def price_per_character(self): return self.db_price_per_character or self.db_document_price / len(self.text) - __mapper_args__ = {'polymorphic_identity': 'sourcedoc'} configure_mappers() diff --git a/sourcing/static/js/doc.js b/sourcing/static/js/doc.js index c0a90a2..d59d667 100644 --- a/sourcing/static/js/doc.js +++ b/sourcing/static/js/doc.js @@ -1,3 +1,5 @@ +'use strict'; + $(function() { $("div#right").hide(); /* $("button#add-new-span").click(function() { diff --git a/sourcing/static/js/sourcedoc.js b/sourcing/static/js/sourcedoc.js index cf2e711..2929929 100644 --- a/sourcing/static/js/sourcedoc.js +++ b/sourcing/static/js/sourcedoc.js @@ -1,3 +1,5 @@ +'use strict'; + function add_message() { $("div#messages").text("whatever"); } diff --git a/sourcing/templates/base.html b/sourcing/templates/base.html index 6ddba92..d910e10 100644 --- a/sourcing/templates/base.html +++ b/sourcing/templates/base.html @@ -29,6 +29,7 @@ + {% block scripts %} diff --git a/sourcing/templates/view.html b/sourcing/templates/view.html index 458c4e6..ee27e4e 100644 --- a/sourcing/templates/view.html +++ b/sourcing/templates/view.html @@ -4,6 +4,29 @@ {% block content %} + + -

{{ self.title() }}

+

{{ self.title() }} + {% if current_user.is_authenticated and doc.user == current_user and not doc.has_title() %} + + {% endif %} +

{% if version %}

Revision as of {{ version.modified.strftime('%H:%M, %d %B %Y') }}

{% endif %} diff --git a/sourcing/view.py b/sourcing/view.py index 1b16e79..b50d58e 100644 --- a/sourcing/view.py +++ b/sourcing/view.py @@ -199,6 +199,14 @@ def fulfil(username, hashid): item=item, doc=fulfil_edl_with_sources(item.text)) +@bp.route('///add_title', methods=['POST']) +def add_title(username, hashid): + item = get_item(username, hashid) + title = request.form['title'] + item.add_title(title, current_user) + flash('title added') + return redirect(item.url) + @bp.route('//') def view_item(username, hashid, raw=False): if ',' in hashid: