Skip to content

Instantly share code, notes, and snippets.

@souldeux
Last active August 29, 2015 14:07
Show Gist options
  • Save souldeux/cbd909562a3d6214e5d8 to your computer and use it in GitHub Desktop.
Save souldeux/cbd909562a3d6214e5d8 to your computer and use it in GitHub Desktop.
Groove + Django Integration
"""GrooveHQ's Custom Profile app sends a REST-ful request to your server any time you ask it to update info for a user.
If you're a Django user, this means you need to set up a view that returns a JSON object and a urlconf to handle the request."""
"""First set up your URL conf wherever it makes sense in your app. For now, let's assume it's in the root urls.py with your
administrative stuff. Add the following:"""
#root urls.py
#this assumes you've already imported your views with: import views
url(r'groove/$', views.grooveHook, name='groove_hook')
"""Now set up the grooveHook view in your views.py file"""
def grooveHook(request):
#make sure the token in the query matches your API token
api_token = "my_secret_api_key"
if request.GET.get('api_token', None)!= api_token:
return None
else:
#Find a User object with the email included in the query.
email = request.GET.get('email')
user = get_object_or_404(User, email=email)
#Create a dictionary of data to use as the response:
response_data = {
'username': user.username,
'id': user.id,
...
}
#Note: make sure all values in this dictionary are serializable. Ex: wrap datetime objects in str() or it won't work.
#Now send the reponse_data payload back to Groove. If you're in Django 1.6 or lower:
#make sure to: import json
return HttpResponse(json.dumps(response_data), content_type="application/json")
#If you're in Django 1.7, do it the easy way instead:
#make sure to: from django.http import JsonResponse
return JsonResponse(response_data)
"""
One last thing: when you're in your GrooveHQ dashboard setting up your HTML template, remember that the Liquid
template engine != the Django template engine. If you do everything right when setting up the webhook but then
include an invalid template tag in your HTML template, the "Fetch & Render Profile" button will tell you that
YOUR server is returning a 500 error. Before you go crazy debugging your webhook, make sure your template tags
are written properly.
For example, if you want to render a template One Way when a certain variable has a certain value, but render that template a
Different Way when that certain variable has a different value, your Django brain will want to do this:
{% if variable == 'thing' %}
{{stuff}}
{% elif variable == 'otherthing' %}
{{otherstuff}}
{% endif %}
And then you will get a 500 error and you will be sad. Make this one simple change to line 3:
{% elsif variable == 'otherthing' %}
Then you'll be fine. Notice the subtle difference in syntax, and don't assume that stuff from Django templates
are directly translatable to Liquid templates.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment