From 2f37577aaaa6150ff8f1c4b98f1fc49728fbf904 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 7 Apr 2017 15:43:46 +0100 Subject: [PATCH] add versioning --- sourcing/model.py | 27 ++++++++++++++++++++++--- sourcing/templates/edit.html | 2 +- sourcing/templates/history.html | 35 +++++++++++++++++++++++++++++++++ sourcing/templates/view.html | 19 +++++++++++------- sourcing/view.py | 18 +++++++++++++++++ 5 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 sourcing/templates/history.html diff --git a/sourcing/model.py b/sourcing/model.py index 94b5ab6..0a3cb35 100644 --- a/sourcing/model.py +++ b/sourcing/model.py @@ -5,15 +5,19 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, ForeignKey from sqlalchemy.types import String, Unicode, Integer, DateTime, Boolean, UnicodeText, Enum from sqlalchemy import func -from sqlalchemy.orm import relationship, validates, synonym +from sqlalchemy.orm import relationship, validates, synonym, configure_mappers from sqlalchemy.sql import exists from flask_login import UserMixin from werkzeug.security import generate_password_hash, check_password_hash from sqlalchemy.ext.hybrid import hybrid_property +from sqlalchemy_continuum import make_versioned +from sqlalchemy_continuum.plugins import FlaskPlugin, ActivityPlugin import re - from hashids import Hashids +activity_plugin = ActivityPlugin() +make_versioned(plugins=[FlaskPlugin(), activity_plugin]) + doc_hashids = Hashids(min_length=8) Base = declarative_base() @@ -119,7 +123,9 @@ class User(TimeStampedModel, UserMixin): return user class Item(TimeStampedModel): + __versioned__ = {} __tablename__ = 'item' + id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('user.id')) published = Column(DateTime) @@ -148,7 +154,19 @@ class Item(TimeStampedModel): @property def url(self): - return url_for('.view_item', username=self.user.username, hashid=self.hashid) + return url_for('.view_item', + username=self.user.username, + hashid=self.hashid) + + def version_url(self, version): + return url_for('.view_item', + username=self.user.username, + hashid=self.hashid, + v=version.transaction_id) + + @property + def history_url(self): + return url_for('.history', username=self.user.username, hashid=self.hashid) @property def external_url(self): @@ -245,3 +263,6 @@ class SourceDoc(Item): 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/templates/edit.html b/sourcing/templates/edit.html index 7359322..df3742a 100644 --- a/sourcing/templates/edit.html +++ b/sourcing/templates/edit.html @@ -21,7 +21,7 @@ {% endif %} {% endif %}