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
|
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
|
2010-01-05 16:07:11 +00:00
|
|
|
from utils import json
|
|
|
|
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
|
2010-08-18 20:35:45 -04:00
|
|
|
logging.info(" ---> [%s] Saving classifier: %s" % (request.user, post))
|
2010-08-22 18:34:40 -04:00
|
|
|
feed_id = int(post['feed_id'])
|
|
|
|
feed = get_object_or_404(Feed, pk=feed_id)
|
2010-01-12 01:19:37 +00:00
|
|
|
code = 0
|
|
|
|
message = 'OK'
|
|
|
|
payload = {}
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
# Mark subscription as dirty, so unread counts can be recalculated
|
2010-04-09 11:59:33 -04:00
|
|
|
usersub = UserSubscription.objects.get(user=request.user, feed=feed)
|
2010-08-01 19:12:42 -04:00
|
|
|
if not usersub.needs_unread_recalc or not usersub.is_trained:
|
2010-04-09 11:59:33 -04:00
|
|
|
usersub.needs_unread_recalc = True
|
2010-08-01 19:12:42 -04:00
|
|
|
usersub.is_trained = True
|
2010-04-09 11:59:33 -04:00
|
|
|
usersub.save()
|
|
|
|
|
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:
|
|
|
|
classifier_dict = {
|
2010-08-22 18:34:40 -04:00
|
|
|
'user_id': request.user.pk,
|
|
|
|
'feed_id': feed_id,
|
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})
|
2010-04-03 00:59:03 -04:00
|
|
|
|
2010-04-03 20:32:40 -04:00
|
|
|
classifier, created = ClassifierCls.objects.get_or_create(**classifier_dict)
|
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-01-12 01:19:37 +00:00
|
|
|
|
2010-08-17 17:45:51 -04:00
|
|
|
logging.info(" ---> [%s] Feed training: %s" % (request.user, 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
|
|
|
|
def get_classifiers_feed(request):
|
2010-08-23 21:04:51 -04:00
|
|
|
feed_id = int(request.POST['feed_id'])
|
2010-03-16 23:18:54 -04:00
|
|
|
user = get_user(request)
|
|
|
|
code = 0
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
payload = get_classifiers_for_user(user, 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
|
|
|
|
|