Repository: fossasia/open-event-server

Issue:
Make social_links of an event importable/exportable
Export social links of an event in the event zip and then further import them from the same. 



Discussion:

Resolving patch:
diff --git a/open_event/api/events.py b/open_event/api/events.py
--- a/open_event/api/events.py
+++ b/open_event/api/events.py
@@ -2,6 +2,7 @@
 from flask import g
 
 from open_event.models.event import Event as EventModel
+from open_event.models.social_link import SocialLink as SocialLinkModel
 from open_event.models.users_events_roles import UsersEventsRoles
 from open_event.models.role import Role
 from open_event.models.user import ORGANIZER
@@ -9,7 +10,7 @@
 
 from .helpers.helpers import get_paginated_list, requires_auth, parse_args
 from .helpers.utils import PAGINATED_MODEL, PaginatedResourceBase, \
-    PAGE_PARAMS, POST_RESPONSES, PUT_RESPONSES, BaseDAO
+    PAGE_PARAMS, POST_RESPONSES, PUT_RESPONSES, BaseDAO, ServiceDAO
 from .helpers import custom_fields as fields
 from helpers.special_fields import EventTypeField, EventTopicField
 
@@ -59,10 +60,25 @@
 })
 
 EVENT_POST = api.clone('EventPost', EVENT)
+SOCIAL_LINK_POST = api.clone('SocialLinkPost', EVENT_SOCIAL)
+
 del EVENT_POST['id']
 del EVENT_POST['creator']
 del EVENT_POST['social_links']
 
+del SOCIAL_LINK_POST['id']
+
+# ###################
+# Data Access Objects
+# ###################
+
+
+class SocialLinkDAO(ServiceDAO):
+    """
+    Social Link DAO
+    """
+    pass
+
 
 class EventDAO(BaseDAO):
     """
@@ -105,6 +121,7 @@ def update(self, event_id, data):
         return BaseDAO.update(self, event_id, payload, validate=False)
 
 
+LinkDAO = SocialLinkDAO(SocialLinkModel, SOCIAL_LINK_POST)
 DAO = EventDAO(EventModel, EVENT_POST)
 
 
diff --git a/open_event/api/helpers/export_helpers.py b/open_event/api/helpers/export_helpers.py
--- a/open_event/api/helpers/export_helpers.py
+++ b/open_event/api/helpers/export_helpers.py
@@ -3,7 +3,8 @@
 import shutil
 from flask_restplus import marshal
 
-from ..events import DAO as EventDAO, EVENT
+from ..events import DAO as EventDAO, EVENT, \
+    LinkDAO as SocialLinkDAO, EVENT_SOCIAL
 from ..microlocations import DAO as MicrolocationDAO, MICROLOCATION
 from ..sessions import DAO as SessionDAO, SESSION, \
     TypeDAO as SessionTypeDAO, SESSION_TYPE_FULL
@@ -19,7 +20,8 @@
     ('speakers', SpeakerDAO, SPEAKER),
     ('sponsors', SponsorDAO, SPONSOR),
     ('tracks', TrackDAO, TRACK),
-    ('session_types', SessionTypeDAO, SESSION_TYPE_FULL)
+    ('session_types', SessionTypeDAO, SESSION_TYPE_FULL),
+    ('social_links', SocialLinkDAO, EVENT_SOCIAL)
 ]
 
 
diff --git a/open_event/api/helpers/import_helpers.py b/open_event/api/helpers/import_helpers.py
--- a/open_event/api/helpers/import_helpers.py
+++ b/open_event/api/helpers/import_helpers.py
@@ -8,7 +8,7 @@
 
 from open_event.helpers.data import save_to_db
 from open_event.models.custom_forms import CustomForms
-from ..events import DAO as EventDAO
+from ..events import DAO as EventDAO, LinkDAO as SocialLinkDAO
 from ..microlocations import DAO as MicrolocationDAO
 from ..sessions import DAO as SessionDAO, TypeDAO as SessionTypeDAO
 from ..speakers import DAO as SpeakerDAO
@@ -18,6 +18,7 @@
 from errors import BaseError, ServerError
 
 IMPORT_SERIES = [
+    ('social_links', SocialLinkDAO),
     ('microlocations', MicrolocationDAO),
     ('sponsors', SponsorDAO),
     ('speakers', SpeakerDAO),


Test patch:
diff --git a/tests/api/test_export_import.py b/tests/api/test_export_import.py
--- a/tests/api/test_export_import.py
+++ b/tests/api/test_export_import.py
@@ -1,5 +1,7 @@
 import unittest
 import json
+import shutil
+import os
 from StringIO import StringIO
 
 from tests.setup_database import Setup
@@ -73,6 +75,7 @@ def _test_import_success(self):
         dic = json.loads(resp.data)
         self.assertEqual(dic['id'], 2)
         self.assertEqual(dic['name'], 'TestEvent')
+        self.assertIn('fb.com', json.dumps(dic['social_links']), dic)
         # No errors generally means everything went fine
         # The method will crash and return 500 in case of any problem
 
@@ -90,5 +93,31 @@ def test_import_extended(self):
         self._test_import_success()
 
 
+class TestImportOTS(OpenEventTestCase):
+    """
+    Tests import of OTS sample
+    """
+    def setUp(self):
+        self.app = Setup.create_app()
+        with app.test_request_context():
+            register(self.app, u'test@example.com', u'test')
+
+    def _upload(self, data, url, filename='anything'):
+        return self.app.post(
+            url,
+            data={'file': (StringIO(data), filename)}
+        )
+
+    def _test_import_ots(self):
+        dir_path = 'samples/ots16'
+        shutil.make_archive(dir_path, 'zip', dir_path)
+        file = open(dir_path + '.zip', 'r').read()
+        os.remove(dir_path + '.zip')
+        upload_path = get_path('import', 'json')
+        resp = self._upload(file, upload_path, 'event.zip')
+        self.assertEqual(resp.status_code, 200)
+        self.assertIn('Open Tech Summit', resp.data)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/tests/api/utils.py b/tests/api/utils.py
--- a/tests/api/utils.py
+++ b/tests/api/utils.py
@@ -8,6 +8,7 @@
 from open_event.models.sponsor import Sponsor
 from open_event.models.microlocation import Microlocation
 from open_event.models.session_type import SessionType
+from open_event.models.social_link import SocialLink
 from open_event.models.track import Track
 
 
@@ -56,6 +57,7 @@ def create_services(event_id, serial_no=''):
     test_sponsor = 'TestSponsor{}_{}'.format(serial_no, event_id)
     test_sponsor_type = 'TestSponsorType{}_{}'.format(serial_no, event_id)
     test_session_type = 'TestSessionType{}_{}'.format(serial_no, event_id)
+    test_social_link = 'TestSocialLink{}_{}'.format(serial_no, event_id)
 
     microlocation = Microlocation(name=test_micro, event_id=event_id)
     track = Track(
@@ -82,7 +84,13 @@ def create_services(event_id, serial_no=''):
                       event_id=event_id)
     sponsor = Sponsor(name=test_sponsor, sponsor_type=test_sponsor_type,
                       event_id=event_id, level='level')
+    social_link = SocialLink(
+        name=test_social_link,
+        link='http://fb.com',
+        event_id=event_id
+    )
 
+    save_to_db(social_link, 'SocialLink saved')
     save_to_db(session_type, 'SessionType saved')
     save_to_db(microlocation, 'Microlocation saved')
     save_to_db(track, 'Track saved')