Permalink
Browse files

recommit this patch: add debug-user method, cleanup a little more code

  • Loading branch information...
1 parent a6e8348 commit d912820683288512e6ff6aa16c00a54f08890155 @jlongster jlongster committed Dec 12, 2011
Showing with 115 additions and 47 deletions.
  1. +1 −0 .gitignore
  2. +39 −0 apps/news/README
  3. +3 −2 apps/news/urls.py
  4. +68 −45 apps/news/views.py
  5. +4 −0 settings.py
View
@@ -11,3 +11,4 @@ build.py
*-all.css
*-min.js
*-all.js
+.#*
View
@@ -1,6 +1,35 @@
This "news" app provides a service for managing Mozilla newsletters.
+Available newsletters:
+
+* mozilla-and-you
+* mobile
+* beta
+* aurora
+* about-mozilla
+* drumbeat
+* addons
+* hacks
+* labs
+* qa-news
+* student-reps
+* about-standards
+* mobile-addon-dev
+* addon-dev
+* join-mozilla
+* mozilla-phone
+* app-dev
+
+If 'auth-required' is specified, a token must be suffixed onto the API
+URL, such as:
+
+/news/user/<token>/
+
+This is a user-specific token given away by the email backend or
+basket in some manner (i.e. emailed to the user from basket). This
+token allows clients to do more powerful things with the user.
+
The following URLs are available (assuming "/news" is app url):
/news/subscribe
@@ -56,3 +85,13 @@ The following URLs are available (assuming "/news" is app url):
fields. Note that the user is only subscribed to "newsletters" after
this, meaning the user will be unsubscribed to all other
newsletters. "optin" should be Y or N and opts in/out the user.
+
+/news/debug-user
+
+ method: GET
+ fields: email, supertoken
+
+ This is the same as a GET request to /user, except that you must
+ pass in the email and a supertoken as GET params. The supertoken is
+ a special token that should never be made public and lets devs debug
+ users to make sure they were entered into the system correctly.
View
@@ -1,9 +1,10 @@
from django.conf.urls.defaults import *
-from views import subscribe, unsubscribe, user, delete_user
+from views import subscribe, unsubscribe, user, delete_user, debug_user
urlpatterns = patterns('',
url('^subscribe/$', subscribe),
url('^unsubscribe/(.*)/$', unsubscribe),
url('^user/(.*)/$', user),
- url('^delete/(.*)/$', delete_user)
+ url('^delete/(.*)/$', delete_user),
+ url('^debug-user/$', debug_user)
)
View
@@ -21,9 +21,10 @@ def logged_in(f):
def wrapper(request, token, *args, **kwargs):
subscriber = Subscriber.objects.filter(token=token)
if not subscriber.exists():
- return json_response({'desc': 'Must have valid token for this request'},
+ return json_response({'status': 'error',
+ 'desc': 'Must have valid token for this request'},
status=403)
-
+
request.subscriber = subscriber[0]
return f(request, token, *args, **kwargs)
return wrapper
@@ -44,6 +45,48 @@ def update_user_task(request, type, data=None):
user and user.email,
type))
+def get_user(email):
+ newsletters = newsletter_fields()
+
+ fields = [
+ 'EMAIL_ADDRESS_',
+ 'EMAIL_FORMAT_',
+ 'COUNTRY_',
+ 'LANGUAGE_ISO2'
+ ]
+
+ for nl in newsletters:
+ fields.append('%s_FLG' % nl)
+
+ try:
+ rs = Responsys()
+ rs.login(settings.RESPONSYS_USER, settings.RESPONSYS_PASS)
+ user = rs.retrieve_list_members(email,
+ settings.RESPONSYS_FOLDER,
+ settings.RESPONSYS_LIST,
+ fields)
+ except NewsletterException, e:
+ return json_response({'status': 'error',
+ 'desc': e.message},
+ status=500)
+ except UnauthorizedException, e:
+ return json_response({'status': 'error',
+ 'desc': 'Responsys auth failure'},
+ status=500)
+
+ user_data = {
+ 'email': email,
+ 'format': user['EMAIL_FORMAT_'],
+ 'country': user['COUNTRY_'],
+ 'lang': user['LANGUAGE_ISO2'],
+ 'newsletters': [newsletter_name(nl) for nl in newsletters
+ if user.get('%s_FLG' % nl, False) == 'Y']
+ }
+
+ rs.logout()
+
+ return json_response(user_data)
+
## Views
@csrf_exempt
@@ -52,7 +95,8 @@ def subscribe(request):
return HttpResponseBadRequest("Only POST supported")
if 'newsletters' not in request.POST:
- return json_response({'desc': 'newsletters is missing'},
+ return json_response({'status': 'error',
+ 'desc': 'newsletters is missing'},
status=500)
# If the user isn't opting in yet, we tell the system to
@@ -74,7 +118,6 @@ def unsubscribe(request, token):
data = request.POST.copy()
if data.get('optout', 'N') == 'Y':
- data['optin'] = 'N'
data['newsletters'] = ','.join(newsletter_names())
update_user_task(request, UNSUBSCRIBE, data)
@@ -88,45 +131,7 @@ def user(request, token):
update_user_task(request, SET)
return json_response({})
- newsletters = newsletter_fields()
-
- fields = [
- 'EMAIL_ADDRESS_',
- 'EMAIL_FORMAT_',
- 'COUNTRY_',
- 'LANGUAGE_ISO2'
- ]
-
- for nl in newsletters:
- fields.append('%s_FLG' % nl)
-
- try:
- rs = Responsys()
- rs.login(settings.RESPONSYS_USER, settings.RESPONSYS_PASS)
- user = rs.retrieve_list_members(request.subscriber.email,
- settings.RESPONSYS_FOLDER,
- settings.RESPONSYS_LIST,
- fields)
- except NewsletterException, e:
- return json_response({'desc': e.message},
- status=500)
- except UnauthorizedException, e:
- return json_response({'desc': 'Responsys auth failure'},
- status=500)
-
- user_data = {
- 'email': request.subscriber.email,
- 'format': user['EMAIL_FORMAT_'],
- 'country': user['COUNTRY_'],
- 'lang': user['LANGUAGE_ISO2'],
- 'newsletters': [newsletter_name(nl) for nl in newsletters
- if user.get('%s_FLG' % nl, False) == 'Y']
- }
-
- rs.logout()
-
- return json_response(user_data)
-
+ return get_user(request.subscriber.email)
@logged_in
@csrf_exempt
@@ -139,11 +144,29 @@ def delete_user(request, token):
settings.RESPONSYS_LIST)
rs.logout()
except NewsletterException, e:
- return json_response({'desc': e.message},
+ return json_response({'status': 'error',
+ 'desc': e.message},
status=500)
except UnauthorizedException, e:
- return json_response({'desc': 'Responsys auth failure'},
+ return json_response({'status': 'error',
+ 'desc': 'Responsys auth failure'},
status=500)
request.subscriber.delete()
return json_response({})
+
+def debug_user(request):
+ if not 'email' in request.GET or not 'supertoken' in request.GET:
+ return json_response(
+ {'status': 'error',
+ 'desc': 'Using debug_user, you need to pass the '
+ '`email` and `supertoken` GET parameters'},
+ status=500
+ )
+
+ if request.GET['supertoken'] != settings.SUPERTOKEN:
+ return json_response({'status': 'error',
+ 'desc': 'Bad supertoken'},
+ status=401)
+
+ return get_user(request.GET['email'])
View
@@ -148,6 +148,10 @@ def JINJA_CONFIG():
RESPONSYS_FOLDER = '!MasterData'
RESPONSYS_LIST = 'TEST_CONTACTS_LIST'
+# This is a token that bypasses the news app auth in certain ways to
+# make debugging easier
+# SUPERTOKEN = <token>
+
# Uncomment these to use Celery, use eager for local dev
# CELERY_ALWAYS_EAGER = False
# BROKER_HOST = 'localhost'

0 comments on commit d912820

Please sign in to comment.