Repository: fossasia/open-event-server

Issue:
Functionality to Manage Events Dashboard and Specific Event Dashboard: Publish, Unpublish, Copy, Delete
- [x] Publish
- [x] Unpublish
- [x] Copy
- [x] Delete



Discussion:

Resolving patch:
diff --git a/open_event/views/admin/models_views/events.py b/open_event/views/admin/models_views/events.py
--- a/open_event/views/admin/models_views/events.py
+++ b/open_event/views/admin/models_views/events.py
@@ -145,6 +145,7 @@ def edit_view(self, event_id):
     def delete_view(self, event_id):
         if request.method == "GET":
             DataManager.delete_event(event_id)
+        flash("Your event has been deleted.", "danger")
         return redirect(url_for('.index_view'))
 
     @expose('/<int:event_id>/update/', methods=('POST',))
@@ -153,3 +154,33 @@ def save_closing_date(self, event_id):
         event.closing_datetime = request.form['closing_datetime']
         save_to_db(event, 'Closing Datetime Updated')
         return self.render('/gentelella/admin/event/details/details.html', event=event)
+
+    @expose('/<int:event_id>/publish/', methods=('GET',))
+    def publish_event(self, event_id):
+        event = DataGetter.get_event(event_id)
+        if string_empty(event.location_name):
+            flash("Your event was saved. To publish your event please review the highlighted fields below.", "warning")
+            return redirect(url_for('.edit_view', event_id=event.id) + "#step=location_name")
+        event.state = 'Published'
+        save_to_db(event, 'Event Published')
+        flash("Your event has been published.", "success")
+        return redirect(url_for('.details_view', event_id=event_id))
+
+    @expose('/<int:event_id>/unpublish/', methods=('GET',))
+    def unpublish_event(self, event_id):
+        event = DataGetter.get_event(event_id)
+        event.state = 'Draft'
+        save_to_db(event, 'Event Unpublished')
+        flash("Your event has been unpublished.", "warning")
+        return redirect(url_for('.details_view', event_id=event_id))
+
+    @expose('/<int:event_id>/copy/', methods=('GET',))
+    def copy_event(self, event_id):
+        event = DataGetter.get_event(event_id)
+        event.name = "Copy of " + event.name
+        return self.render('/gentelella/admin/event/new/new.html',
+                           event=event,
+                           is_copy=True,
+                           start_date=datetime.datetime.now() + datetime.timedelta(days=10),
+                           event_types=DataGetter.get_event_types(),
+                           event_topics=DataGetter.get_event_topics())


Test patch:
diff --git a/tests/test_event_functions.py b/tests/test_event_functions.py
new file mode 100644
--- /dev/null
+++ b/tests/test_event_functions.py
@@ -0,0 +1,67 @@
+import unittest
+
+from tests.auth_helper import register, login
+from tests.utils import OpenEventTestCase
+from tests.setup_database import Setup
+from tests.object_mother import ObjectMother
+from open_event import current_app as app
+from open_event.helpers.data import save_to_db
+from open_event.helpers.data_getter import DataGetter
+from flask import url_for
+
+
+class TestEventFunctions(OpenEventTestCase):
+    def setUp(self):
+        self.app = Setup.create_app()
+
+    def test_add_event_to_db(self):
+        event = ObjectMother.get_event()
+        with app.test_request_context():
+            save_to_db(event, "Event saved")
+            self.assertEqual(event.id, event.id)
+
+    def test_event_publish(self):
+        with app.test_request_context():
+            event = ObjectMother.get_event()
+            register(self.app, u'email2@gmail.com', u'test2')
+            login(self.app, 'email2@gmail.com', 'test2')
+            save_to_db(event, "Event saved")
+            url = url_for('event.publish_event', event_id=event.id)
+            rv = self.app.get(url, follow_redirects=True)
+            event = DataGetter.get_event(event.id)
+            self.assertEqual("Published", event.state, msg=event.state)
+
+    def test_event_unpublish(self):
+        with app.test_request_context():
+            event = ObjectMother.get_event()
+            event.state = "Published"
+            register(self.app, u'email2@gmail.com', u'test2')
+            login(self.app, 'email2@gmail.com', 'test2')
+            save_to_db(event, "Event saved")
+            url = url_for('event.unpublish_event', event_id=event.id)
+            rv = self.app.get(url, follow_redirects=True)
+            event = DataGetter.get_event(event.id)
+            self.assertEqual("Draft", event.state, msg=event.state)
+
+    def test_event_delete(self):
+        with app.test_request_context():
+            event = ObjectMother.get_event()
+            register(self.app, u'email2@gmail.com', u'test2')
+            login(self.app, 'email2@gmail.com', 'test2')
+            save_to_db(event, "Event saved")
+            url = url_for('event.delete_view', event_id=event.id)
+            rv = self.app.get(url, follow_redirects=True)
+            self.assertTrue("Your event has been deleted" in rv.data, msg=rv.data)
+
+    def test_event_copy(self):
+        with app.test_request_context():
+            event = ObjectMother.get_event()
+            register(self.app, u'email2@gmail.com', u'test2')
+            login(self.app, 'email2@gmail.com', 'test2')
+            save_to_db(event, "Event saved")
+            url = url_for('event.copy_event', event_id=event.id)
+            rv = self.app.get(url, follow_redirects=True)
+            self.assertTrue("Copy of event1" in rv.data, msg=rv.data)
+
+if __name__ == '__main__':
+    unittest.main()