diff --git a/cal/api/views.py b/cal/api/views.py index 88fe211..cc2e1d9 100644 --- a/cal/api/views.py +++ b/cal/api/views.py @@ -32,16 +32,18 @@ def sync(request, format=None): return Response("Not logged in") profile = Profile.get_or_create(request.user)[0] + full_sync = request.query_params.get('full_sync') if request.query_params.get('sync_all'): - profile.create_calendars(only_primary=False) + if full_sync: + profile.create_calendars(only_primary=False) calendars = GCalendar.objects.filter(user=request.user) else: profile.create_calendars(only_primary=True) calendars = [profile.main_calendar] for calendar in calendars: - if request.query_params.get('full_sync'): + if full_sync: calendar.sync(full_sync=True) else: calendar.sync() diff --git a/cal/cal/__init__.py b/cal/cal/__init__.py index e69de29..3b91b07 100644 --- a/cal/cal/__init__.py +++ b/cal/cal/__init__.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import, unicode_literals + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ['celery_app'] diff --git a/cal/cal/celery.py b/cal/cal/celery.py new file mode 100644 index 0000000..9f75171 --- /dev/null +++ b/cal/cal/celery.py @@ -0,0 +1,22 @@ +from __future__ import absolute_import, unicode_literals +import os +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cal.settings') + +app = Celery('cal') + +# Using a string here means the worker don't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object('django.conf:settings', namespace='CELERY') + +# Load task modules from all registered Django app configs. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) diff --git a/cal/cal/tasks.py b/cal/cal/tasks.py new file mode 100644 index 0000000..2127e36 --- /dev/null +++ b/cal/cal/tasks.py @@ -0,0 +1,2 @@ +from __future__ import absolute_import, unicode_literals +from celery import shared_task diff --git a/cal/cal/views.py b/cal/cal/views.py index fc82255..4e4b2cf 100644 --- a/cal/cal/views.py +++ b/cal/cal/views.py @@ -1,9 +1,9 @@ -from cal.models import GoogleCredentials, GoogleFlow, Profile, Category, Tag +from cal.models import GoogleCredentials, GoogleFlow, Profile from django.conf import settings from django.contrib.auth import login, logout from django.contrib.auth.decorators import login_required -from django.http import HttpResponseRedirect, HttpResponseBadRequest +from django.http import HttpResponseRedirect, HttpResponseBadRequest, StreamingHttpResponse from django.shortcuts import render from django.template import RequestContext from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie @@ -48,10 +48,30 @@ def generate_categories(request): def accounts_profile(request): """ Shows the account information for a user - For now, redirects to homepage. + For now, demonstrates how to create a streaming HTTP response.. """ - # TODO either remove this view or change Python Social Auth after login - return HttpResponseRedirect("/") + + class TestIter: + def __init__(self): + self.index = 0 + self.values = [5, 1, 0, 6, 2, 9, 3, 2, 6, 4] + + def __iter__(self): + return self + + def next(self): + if self.index < len(self.values): + import time + time.sleep(1) + value = self.values[self.index] + self.index += 1 + return value + else: + raise StopIteration() + + t = TestIter() + + return StreamingHttpResponse(t) @csrf_exempt @psa('social:complete') diff --git a/requirements.txt b/requirements.txt index 309e7d4..f8bb0e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,18 @@ +amqp==2.1.4 appnope==0.1.0 awsebcli==3.7.7 +billiard==3.5.0.2 blessed==1.9.5 botocore==1.4.43 +celery==4.0.2 cement==2.8.2 colorama==0.3.7 +csscompressor==0.9.4 decorator==4.0.9 Django==1.8.4 +django-appconf==1.0.2 django-bower==5.1.0 +django-compressor==2.1 django-filter==0.11.0 django-loginas==0.2.2 djangorestframework==3.2.4 @@ -14,6 +20,7 @@ docker-py==1.7.2 dockerpty==0.4.1 docopt==0.6.2 docutils==0.12 +gnureadline==6.3.3 google-api-python-client==1.4.1 gunicorn==19.3.0 httplib2==0.9.1 @@ -22,6 +29,7 @@ ipython==4.1.2 ipython-genutils==0.1.0 jmespath==0.9.0 jsonfield==1.0.3 +kombu==4.0.2 Markdown==2.6.2 oauth2client==1.5.0 oauthlib==1.0.3 @@ -40,8 +48,10 @@ python-openid==2.2.5 python-social-auth==0.2.14 pytz==2015.7 PyYAML==3.11 +rcssmin==1.0.6 requests==2.9.1 requests-oauthlib==0.6.1 +rjsmin==1.0.12 rsa==3.2 semantic-version==2.5.0 simplegeneric==0.8.1 @@ -50,5 +60,6 @@ six==1.10.0 texttable==0.8.4 traitlets==4.2.1 uritemplate==0.6 +vine==1.1.3 wcwidth==0.1.7 websocket-client==0.37.0