Repository: fossasia/open-event-server

Issue:
User can publish past events
**Describe the bug**
Past draft events can be published

**To Reproduce**
Create a draft event with today's date. Publish it tomorrow.

**Expected behavior**
Publishing past draft events should fail



Discussion:
Issue-Label Bot is automatically applying the label `bug` to this issue, with a confidence of 0.95. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback! 

 Links: [app homepage](https://github.com/marketplace/issue-label-bot), [dashboard](https://mlbot.net/data/fossasia/open-event-server) and [code](https://github.com/hamelsmu/MLapp) for this bot.
So if the draft is created within 24 hours, then only it should be publishable?
No, if my draft says it event starts on a past date, it should not be publishable. I thought this was already handled in the date verifications
Okay. The date check is updated to check only if event date is changed. It was necessary for few checks. 
I'll add condition for this transition to check date.
It should be able to create a draft event with past date but not publish. I think this will also make it easier to restore events. So when you restore event, if it has past date, make it a draft

Resolving patch:
diff --git a/app/api/events.py b/app/api/events.py
--- a/app/api/events.py
+++ b/app/api/events.py
@@ -107,8 +107,13 @@ def validate_date(event, data):
                                   "ends-at should be after starts-at")
 
     if datetime.timestamp(data['starts_at']) <= datetime.timestamp(datetime.now()):
-        raise UnprocessableEntity({'pointer': '/data/attributes/starts-at'},
-                                  "starts-at should be after current date-time")
+        if event and event.deleted_at and not data.get('deleted_at'):
+            data['state'] = 'draft'
+        elif event and not event.deleted_at and data.get('deleted_at'):
+            pass
+        else:
+            raise UnprocessableEntity({'pointer': '/data/attributes/starts-at'},
+                                      "starts-at should be after current date-time")
 
 class EventList(ResourceList):
     def before_get(self, args, kwargs):
@@ -233,7 +238,8 @@ def before_post(self, args, kwargs, data=None):
         user = User.query.filter_by(id=kwargs['user_id']).first()
         modules = Module.query.first()
         validate_event(user, modules, data)
-        validate_date(None, data)
+        if data['state'] != 'draft':
+            validate_date(None, data)
 
     def after_create_object(self, event, data, view_kwargs):
         """
@@ -550,7 +556,11 @@ def before_update_object(self, event, data, view_kwargs):
         :param view_kwargs:
         :return:
         """
-        if data.get('starts_at') != event.starts_at or data.get('ends_at') != event.ends_at:
+        is_date_updated = (data.get('starts_at') != event.starts_at or data.get('ends_at') != event.ends_at)
+        is_draft_published = (event.state == "draft" and data.get('state') == "published")
+        is_event_restored = (event.deleted_at and not data.get('deleted_at'))
+
+        if is_date_updated or is_draft_published or is_event_restored:
             validate_date(event, data)
 
         if has_access('is_admin') and data.get('deleted_at') != event.deleted_at:


Test patch: