2011-01-21 22:00:36 -05:00
|
|
|
import os
|
|
|
|
import base64
|
2012-08-24 18:07:44 -07:00
|
|
|
import urlparse
|
2012-08-25 19:58:03 -07:00
|
|
|
import datetime
|
2011-01-21 22:00:36 -05:00
|
|
|
from django.conf import settings
|
2011-01-22 11:22:14 -05:00
|
|
|
from django.http import HttpResponse
|
2011-01-21 20:29:19 -05:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
2011-04-24 18:17:44 -04:00
|
|
|
from django.contrib.auth import login as login_user
|
|
|
|
from django.contrib.auth import logout as logout_user
|
|
|
|
from apps.reader.forms import SignupForm, LoginForm
|
2011-01-21 20:29:19 -05:00
|
|
|
from apps.profile.models import Profile
|
2012-08-24 15:07:10 -07:00
|
|
|
from apps.social.models import MSocialProfile, MSharedStory, MSocialSubscription
|
2012-08-24 14:34:53 -07:00
|
|
|
from apps.rss_feeds.models import Feed
|
2011-01-21 20:29:19 -05:00
|
|
|
from apps.reader.models import UserSubscription, UserSubscriptionFolders
|
2011-01-20 19:14:54 -05:00
|
|
|
from utils import json_functions as json
|
2011-03-21 10:15:18 -04:00
|
|
|
from utils import log as logging
|
2012-08-25 19:19:44 -07:00
|
|
|
from utils.feed_functions import relative_timesince
|
2011-01-20 19:14:54 -05:00
|
|
|
|
2011-04-24 18:17:44 -04:00
|
|
|
@json.json_view
|
|
|
|
def login(request):
|
|
|
|
code = -1
|
2011-09-14 20:08:40 -07:00
|
|
|
errors = None
|
|
|
|
|
2011-04-24 18:17:44 -04:00
|
|
|
if request.method == "POST":
|
|
|
|
form = LoginForm(data=request.POST)
|
2011-09-14 20:08:40 -07:00
|
|
|
if form.errors:
|
|
|
|
errors = form.errors
|
2011-04-24 18:17:44 -04:00
|
|
|
if form.is_valid():
|
|
|
|
login_user(request, form.get_user())
|
2011-10-12 22:42:30 -07:00
|
|
|
logging.user(request, "~FG~BB~SKAPI Login~FW")
|
2011-04-24 18:17:44 -04:00
|
|
|
code = 1
|
2011-11-03 22:15:20 -07:00
|
|
|
else:
|
2011-11-03 22:21:11 -07:00
|
|
|
errors = dict(method="Invalid method. Use POST. You used %s" % request.method)
|
2011-11-03 22:15:20 -07:00
|
|
|
|
2011-09-14 20:08:40 -07:00
|
|
|
return dict(code=code, errors=errors)
|
2011-04-24 18:17:44 -04:00
|
|
|
|
2011-05-12 17:56:24 -04:00
|
|
|
@json.json_view
|
2011-04-24 18:17:44 -04:00
|
|
|
def signup(request):
|
|
|
|
code = -1
|
2011-09-14 20:08:40 -07:00
|
|
|
errors = None
|
|
|
|
|
2011-04-24 18:17:44 -04:00
|
|
|
if request.method == "POST":
|
|
|
|
form = SignupForm(data=request.POST)
|
2011-09-14 20:08:40 -07:00
|
|
|
if form.errors:
|
|
|
|
errors = form.errors
|
2011-04-24 18:17:44 -04:00
|
|
|
if form.is_valid():
|
|
|
|
new_user = form.save()
|
|
|
|
login_user(request, new_user)
|
2011-10-12 22:42:30 -07:00
|
|
|
logging.user(request, "~FG~SB~BBAPI NEW SIGNUP~FW")
|
2011-04-24 18:17:44 -04:00
|
|
|
code = 1
|
2011-11-03 22:15:20 -07:00
|
|
|
else:
|
2011-11-03 22:21:11 -07:00
|
|
|
errors = dict(method="Invalid method. Use POST. You used %s" % request.method)
|
2011-11-03 22:15:20 -07:00
|
|
|
|
2011-04-24 18:17:44 -04:00
|
|
|
|
2011-09-14 20:08:40 -07:00
|
|
|
return dict(code=code, errors=errors)
|
2011-04-24 18:17:44 -04:00
|
|
|
|
2011-05-12 17:56:24 -04:00
|
|
|
@json.json_view
|
2011-04-24 18:17:44 -04:00
|
|
|
def logout(request):
|
|
|
|
code = 1
|
2011-09-16 09:26:22 -07:00
|
|
|
logging.user(request, "~FG~BBAPI Logout~FW")
|
2011-04-24 18:17:44 -04:00
|
|
|
logout_user(request)
|
|
|
|
|
|
|
|
return dict(code=code)
|
2011-04-05 10:50:39 -04:00
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
def add_site_load_script(request, token):
|
|
|
|
code = 0
|
2011-05-08 19:52:41 -04:00
|
|
|
usf = None
|
2012-08-25 19:56:07 -07:00
|
|
|
profile = None;
|
2012-08-11 17:18:35 -07:00
|
|
|
user_profile = None;
|
2012-08-11 13:53:00 -07:00
|
|
|
def image_base64(image_name, path='icons/silk/'):
|
2012-08-11 17:18:35 -07:00
|
|
|
image_file = open(os.path.join(settings.MEDIA_ROOT, 'img/%s%s' % (path, image_name)))
|
2011-03-21 10:15:18 -04:00
|
|
|
return base64.b64encode(image_file.read())
|
|
|
|
|
2012-08-11 17:18:35 -07:00
|
|
|
accept_image = image_base64('accept.png')
|
|
|
|
error_image = image_base64('error.png')
|
|
|
|
new_folder_image = image_base64('arrow_down_right.png')
|
|
|
|
add_image = image_base64('add.png')
|
2011-03-21 10:15:18 -04:00
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
try:
|
2012-07-31 11:41:30 -07:00
|
|
|
profiles = Profile.objects.filter(secret_token=token)
|
|
|
|
if profiles:
|
|
|
|
profile = profiles[0]
|
|
|
|
usf = UserSubscriptionFolders.objects.get(
|
|
|
|
user=profile.user
|
|
|
|
)
|
2012-08-31 13:51:24 -07:00
|
|
|
user_profile = MSocialProfile.get_user(user_id=profile.user.pk)
|
2012-07-31 11:41:30 -07:00
|
|
|
else:
|
|
|
|
code = -1
|
2011-01-21 20:29:19 -05:00
|
|
|
except Profile.DoesNotExist:
|
|
|
|
code = -1
|
|
|
|
except UserSubscriptionFolders.DoesNotExist:
|
|
|
|
code = -1
|
|
|
|
|
2012-08-11 13:53:00 -07:00
|
|
|
return render_to_response('api/share_bookmarklet.js', {
|
2011-03-21 10:15:18 -04:00
|
|
|
'code': code,
|
|
|
|
'token': token,
|
2012-04-21 17:15:45 -07:00
|
|
|
'folders': (usf and usf.folders) or [],
|
2012-08-11 17:18:35 -07:00
|
|
|
'user': profile and profile.user or {},
|
|
|
|
'user_profile': user_profile and json.encode(user_profile.to_json()) or {},
|
2011-03-21 10:15:18 -04:00
|
|
|
'accept_image': accept_image,
|
|
|
|
'error_image': error_image,
|
|
|
|
'add_image': add_image,
|
|
|
|
'new_folder_image': new_folder_image,
|
|
|
|
},
|
|
|
|
context_instance=RequestContext(request),
|
|
|
|
mimetype='application/javascript')
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-20 19:14:54 -05:00
|
|
|
def add_site(request, token):
|
2011-03-21 10:15:18 -04:00
|
|
|
code = 0
|
|
|
|
url = request.GET['url']
|
|
|
|
folder = request.GET['folder']
|
2011-03-21 20:37:58 -04:00
|
|
|
new_folder = request.GET.get('new_folder')
|
2011-03-21 10:15:18 -04:00
|
|
|
callback = request.GET['callback']
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-22 11:22:14 -05:00
|
|
|
if not url:
|
2011-01-21 20:29:19 -05:00
|
|
|
code = -1
|
2011-01-22 11:22:14 -05:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
profile = Profile.objects.get(secret_token=token)
|
2011-03-21 10:15:18 -04:00
|
|
|
if new_folder:
|
|
|
|
usf, _ = UserSubscriptionFolders.objects.get_or_create(user=profile.user)
|
|
|
|
usf.add_folder(folder, new_folder)
|
|
|
|
folder = new_folder
|
2011-01-22 11:22:14 -05:00
|
|
|
code, message, us = UserSubscription.add_subscription(
|
|
|
|
user=profile.user,
|
|
|
|
feed_address=url,
|
2011-01-22 21:42:58 -05:00
|
|
|
folder=folder,
|
|
|
|
bookmarklet=True
|
2011-01-22 11:22:14 -05:00
|
|
|
)
|
|
|
|
except Profile.DoesNotExist:
|
|
|
|
code = -1
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-22 18:25:16 -05:00
|
|
|
if code > 0:
|
2011-01-22 21:42:58 -05:00
|
|
|
message = 'OK'
|
2011-01-22 18:25:16 -05:00
|
|
|
|
2012-08-24 14:34:53 -07:00
|
|
|
logging.user(profile.user, "~FRAdding URL from site: ~SB%s (in %s)" % (url, folder),
|
|
|
|
request=request)
|
2011-03-21 10:15:18 -04:00
|
|
|
|
2011-01-22 11:22:14 -05:00
|
|
|
return HttpResponse(callback + '(' + json.encode({
|
2011-01-21 20:29:19 -05:00
|
|
|
'code': code,
|
|
|
|
'message': message,
|
2012-01-26 09:32:24 -08:00
|
|
|
'usersub': us and us.feed_id,
|
2012-08-17 23:29:17 -07:00
|
|
|
}) + ')', mimetype='text/plain')
|
|
|
|
|
|
|
|
def check_share_on_site(request, token):
|
|
|
|
code = 0
|
|
|
|
story_url = request.GET['story_url']
|
2012-08-24 14:34:53 -07:00
|
|
|
rss_url = request.GET.get('rss_url')
|
2012-08-17 23:29:17 -07:00
|
|
|
callback = request.GET['callback']
|
2012-08-24 14:34:53 -07:00
|
|
|
other_stories = None
|
|
|
|
same_stories = None
|
|
|
|
usersub = None
|
2012-08-17 23:29:17 -07:00
|
|
|
message = None
|
2012-08-24 14:34:53 -07:00
|
|
|
user = None
|
2012-08-17 23:29:17 -07:00
|
|
|
|
|
|
|
if not story_url:
|
|
|
|
code = -1
|
|
|
|
else:
|
|
|
|
try:
|
2012-08-25 19:19:44 -07:00
|
|
|
user_profile = Profile.objects.get(secret_token=token)
|
|
|
|
user = user_profile.user
|
2012-08-17 23:29:17 -07:00
|
|
|
except Profile.DoesNotExist:
|
|
|
|
code = -1
|
|
|
|
|
2012-08-24 14:34:53 -07:00
|
|
|
feed = Feed.get_feed_from_url(rss_url, create=False, fetch=False)
|
|
|
|
if not feed:
|
2012-08-27 10:26:49 -07:00
|
|
|
feed = Feed.get_feed_from_url(story_url, create=False, fetch=False)
|
|
|
|
if not feed:
|
|
|
|
parsed_url = urlparse.urlparse(story_url)
|
|
|
|
base_url = "%s://%s%s" % (parsed_url.scheme, parsed_url.hostname, parsed_url.path)
|
|
|
|
feed = Feed.get_feed_from_url(base_url, create=False, fetch=False)
|
|
|
|
if not feed:
|
|
|
|
feed = Feed.get_feed_from_url(base_url+'/', create=False, fetch=False)
|
2012-08-24 14:34:53 -07:00
|
|
|
|
|
|
|
if feed and user:
|
2012-08-25 19:19:44 -07:00
|
|
|
try:
|
|
|
|
usersub = UserSubscription.objects.filter(user=user, feed=feed)
|
|
|
|
except UserSubscription.DoesNotExist:
|
|
|
|
usersub = None
|
|
|
|
feed_id = feed and feed.pk
|
|
|
|
your_story, same_stories, other_stories = MSharedStory.get_shared_stories_from_site(feed_id,
|
|
|
|
user_id=user_profile.user.pk, story_url=story_url)
|
|
|
|
previous_stories = MSharedStory.objects.filter(user_id=user_profile.user.pk).order_by('-shared_date').limit(3)
|
|
|
|
previous_stories = [{
|
|
|
|
"user_id": story.user_id,
|
|
|
|
"story_title": story.story_title,
|
|
|
|
"comments": story.comments,
|
|
|
|
"shared_date": story.shared_date,
|
|
|
|
"relative_date": relative_timesince(story.shared_date),
|
|
|
|
"blurblog_permalink": story.blurblog_permalink(),
|
|
|
|
} for story in previous_stories]
|
|
|
|
|
|
|
|
user_ids = set([user_profile.user.pk])
|
|
|
|
for story in same_stories:
|
|
|
|
user_ids.add(story['user_id'])
|
|
|
|
for story in other_stories:
|
|
|
|
user_ids.add(story['user_id'])
|
|
|
|
|
|
|
|
users = {}
|
|
|
|
profiles = MSocialProfile.profiles(user_ids)
|
|
|
|
for profile in profiles:
|
|
|
|
users[profile.user_id] = {
|
|
|
|
"username": profile.username,
|
|
|
|
"photo_url": profile.photo_url,
|
|
|
|
}
|
2012-08-24 14:34:53 -07:00
|
|
|
|
2012-08-25 19:19:44 -07:00
|
|
|
logging.user(user_profile.user, "~BM~FCChecking share from site: ~SB%s" % (story_url),
|
2012-08-24 14:34:53 -07:00
|
|
|
request=request)
|
|
|
|
|
|
|
|
response = HttpResponse(callback + '(' + json.encode({
|
2012-08-25 19:19:44 -07:00
|
|
|
'code' : code,
|
|
|
|
'message' : message,
|
|
|
|
'feed' : feed,
|
|
|
|
'subscribed' : bool(usersub),
|
|
|
|
'your_story' : your_story,
|
|
|
|
'same_stories' : same_stories,
|
|
|
|
'other_stories' : other_stories,
|
|
|
|
'previous_stories' : previous_stories,
|
|
|
|
'users' : users,
|
2012-08-17 23:29:17 -07:00
|
|
|
}) + ')', mimetype='text/plain')
|
2012-08-24 14:34:53 -07:00
|
|
|
response['Access-Control-Allow-Origin'] = '*'
|
|
|
|
response['Access-Control-Allow-Methods'] = 'GET'
|
|
|
|
|
|
|
|
return response
|
2012-08-17 23:29:17 -07:00
|
|
|
|
|
|
|
def share_story(request, token):
|
2012-08-24 15:07:10 -07:00
|
|
|
code = 0
|
|
|
|
story_url = request.POST['story_url']
|
|
|
|
comments = request.POST['comments']
|
|
|
|
title = request.POST['title']
|
|
|
|
content = request.POST['content']
|
|
|
|
rss_url = request.POST.get('rss_url')
|
2012-08-25 20:46:48 -07:00
|
|
|
feed_id = request.POST.get('feed_id') or 0
|
2012-08-24 18:07:44 -07:00
|
|
|
feed = None
|
2012-08-24 15:07:10 -07:00
|
|
|
message = None
|
2012-08-17 23:29:17 -07:00
|
|
|
|
|
|
|
if not story_url:
|
|
|
|
code = -1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
profile = Profile.objects.get(secret_token=token)
|
|
|
|
except Profile.DoesNotExist:
|
|
|
|
code = -1
|
2012-08-24 15:07:10 -07:00
|
|
|
|
|
|
|
if feed_id:
|
|
|
|
feed = Feed.objects.get(pk=feed_id)
|
|
|
|
else:
|
2012-08-24 18:07:44 -07:00
|
|
|
if rss_url:
|
|
|
|
feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True)
|
2012-08-24 15:07:10 -07:00
|
|
|
if not feed:
|
|
|
|
feed = Feed.get_feed_from_url(story_url, create=True, fetch=True)
|
|
|
|
if feed:
|
|
|
|
feed_id = feed.pk
|
|
|
|
|
|
|
|
shared_story = MSharedStory.objects.filter(user_id=profile.user.pk,
|
|
|
|
story_feed_id=feed_id,
|
|
|
|
story_guid=story_url).limit(1).first()
|
|
|
|
if not shared_story:
|
|
|
|
story_db = {
|
|
|
|
"story_guid": story_url,
|
2012-08-24 18:07:44 -07:00
|
|
|
"story_permalink": story_url,
|
2012-08-24 15:07:10 -07:00
|
|
|
"story_title": title,
|
|
|
|
"story_feed_id": feed_id,
|
|
|
|
"story_content": content,
|
2012-08-25 19:58:03 -07:00
|
|
|
"story_date": datetime.datetime.now(),
|
2012-08-17 23:29:17 -07:00
|
|
|
|
2012-08-24 15:07:10 -07:00
|
|
|
"user_id": profile.user.pk,
|
|
|
|
"comments": comments,
|
|
|
|
"has_comments": bool(comments),
|
|
|
|
}
|
|
|
|
shared_story = MSharedStory.objects.create(**story_db)
|
|
|
|
socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk)
|
|
|
|
for socialsub in socialsubs:
|
|
|
|
socialsub.needs_unread_recalc = True
|
|
|
|
socialsub.save()
|
|
|
|
logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments))
|
|
|
|
else:
|
2012-08-24 18:07:44 -07:00
|
|
|
shared_story.story_content = content
|
|
|
|
shared_story.story_title = title
|
2012-08-24 15:07:10 -07:00
|
|
|
shared_story.comments = comments
|
2012-08-24 18:07:44 -07:00
|
|
|
shared_story.story_permalink = story_url
|
|
|
|
shared_story.story_guid = story_url
|
2012-08-24 15:07:10 -07:00
|
|
|
shared_story.has_comments = bool(comments)
|
2012-08-25 20:46:48 -07:00
|
|
|
shared_story.story_feed_id = feed_id
|
2012-08-24 15:07:10 -07:00
|
|
|
shared_story.save()
|
|
|
|
logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments))
|
|
|
|
|
|
|
|
shared_story.publish_update_to_subscribers()
|
2012-08-17 23:29:17 -07:00
|
|
|
|
2012-08-24 14:34:53 -07:00
|
|
|
response = HttpResponse(json.encode({
|
2012-08-17 23:29:17 -07:00
|
|
|
'code': code,
|
|
|
|
'message': message,
|
|
|
|
'story': None,
|
2012-08-24 14:34:53 -07:00
|
|
|
}), mimetype='text/plain')
|
|
|
|
response['Access-Control-Allow-Origin'] = '*'
|
|
|
|
response['Access-Control-Allow-Methods'] = 'POST'
|
|
|
|
|
|
|
|
return response
|