mirror of
https://github.com/fergalmoran/dss.api.git
synced 2026-01-10 10:34:20 +00:00
Fixed facebook redirect
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
#perform outstanding migrations
|
||||
python manage.py migrate
|
||||
|
||||
#update poorly saved descriptions
|
||||
update spa_userprofile set description = '' where description like('Just another%')
|
||||
|
||||
@@ -6,4 +9,3 @@
|
||||
|
||||
#jiggle the waveforms
|
||||
python manage.py zoom_convert_waveforms
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ urlpatterns = patterns(
|
||||
url(r'^api/v2/', include('api.urls')),
|
||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||
(r'^grappelli/', include('grappelli.urls')),
|
||||
(r'^social/', include('spa.social.urls')),
|
||||
)
|
||||
handler500 = 'spa.views.debug_500'
|
||||
|
||||
|
||||
@@ -3,10 +3,7 @@ from django.conf.urls import patterns, url
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^playlist/(?P<slug>[\w\d_.-]+)/$', 'spa.social.views.playlist', name='social_playlist_slug'),
|
||||
url(r'^redirect/mix/(?P<mix_id>\d+)/$', 'spa.social.views.mix', name='social_redirect_mix'),
|
||||
url(r'^redirect/mix/(?P<slug>[\w\d_.-]+)/$', 'spa.social.views.mix', name='social_redirect_mix_slug'),
|
||||
url(r'^mix/(?P<mix_id>\d+)/$', 'spa.social.views.mix', name='social_mix'),
|
||||
url(r'^mix/(?P<slug>[\w\d_.-]+)/$', 'spa.social.views.mix', name='social_mix_slug'),
|
||||
url(r'^mix/(?P<slug>[\w\d_.-]+)/$', 'spa.social.views.facebook_mix', name='social_mix_slug'),
|
||||
url(r'^user/(?P<user_id>\w+)/$', 'spa.social.views.user', name='social_user'),
|
||||
url(r'^like/(?P<mix_id>\d+)/$', 'spa.social.views.post_like', name='social_like'),
|
||||
url(r'^$', 'spa.social.views.index', name='social_index')
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
import urllib2
|
||||
import logging
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.urlresolvers import resolve
|
||||
from django.http import Http404
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template.context import RequestContext
|
||||
import requests
|
||||
from allauth.socialaccount.models import SocialToken
|
||||
from core.utils.url import wrap_full
|
||||
|
||||
from dss import settings
|
||||
from spa.models import Playlist
|
||||
from spa.models.mix import Mix
|
||||
from spa.models.userprofile import UserProfile
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
"""
|
||||
Handles callbacks from facebook and twitter
|
||||
"""
|
||||
|
||||
|
||||
def _getPayload(request):
|
||||
return {
|
||||
"app_id": settings.FACEBOOK_APP_ID,
|
||||
"site_url": 'http://%s' % Site.objects.get_current().domain,
|
||||
"site_image_url": '%s/img/dss-large.png' % settings.STATIC_URL,
|
||||
}
|
||||
|
||||
|
||||
def facebook_mix(request, slug):
|
||||
try:
|
||||
mix = Mix.objects.get(slug=slug)
|
||||
except Mix.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
image = mix.get_image_url('400x400')
|
||||
mix_url = mix.get_absolute_url()
|
||||
default = _getPayload(request)
|
||||
extras = {
|
||||
"description": mix.description.replace('<br />', '\n'),
|
||||
"title": mix.title,
|
||||
"image_url": image,
|
||||
"mix_url": 'http://%s%s' % (Site.objects.get_current().domain, mix_url)
|
||||
}
|
||||
payload = dict(default.items() + extras.items())
|
||||
response = render_to_response(
|
||||
'social/facebook/mix.html',
|
||||
payload,
|
||||
context_instance=RequestContext(request)
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def playlist(request, args):
|
||||
try:
|
||||
playlist = Playlist.objects.get(slug=args['slug'])
|
||||
except Playlist.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
site_url = settings.DEBUG_URL if settings.DEBUG else Site.objects.get_current().domain
|
||||
image = playlist.get_image_url('400x400')
|
||||
playlist_url = "http://%s%s" % (site_url, playlist.get_absolute_url())
|
||||
default = _getPayload(request)
|
||||
extras = {
|
||||
"description": "Deep South Sounds Playlist by %s" % playlist.user.get_nice_name(),
|
||||
"title": playlist.name,
|
||||
"image_url": image,
|
||||
"playlist_url": playlist_url
|
||||
}
|
||||
payload = dict(default.items() + extras.items())
|
||||
response = render_to_response(
|
||||
'inc/facebook/playlist.html',
|
||||
payload,
|
||||
context_instance=RequestContext(request)
|
||||
)
|
||||
print response.content
|
||||
return response
|
||||
|
||||
|
||||
def user(request, args):
|
||||
try:
|
||||
user = UserProfile.objects.get(slug=args['user_id'])
|
||||
except UserProfile.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
image = user.get_avatar_image()
|
||||
profile_url = user.get_profile_url()
|
||||
default = _getPayload(request)
|
||||
extras = {
|
||||
"title": user.get_nice_name(),
|
||||
"description": user.get_profile_description().replace('<br>', '\n'),
|
||||
"profile_url": wrap_full(profile_url),
|
||||
"image_url": image,
|
||||
}
|
||||
payload = dict(default.items() + extras.items())
|
||||
response = render_to_response(
|
||||
'inc/facebook/user.html',
|
||||
payload,
|
||||
context_instance=RequestContext(request)
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def index(request):
|
||||
response = render_to_response(
|
||||
"inc/facebook/index.html",
|
||||
_getPayload(request),
|
||||
context_instance=RequestContext(request))
|
||||
return response
|
||||
|
||||
|
||||
def social_redirect(request):
|
||||
try:
|
||||
resolver = resolve('/social' + request.path)
|
||||
if resolver is not None:
|
||||
logger.debug("Resolver successfully resolved")
|
||||
return resolver.func(request, resolver.kwargs)
|
||||
else:
|
||||
logger.debug("No resolver found for: $%s" % request.path)
|
||||
except Http404:
|
||||
logger.debug("404 on resolver: $%s" % request.path)
|
||||
return index(request)
|
||||
except Exception, ex:
|
||||
logger.debug("Unhandled exception in social_redirect: $%s" % ex)
|
||||
return index(request)
|
||||
|
||||
|
||||
def post_like(request, mix):
|
||||
try:
|
||||
tokens = SocialToken.objects.filter(
|
||||
account__user=request.user,
|
||||
account__provider='facebook')
|
||||
for token in tokens:
|
||||
url = 'https://graph.facebook.com/%s/og.likes' % token.account.uid
|
||||
values = {
|
||||
'access_token': token.token,
|
||||
'object': mix.get_full_url(),
|
||||
}
|
||||
response = requests.post(url, data=values)
|
||||
if response.status_code == 200:
|
||||
print "Returned %s" % response.json
|
||||
return response.json['id']
|
||||
else:
|
||||
print "Returned status code of %s" % response.status_code
|
||||
except urllib2.HTTPError, httpEx:
|
||||
print httpEx.message
|
||||
except Exception, ex:
|
||||
print ex.message
|
||||
return ""
|
||||
|
||||
|
||||
def delete_like(request, uid):
|
||||
try:
|
||||
tokens = SocialToken.objects.filter(account__user=request.user, account__provider='facebook')
|
||||
for token in tokens:
|
||||
url = "https://graph.facebook.com/%s" % uid
|
||||
values = {
|
||||
'access_token': token.token,
|
||||
}
|
||||
response = requests.delete(url, data=values)
|
||||
return response
|
||||
except Exception, ex:
|
||||
print "Error talking with facebook: %s" % ex.message
|
||||
|
||||
17
templates/social/facebook/mix.html
Normal file
17
templates/social/facebook/mix.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:og="http://ogp.me/ns#"
|
||||
xmlns:fb="https://www.facebook.com/2008/fbml">
|
||||
<head>
|
||||
<meta property="fb:app_id" content="{{ app_id }}" />
|
||||
<meta property="og:url" content="{{ mix_url }}/" />
|
||||
|
||||
<meta property="og:site_name" content="Deep South Sounds"/>
|
||||
<meta property="og:type" content="deepsouthsounds:mix" />
|
||||
<meta property="og:title" content="{{ title }}" />
|
||||
<meta property="og:description" content="{{ description }}" />
|
||||
<meta property="og:image" content="{{ image_url }}" />
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user