Repository: fossasia/open-event-server

Issue:
Sessions API: Add missing 'Last Modified'
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server

**Current behavior:**
![screen shot 2017-11-08 at 2 22 11 am](https://user-images.githubusercontent.com/12807846/32517233-cc9f5704-c42b-11e7-8a7a-18e9c65bc081.png)
Currently a field is missing in the API for displaying last modification date.

**Expected behavior:**
The API should include a field for modification/updates made to the session.



Discussion:
i would like to work on this issue @geekyd 
I would like to work on this issue. 

Resolving patch:
diff --git a/app/api/schema/sessions.py b/app/api/schema/sessions.py
--- a/app/api/schema/sessions.py
+++ b/app/api/schema/sessions.py
@@ -72,6 +72,7 @@ def validate_date(self, data, original_data):
     deleted_at = fields.DateTime(dump_only=True)
     submitted_at = fields.DateTime(allow_none=True)
     is_mail_sent = fields.Boolean()
+    last_modified_at = fields.DateTime(dump_only=True)
     microlocation = Relationship(attribute='microlocation',
                                  self_view='v1.session_microlocation',
                                  self_view_kwargs={'id': '<id>'},
diff --git a/app/models/session.py b/app/models/session.py
--- a/app/models/session.py
+++ b/app/models/session.py
@@ -1,7 +1,8 @@
 import datetime
-
+import pytz
 from app.models.helpers.versioning import clean_up_string, clean_html
 from app.models import db
+from sqlalchemy import event
 
 speakers_sessions = db.Table('speakers_sessions',
                              db.Column('speaker_id', db.Integer, db.ForeignKey('speaker.id', ondelete='CASCADE')),
@@ -45,6 +46,7 @@ class Session(db.Model):
     submitted_at = db.Column(db.DateTime(timezone=True))
     submission_modifier = db.Column(db.String)
     is_mail_sent = db.Column(db.Boolean, default=False)
+    last_modified_at = db.Column(db.DateTime(timezone=True), default=datetime.datetime.utcnow)
 
     def __init__(self,
                  title=None,
@@ -71,7 +73,8 @@ def __init__(self,
                  submission_modifier=None,
                  is_mail_sent=False,
                  deleted_at=None,
-                 submitted_at=None):
+                 submitted_at=None,
+                 last_modified_at=None):
 
         if speakers is None:
             speakers = []
@@ -101,6 +104,7 @@ def __init__(self,
         self.is_mail_sent = is_mail_sent
         self.submitted_at = submitted_at
         self.submission_modifier = submission_modifier
+        self.last_modified_at = datetime.datetime.now(pytz.utc)
 
     @staticmethod
     def get_service_name():
@@ -124,3 +128,8 @@ def __setattr__(self, name, value):
 
     def __unicode__(self):
         return self.title
+
+
+@event.listens_for(Session, 'before_update')
+def receive_after_update(mapper, connection, target):
+    target.last_modified_at = datetime.datetime.now(pytz.utc)
diff --git a/migrations/versions/4591885edbf0_.py b/migrations/versions/4591885edbf0_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/4591885edbf0_.py
@@ -0,0 +1,30 @@
+"""empty message
+
+Revision ID: 4591885edbf0
+Revises: 6ffaeab2c3c2
+Create Date: 2017-12-14 16:32:22.540831
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+# revision identifiers, used by Alembic.
+revision = '4591885edbf0'
+down_revision = '6ffaeab2c3c2'
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('sessions', sa.Column('last_modified_at', sa.DateTime(timezone=True), nullable=True))
+    op.add_column('sessions_version', sa.Column('last_modified_at', sa.DateTime(timezone=True), autoincrement=False, nullable=True))
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('sessions_version', 'last_modified_at')
+    op.drop_column('sessions', 'last_modified_at')
+    # ### end Alembic commands ###


Test patch: