2010-08-16 12:55:45 -04:00
|
|
|
from utils import log as logging
|
2010-08-18 20:35:45 -04:00
|
|
|
from django.shortcuts import get_object_or_404
|
2010-01-05 16:07:11 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2012-09-07 19:14:01 -07:00
|
|
|
# from mongoengine.queryset import OperationError
|
2010-08-22 18:34:40 -04:00
|
|
|
from apps.rss_feeds.models import Feed
|
2010-06-27 21:55:11 -04:00
|
|
|
from apps.reader.models import UserSubscription
|
2010-08-22 18:34:40 -04:00
|
|
|
from apps.analyzer.models import MClassifierTitle, MClassifierAuthor, MClassifierFeed, MClassifierTag
|
|
|
|
from apps.analyzer.models import get_classifiers_for_user
|
2012-02-14 10:34:10 -08:00
|
|
|
from apps.social.models import MSocialSubscription
|
2010-10-23 13:06:28 -04:00
|
|
|
from utils import json_functions as json
|
2010-01-05 16:07:11 +00:00
|
|
|
from utils.user_functions import get_user
|
2010-07-24 18:22:23 -04:00
|
|
|
from utils.user_functions import ajax_login_required
|
2010-01-05 16:07:11 +00:00
|
|
|
|
|
|
|
def index(requst):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@require_POST
|
2010-07-24 18:22:23 -04:00
|
|
|
@ajax_login_required
|
2010-01-21 13:12:29 -05:00
|
|
|
@json.json_view
|
2010-04-03 20:32:40 -04:00
|
|
|
def save_classifier(request):
|
2010-01-12 01:19:37 +00:00
|
|
|
post = request.POST
|
2012-02-14 10:34:10 -08:00
|
|
|
feed_id = post['feed_id']
|
|
|
|
feed = None
|
|
|
|
social_user_id = None
|
|
|
|
if feed_id.startswith('social:'):
|
|
|
|
social_user_id = int(feed_id.replace('social:', ''))
|
|
|
|
feed_id = None
|
|
|
|
else:
|
|
|
|
feed_id = int(feed_id)
|
|
|
|
feed = get_object_or_404(Feed, pk=feed_id)
|
2010-01-12 01:19:37 +00:00
|
|
|
code = 0
|
|
|
|
message = 'OK'
|
|
|
|
payload = {}
|
|
|
|
|
2011-09-16 09:26:22 -07:00
|
|
|
logging.user(request, "~FGSaving classifier: ~SB%s~SN ~FW%s" % (feed, post))
|
2011-03-17 22:17:31 -04:00
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
# Mark subscription as dirty, so unread counts can be recalculated
|
2012-02-14 10:34:10 -08:00
|
|
|
usersub = None
|
|
|
|
socialsub = None
|
|
|
|
if social_user_id:
|
|
|
|
socialsub = MSocialSubscription.objects.get(user_id=request.user.pk, subscription_user_id=social_user_id)
|
|
|
|
if not socialsub.needs_unread_recalc:
|
|
|
|
socialsub.needs_unread_recalc = True
|
|
|
|
socialsub.save()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
usersub = UserSubscription.objects.get(user=request.user, feed=feed)
|
|
|
|
except UserSubscription.DoesNotExist:
|
|
|
|
pass
|
|
|
|
if usersub and (not usersub.needs_unread_recalc or not usersub.is_trained):
|
|
|
|
usersub.needs_unread_recalc = True
|
|
|
|
usersub.is_trained = True
|
|
|
|
usersub.save()
|
2010-04-09 11:59:33 -04:00
|
|
|
|
2010-08-01 19:12:42 -04:00
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
def _save_classifier(ClassifierCls, content_type):
|
2010-04-03 23:16:38 -04:00
|
|
|
classifiers = {
|
|
|
|
'like_'+content_type: 1,
|
|
|
|
'dislike_'+content_type: -1,
|
|
|
|
'remove_like_'+content_type: 0,
|
|
|
|
'remove_dislike_'+content_type: 0,
|
|
|
|
}
|
|
|
|
for opinion, score in classifiers.items():
|
2010-03-28 14:50:41 -04:00
|
|
|
if opinion in post:
|
|
|
|
post_contents = post.getlist(opinion)
|
|
|
|
for post_content in post_contents:
|
2010-11-09 22:06:08 -05:00
|
|
|
if not post_content: continue
|
2010-03-28 14:50:41 -04:00
|
|
|
classifier_dict = {
|
2012-01-26 09:54:48 -08:00
|
|
|
'user_id': request.user.pk,
|
2012-02-14 10:34:10 -08:00
|
|
|
'feed_id': feed_id or 0,
|
|
|
|
'social_user_id': social_user_id or 0,
|
2010-03-28 14:50:41 -04:00
|
|
|
'defaults': {
|
|
|
|
'score': score
|
|
|
|
}
|
|
|
|
}
|
2010-08-22 18:34:40 -04:00
|
|
|
if content_type in ('author', 'tag', 'title'):
|
2010-03-28 14:50:41 -04:00
|
|
|
classifier_dict.update({content_type: post_content})
|
2012-02-14 10:34:10 -08:00
|
|
|
if content_type == 'feed':
|
|
|
|
if not post_content.startswith('social:'):
|
|
|
|
classifier_dict['feed_id'] = post_content
|
|
|
|
# try:
|
|
|
|
classifier, created = ClassifierCls.objects.get_or_create(**classifier_dict)
|
|
|
|
# except OperationError:
|
|
|
|
# continue
|
2010-07-20 20:23:49 -04:00
|
|
|
if score == 0:
|
|
|
|
classifier.delete()
|
|
|
|
elif classifier.score != score:
|
2010-04-03 23:16:38 -04:00
|
|
|
if score == 0:
|
|
|
|
if ((classifier.score == 1 and opinion.startswith('remove_like'))
|
|
|
|
or (classifier.score == -1 and opinion.startswith('remove_dislike'))):
|
|
|
|
classifier.delete()
|
|
|
|
else:
|
|
|
|
classifier.score = score
|
|
|
|
classifier.save()
|
2010-03-28 14:50:41 -04:00
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
_save_classifier(MClassifierAuthor, 'author')
|
|
|
|
_save_classifier(MClassifierTag, 'tag')
|
|
|
|
_save_classifier(MClassifierTitle, 'title')
|
2010-10-21 13:37:36 -04:00
|
|
|
_save_classifier(MClassifierFeed, 'feed')
|
2010-08-02 23:09:47 -04:00
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
response = dict(code=code, message=message, payload=payload)
|
2010-03-16 23:18:54 -04:00
|
|
|
return response
|
|
|
|
|
|
|
|
@json.json_view
|
2011-04-24 01:52:44 -04:00
|
|
|
def get_classifiers_feed(request, feed_id):
|
2010-03-16 23:18:54 -04:00
|
|
|
user = get_user(request)
|
|
|
|
code = 0
|
|
|
|
|
2012-02-15 18:00:10 -08:00
|
|
|
payload = get_classifiers_for_user(user, feed_id=feed_id)
|
2010-03-16 23:18:54 -04:00
|
|
|
|
|
|
|
response = dict(code=code, payload=payload)
|
|
|
|
|
2010-08-01 19:12:42 -04:00
|
|
|
return response
|
|
|
|
|