Repository: fossasia/open-event-server

Issue:
Enhance and correct Tracks API
- Remove `track_image_url` (Both API and DB. There is no data anyway)
- Remove `location` (Both API and DB. There is no data anyway)
- Add `font_color` to API response (only API response. No DB field. Dynamic based on color. See #3568 for old implementation.)
- Update the above details in the API Blueprint too


Discussion:
@niranjan94 One question, would it better if:
1) we allow client to post/patch the font-color and we deleted it (not update it) in the api and show a message saying 'it will not be updated since it is automated' or something like that.
or
2) raise an error message if they try to post/patch font-color.
Allow the client to post/patch font color for now. But ignore any changes to it. 

Resolving patch:
diff --git a/app/api/tracks.py b/app/api/tracks.py
--- a/app/api/tracks.py
+++ b/app/api/tracks.py
@@ -29,9 +29,8 @@ class Meta:
     id = fields.Str(dump_only=True)
     name = fields.Str(required=True)
     description = fields.Str()
-    track_image_url = fields.Url()
-    color = fields.Str()
-    location = fields.Str()
+    color = fields.Str(required=True)
+    font_color = fields.Str()
     event = Relationship(attribute='event',
                          self_view='v1.track_event',
                          self_view_kwargs={'id': '<id>'},
@@ -70,6 +69,9 @@ def before_create_object(self, data, view_kwargs):
         elif view_kwargs.get('identifier'):
             event = self.session.query(Event).filter_by(identifier=view_kwargs['identifier']).one()
             data['event_id'] = event.id
+        
+        if data['font_color']:
+            del data['font_color']
 
     view_kwargs = True
     decorators = (jwt_required,)
@@ -100,11 +102,16 @@ def before_get_object(self, view_kwargs):
                 else:
                     view_kwargs['id'] = None
 
+    def before_update_object(self, obj, data, view_kwargs):
+        if data['font_color']:
+            del data['font_color']
+
     decorators = (jwt_required,)
     schema = TrackSchema
     data_layer = {'session': db.session,
                   'model': Track,
-                  'methods': {'before_get_object': before_get_object}}
+                  'methods': {'before_get_object': before_get_object,
+                              'before_update_object': before_update_object}}
 
 
 class TrackRelationship(ResourceRelationship):
diff --git a/app/models/track.py b/app/models/track.py
--- a/app/models/track.py
+++ b/app/models/track.py
@@ -7,22 +7,17 @@ class Track(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     name = db.Column(db.String, nullable=False)
     description = db.Column(db.Text)
-    track_image_url = db.Column(db.Text)
     color = db.Column(db.String, nullable=False)
-    location = db.Column(db.String)
     sessions = db.relationship('Session', backref='track')
     event_id = db.Column(db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
 
     def __init__(self, name=None, description=None, event_id=None,
-                 session=None, track_image_url=None, color=None,
-                 location=None):
+                 session=None, color=None):
         self.name = name
         self.description = description
         self.event_id = event_id
         self.session_id = session
-        self.track_image_url = track_image_url
         self.color = color
-        self.location = location
 
     @staticmethod
     def get_service_name():
diff --git a/migrations/versions/a71a8fcf6493_.py b/migrations/versions/a71a8fcf6493_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/a71a8fcf6493_.py
@@ -0,0 +1,29 @@
+"""empty message
+
+Revision ID: a71a8fcf6493
+Revises: c79e6d36a194
+Create Date: 2017-06-16 22:57:31.042325
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = 'a71a8fcf6493'
+down_revision = 'c79e6d36a194'
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('tracks', 'track_image_url')
+    op.drop_column('tracks', 'location')
+    ### end Alembic commands ###
+
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('tracks', sa.Column('location', sa.VARCHAR(), autoincrement=False, nullable=True))
+    op.add_column('tracks', sa.Column('track_image_url', sa.TEXT(), autoincrement=False, nullable=True))
+    ### end Alembic commands ###


Test patch: