From 41d3730480bd8a8ca1288cc0bc049a44d7c10d17 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Thu, 9 Apr 2020 21:37:57 +0800 Subject: [PATCH 1/3] Remove the remotedb app --- src/remotedb/__init__.py | 1 - src/remotedb/apps.py | 47 ------- src/remotedb/dumpers.py | 115 ------------------ src/remotedb/management/__init__.py | 0 src/remotedb/management/commands/__init__.py | 0 .../management/commands/syncfirebase.py | 67 ---------- src/remotedb/triggers.py | 104 ---------------- 7 files changed, 334 deletions(-) delete mode 100644 src/remotedb/__init__.py delete mode 100644 src/remotedb/apps.py delete mode 100644 src/remotedb/dumpers.py delete mode 100644 src/remotedb/management/__init__.py delete mode 100644 src/remotedb/management/commands/__init__.py delete mode 100644 src/remotedb/management/commands/syncfirebase.py delete mode 100644 src/remotedb/triggers.py diff --git a/src/remotedb/__init__.py b/src/remotedb/__init__.py deleted file mode 100644 index 8997d8fe4..000000000 --- a/src/remotedb/__init__.py +++ /dev/null @@ -1 +0,0 @@ -default_app_config = 'remotedb.apps.AppConfig' diff --git a/src/remotedb/apps.py b/src/remotedb/apps.py deleted file mode 100644 index ce9d73c76..000000000 --- a/src/remotedb/apps.py +++ /dev/null @@ -1,47 +0,0 @@ -from django.apps import AppConfig as BaseAppConfig -from django.conf import settings -from django.db.models.signals import post_save -from django.utils.translation import gettext_lazy as _ - - -class AppConfig(BaseAppConfig): - """Default config for the remotedb app. - """ - name = 'remotedb' - verbose_name = _('Remote DB') - - def ready(self): - from . import triggers - - def sync_sponsored_talk_detail(sender, instance, *args, **kwargs): - triggers.sync_sponsored_talk_detail(instance) - - def sync_proposal_detail(sender, instance, *args, **kwargs): - triggers.sync_proposal_detail(instance) - - def sync_schedule(*args, **kwargs): - triggers.sync_schedule() - - def sync_user_events(sender, instance, *args, **kwargs): - triggers.sync_user_events(instance) - - post_save.connect( - sync_proposal_detail, - sender='events.Schedule', - weak=False, - ) - post_save.connect( - sync_sponsored_talk_detail, - sender='events.SponsoredEvent', - weak=False, - ) - post_save.connect( - sync_proposal_detail, - sender='proposals.TalkProposal', - weak=False, - ) - post_save.connect( - sync_user_events, - sender=settings.AUTH_USER_MODEL, - weak=False, - ) diff --git a/src/remotedb/dumpers.py b/src/remotedb/dumpers.py deleted file mode 100644 index 8deee76eb..000000000 --- a/src/remotedb/dumpers.py +++ /dev/null @@ -1,115 +0,0 @@ -import collections -import functools -import urllib.parse - -from django.core.serializers.json import DjangoJSONEncoder - - -SITE_PREFIX = 'https://tw.pycon.org/2016/media/' - -USER_DUMP_KEYS = [ - 'bio', 'email', 'speaker_name', - 'facebook_profile_url', 'github_id', 'twitter_id', -] - -PROPOSAL_DUMP_KEYS = SPONSORED_EVENT_DUMP_KEYS = [ - 'abstract', 'category', 'detailed_description', 'language', 'python_level', - 'recording_policy', 'slide_link', 'title', -] - - -def dump_user(user): - data = {key: getattr(user, key) for key in USER_DUMP_KEYS} - if user.photo: - data['photo_url'] = urllib.parse.urljoin(SITE_PREFIX, user.photo.url) - return data - - -def dump_proposal(proposal): - data = {key: getattr(proposal, key) for key in PROPOSAL_DUMP_KEYS} - data['speakers'] = [dump_user(info.user) for info in proposal.speakers] - return data - - -def dump_sponsored_event_detail(event): - data = {key: getattr(event, key) for key in SPONSORED_EVENT_DUMP_KEYS} - data['speakers'] = [dump_user(event.host)] - return data - - -json_encoder = DjangoJSONEncoder() - - -def event_dumper(f): - """Decorator to provide dumping of common event fields. - """ - @functools.wraps(f) - def inner(obj): - data = { - 'begin_time': json_encoder.encode(obj.begin_time.value).strip('"'), - 'end_time': json_encoder.encode(obj.end_time.value).strip('"'), - 'location': obj.location, - } - data.update(f(obj)) - return data - - return inner - - -@event_dumper -def dump_keynote_event(event): - return { - 'type': 'keynote', - 'speakers': [event.speaker_name], - } - - -@event_dumper -def dump_custom_event(event): - return { - 'type': 'custom', - 'title': event.title, - } - - -@event_dumper -def dump_sponsored_event(event): - return { - 'type': 'sponsored_talk', - 'title': event.title, - 'speakers': [event.host.speaker_name], - 'detail_id': 'sponsored_{}'.format(event.pk) - } - - -@event_dumper -def dump_proposed_talk_event(event): - return { - 'type': 'talk', - 'title': event.proposal.title, - 'speakers': [ - speaker.user.speaker_name - for speaker in event.proposal.speakers - ], - 'detail_id': str(event.proposal.pk), - } - - -EVENT_LOADERS = { - 'keynoteevent': dump_keynote_event, - 'customevent': dump_custom_event, - 'sponsoredevent': dump_sponsored_event, - 'proposedtalkevent': dump_proposed_talk_event, -} - - -def dump_schedule(event_iter): - schedule_data_lists = collections.defaultdict(list) - for event in event_iter: - loader = EVENT_LOADERS[event._meta.model_name] - data = loader(event) - key = data['begin_time'].split('T', 1)[0] - schedule_data_lists[key].append(data) - for data_list in schedule_data_lists.values(): - data_list.sort(key=lambda data: (data['begin_time'], data['location'])) - return schedule_data_lists diff --git a/src/remotedb/management/__init__.py b/src/remotedb/management/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/remotedb/management/commands/__init__.py b/src/remotedb/management/commands/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/remotedb/management/commands/syncfirebase.py b/src/remotedb/management/commands/syncfirebase.py deleted file mode 100644 index 7849296d0..000000000 --- a/src/remotedb/management/commands/syncfirebase.py +++ /dev/null @@ -1,67 +0,0 @@ -import itertools - -from firebase.firebase import FirebaseApplication - -from django.conf import settings -from django.core.management.base import BaseCommand - -from events.models import ( - CustomEvent, KeynoteEvent, ProposedTalkEvent, SponsoredEvent, -) -from remotedb import dumpers - - -class Command(BaseCommand): - - help = 'Sync Firebase instance with local data.' - - def handle(self, *args, **options): - custeom_event_qs = ( - CustomEvent.objects - .select_related('begin_time', 'end_time') - ) - keynote_event_qs = ( - KeynoteEvent.objects - .select_related('begin_time', 'end_time') - ) - proposed_talk_event_qs = ( - ProposedTalkEvent.objects - .select_related('begin_time', 'end_time', 'proposal__submitter') - ) - sponsored_event_qs = ( - SponsoredEvent.objects - .select_related('begin_time', 'end_time', 'host') - ) - - self.stdout.write('Dumping schedule...', ending=' ') - schedule_data = dumpers.dump_schedule(itertools.chain( - custeom_event_qs, - keynote_event_qs, - proposed_talk_event_qs, - sponsored_event_qs, - )) - self.stdout.write('Done') - - self.stdout.write('Dumping event details...', ending=' ') - event_data = { - str(event.proposal.pk): dumpers.dump_proposal(event.proposal) - for event in proposed_talk_event_qs - } - event_data.update({ - 'sponsored_{}'.format(e.pk): dumpers.dump_sponsored_event_detail(e) - for e in sponsored_event_qs - }) - self.stdout.write('Done') - - app = FirebaseApplication(settings.FIREBASE_URL) - - self.stdout.write('Uploading schedule...', ending=' ') - for key, value in schedule_data.items(): - self.stdout.write(key, ending=' ') - data = {'date': key, 'slots': value} - app.put('{}/schedule'.format(settings.FIREBASE_DB), key, data) - self.stdout.write('Done') - - self.stdout.write('Uploading events...', ending=' ') - app.put(settings.FIREBASE_DB, 'events', event_data) - self.stdout.write('Done') diff --git a/src/remotedb/triggers.py b/src/remotedb/triggers.py deleted file mode 100644 index 18f909605..000000000 --- a/src/remotedb/triggers.py +++ /dev/null @@ -1,104 +0,0 @@ -import functools -import itertools -import logging - -from firebase.firebase import FirebaseApplication - -from django.conf import settings - -from django_q import tasks - -from events.models import ( - CustomEvent, KeynoteEvent, ProposedTalkEvent, SponsoredEvent, -) -from proposals.models import TalkProposal - -from . import dumpers - - -logger = logging.getLogger(__name__) - - -def mock_on_debug(f): - """Mock out decorated function when `settings.DEBUG` is True. - """ - @functools.wraps(f) - def wrapped(*args, **kwargs): - if settings.DJANGO_Q_DEBUG: - logger.info('Called {0} with args={args!r}, kwargs={kw!r}'.format( - f.__name__, args=args, kw=kwargs, - )) - return - return f(*args, **kwargs) - - return wrapped - - -def update_firebase(path, key, data): - app = FirebaseApplication(settings.FIREBASE_URL) - app.put('{}/{}'.format(settings.FIREBASE_DB, path), key, data) - - -def _sync_proposal_detail(proposal): - detail_data = dumpers.dump_proposal(proposal) - update_firebase('events', str(proposal.pk), detail_data) - - -@mock_on_debug -def sync_proposal_detail(proposal): - tasks.async_task(_sync_proposal_detail, proposal) - - -def _sync_sponsored_talk_detail(event): - detail_data = dumpers.dump_sponsored_event_detail(event) - update_firebase('events', 'sponsored_{}'.format(event.pk), detail_data) - - -@mock_on_debug -def sync_sponsored_talk_detail(event): - tasks.async_task(_sync_sponsored_talk_detail, event) - - -def _sync_schedule(): - custeom_event_qs = ( - CustomEvent.objects - .select_related('begin_time', 'end_time') - ) - keynote_event_qs = ( - KeynoteEvent.objects - .select_related('begin_time', 'end_time') - ) - proposed_talk_event_qs = ( - ProposedTalkEvent.objects - .select_related('begin_time', 'end_time', 'proposal__submitter') - ) - sponsored_event_qs = ( - SponsoredEvent.objects - .select_related('begin_time', 'end_time', 'host') - ) - schedule_data = dumpers.dump_schedule(itertools.chain( - custeom_event_qs, - keynote_event_qs, - proposed_talk_event_qs, - sponsored_event_qs, - )) - for key, value in schedule_data.items(): - schedule_date_data = {'date': key, 'slots': value} - update_firebase('schedule', key, schedule_date_data) - - -@mock_on_debug -def sync_schedule(): - tasks.async_task(_sync_schedule) - - -def _sync_user_events(user): - for e in SponsoredEvent.objects.filter(host=user): - _sync_sponsored_talk_detail(e) - for p in TalkProposal.objects.filter_viewable(user=user): - _sync_proposal_detail(p) - - -@mock_on_debug -def sync_user_events(user): - tasks.async_task(_sync_user_events, user) From 0a8bc98807e2288610fed5838e7c8765fbeb9bee Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Thu, 9 Apr 2020 21:41:02 +0800 Subject: [PATCH 2/3] Remove remotedb related config & translation --- src/locale/_src/LC_MESSAGES/django.po | 4 ---- src/locale/en_US/LC_MESSAGES/django.po | 4 ---- src/locale/zh_Hant/LC_MESSAGES/django.po | 4 ---- src/pycontw2016/settings/base.py | 1 - 4 files changed, 13 deletions(-) diff --git a/src/locale/_src/LC_MESSAGES/django.po b/src/locale/_src/LC_MESSAGES/django.po index 1d270bf59..28044d25d 100644 --- a/src/locale/_src/LC_MESSAGES/django.po +++ b/src/locale/_src/LC_MESSAGES/django.po @@ -878,10 +878,6 @@ msgstr "" msgid "Day 3" msgstr "" -#: remotedb/apps.py:11 -msgid "Remote DB" -msgstr "" - #: reviews/admin.py:50 msgid "Key" msgstr "" diff --git a/src/locale/en_US/LC_MESSAGES/django.po b/src/locale/en_US/LC_MESSAGES/django.po index 7dfa31dc1..85e5f742e 100644 --- a/src/locale/en_US/LC_MESSAGES/django.po +++ b/src/locale/en_US/LC_MESSAGES/django.po @@ -871,10 +871,6 @@ msgstr "Prefer 45min" msgid "Day 3" msgstr "Day 3" -#: remotedb/apps.py:11 -msgid "Remote DB" -msgstr "Remote DB" - #: reviews/admin.py:50 msgid "Key" msgstr "Key" diff --git a/src/locale/zh_Hant/LC_MESSAGES/django.po b/src/locale/zh_Hant/LC_MESSAGES/django.po index 32a47f4d1..27974ba44 100644 --- a/src/locale/zh_Hant/LC_MESSAGES/django.po +++ b/src/locale/zh_Hant/LC_MESSAGES/django.po @@ -878,10 +878,6 @@ msgstr "偏好 45 分鐘" msgid "Day 3" msgstr "第 3 天" -#: remotedb/apps.py:11 -msgid "Remote DB" -msgstr "遠端資料庫" - #: reviews/admin.py:50 msgid "Key" msgstr "鍵" diff --git a/src/pycontw2016/settings/base.py b/src/pycontw2016/settings/base.py index db55ad68c..e83c072d6 100644 --- a/src/pycontw2016/settings/base.py +++ b/src/pycontw2016/settings/base.py @@ -108,7 +108,6 @@ 'core', 'events', 'proposals', - 'remotedb', 'reviews', 'sponsors', 'users', From 9a19f34795e370d8db2c3c0d000db6a4d8eb7c77 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Thu, 9 Apr 2020 21:42:04 +0800 Subject: [PATCH 3/3] Remove firebase package and related config --- requirements/base.txt | 4 ---- src/pycontw2016/settings/base.py | 4 ---- 2 files changed, 8 deletions(-) diff --git a/requirements/base.txt b/requirements/base.txt index 25ff937db..4282755aa 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -70,10 +70,6 @@ lxml==4.5.0 # https://python-pillow.org/ Pillow==7.0.0 -# Python interface to the Firebase's REST API. -# http://ozgur.github.io/python-firebase/ -python-firebase==1.2 - # World Timezone Definitions for Python. # https://pythonhosted.org/pytz/ pytz==2019.3 diff --git a/src/pycontw2016/settings/base.py b/src/pycontw2016/settings/base.py index e83c072d6..1d3a858e8 100644 --- a/src/pycontw2016/settings/base.py +++ b/src/pycontw2016/settings/base.py @@ -268,10 +268,6 @@ def node_bin(name): CRISPY_TEMPLATE_PACK = 'bootstrap3' -FIREBASE_URL = 'https://pycon-630b8.firebaseio.com' - -FIREBASE_DB = '/pycontw2018' - WERKZEUG_DEBUG = env.bool('WERKZEUG_DEBUG', default=True) GTM_TRACK_ID = None