2010-08-22 18:34:40 -04:00
|
|
|
import mongoengine as mongo
|
2009-06-16 03:08:55 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
2011-01-17 23:24:30 -05:00
|
|
|
from apps.rss_feeds.models import Feed
|
2009-11-03 03:52:03 +00:00
|
|
|
|
|
|
|
class FeatureCategory(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
|
|
|
feed = models.ForeignKey(Feed)
|
|
|
|
feature = models.CharField(max_length=255)
|
|
|
|
category = models.CharField(max_length=255)
|
|
|
|
count = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return '%s - %s (%s)' % (self.feature, self.category, self.count)
|
|
|
|
|
|
|
|
class Category(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
|
|
|
feed = models.ForeignKey(Feed)
|
|
|
|
category = models.CharField(max_length=255)
|
|
|
|
count = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return '%s (%s)' % (self.category, self.count)
|
2010-01-05 16:07:11 +00:00
|
|
|
|
2011-01-17 23:24:30 -05:00
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
class MClassifierTitle(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
2012-02-15 18:00:10 -08:00
|
|
|
feed_id = mongo.IntField(default=0)
|
|
|
|
social_user_id = mongo.IntField(default=0)
|
2010-08-22 18:34:40 -04:00
|
|
|
title = mongo.StringField(max_length=255)
|
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_title',
|
2012-02-08 12:20:05 -08:00
|
|
|
'indexes': [('user_id', 'feed_id'), 'feed_id', ('user_id', 'social_user_id'), 'social_user_id'],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
|
|
|
class MClassifierAuthor(mongo.Document):
|
2012-02-14 10:34:10 -08:00
|
|
|
user_id = mongo.IntField(unique_with=('feed_id', 'social_user_id', 'author'))
|
2012-02-15 18:00:10 -08:00
|
|
|
feed_id = mongo.IntField(default=0)
|
|
|
|
social_user_id = mongo.IntField(default=0)
|
2012-02-14 10:34:10 -08:00
|
|
|
author = mongo.StringField(max_length=255)
|
2010-08-22 18:34:40 -04:00
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_author',
|
2012-02-08 12:20:05 -08:00
|
|
|
'indexes': [('user_id', 'feed_id'), 'feed_id', ('user_id', 'social_user_id'), 'social_user_id'],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
2010-01-21 13:12:29 -05:00
|
|
|
|
2012-02-14 10:34:10 -08:00
|
|
|
|
|
|
|
class MClassifierTag(mongo.Document):
|
|
|
|
user_id = mongo.IntField(unique_with=('feed_id', 'social_user_id', 'tag'))
|
2012-02-15 18:00:10 -08:00
|
|
|
feed_id = mongo.IntField(default=0)
|
|
|
|
social_user_id = mongo.IntField(default=0)
|
2012-02-14 10:34:10 -08:00
|
|
|
tag = mongo.StringField(max_length=255)
|
2010-08-22 18:34:40 -04:00
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
2012-02-14 10:34:10 -08:00
|
|
|
'collection': 'classifier_tag',
|
2012-02-08 12:20:05 -08:00
|
|
|
'indexes': [('user_id', 'feed_id'), 'feed_id', ('user_id', 'social_user_id'), 'social_user_id'],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
2012-02-14 10:34:10 -08:00
|
|
|
|
|
|
|
class MClassifierFeed(mongo.Document):
|
|
|
|
user_id = mongo.IntField(unique_with=('feed_id', 'social_user_id'))
|
2012-02-15 18:00:10 -08:00
|
|
|
feed_id = mongo.IntField(default=0)
|
|
|
|
social_user_id = mongo.IntField(default=0)
|
2010-08-22 18:34:40 -04:00
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
2012-02-14 10:34:10 -08:00
|
|
|
'collection': 'classifier_feed',
|
2012-02-08 12:20:05 -08:00
|
|
|
'indexes': [('user_id', 'feed_id'), 'feed_id', ('user_id', 'social_user_id'), 'social_user_id'],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-21 13:12:29 -05:00
|
|
|
def apply_classifier_titles(classifiers, story):
|
2011-02-27 16:13:22 -05:00
|
|
|
score = 0
|
2010-01-21 13:12:29 -05:00
|
|
|
for classifier in classifiers:
|
2010-01-24 22:53:46 -05:00
|
|
|
if classifier.title.lower() in story['story_title'].lower():
|
2010-01-21 13:12:29 -05:00
|
|
|
# print 'Titles: (%s) %s -- %s' % (classifier.title in story['story_title'], classifier.title, story['story_title'])
|
2011-02-27 16:13:22 -05:00
|
|
|
score = classifier.score
|
|
|
|
if score > 0: return score
|
|
|
|
return score
|
2010-01-21 13:12:29 -05:00
|
|
|
|
|
|
|
def apply_classifier_authors(classifiers, story):
|
2011-02-27 16:13:22 -05:00
|
|
|
score = 0
|
2010-01-21 13:12:29 -05:00
|
|
|
for classifier in classifiers:
|
2010-08-22 18:34:40 -04:00
|
|
|
if story.get('story_authors') and classifier.author == story.get('story_authors'):
|
|
|
|
# print 'Authors: %s -- %s' % (classifier.author, story['story_authors'])
|
2011-02-27 16:13:22 -05:00
|
|
|
score = classifier.score
|
|
|
|
if score > 0: return classifier.score
|
|
|
|
return score
|
2010-01-21 13:12:29 -05:00
|
|
|
|
|
|
|
def apply_classifier_tags(classifiers, story):
|
2011-02-27 16:13:22 -05:00
|
|
|
score = 0
|
2010-01-21 13:12:29 -05:00
|
|
|
for classifier in classifiers:
|
2010-08-22 18:34:40 -04:00
|
|
|
if story['story_tags'] and classifier.tag in story['story_tags']:
|
|
|
|
# print 'Tags: (%s-%s) %s -- %s' % (classifier.tag in story['story_tags'], classifier.score, classifier.tag, story['story_tags'])
|
2011-02-27 16:13:22 -05:00
|
|
|
score = classifier.score
|
|
|
|
if score > 0: return classifier.score
|
|
|
|
return score
|
2010-03-23 20:03:40 -04:00
|
|
|
|
2012-02-15 18:00:10 -08:00
|
|
|
def apply_classifier_feeds(classifiers, feed, social_user_id=None):
|
|
|
|
feed_id = feed if isinstance(feed, int) else feed.pk
|
|
|
|
for classifier in classifiers:
|
|
|
|
if classifier.feed_id == feed_id:
|
|
|
|
# print 'Feeds: %s -- %s' % (classifier.feed_id, feed.pk)
|
|
|
|
return classifier.score
|
|
|
|
if social_user_id and not classifier.feed_id and social_user_id == classifier.social_user_id:
|
|
|
|
return classifier.score
|
|
|
|
return 0
|
|
|
|
|
2012-02-14 10:34:10 -08:00
|
|
|
def get_classifiers_for_user(user, feed_id=None, social_user_id=None, classifier_feeds=None, classifier_authors=None,
|
|
|
|
classifier_titles=None, classifier_tags=None):
|
|
|
|
params = dict(user_id=user.pk)
|
|
|
|
if isinstance(feed_id, int):
|
|
|
|
params['feed_id'] = feed_id
|
|
|
|
elif isinstance(feed_id, list):
|
|
|
|
params['feed_id__in'] = feed_id
|
|
|
|
if social_user_id:
|
|
|
|
params['social_user_id'] = int(social_user_id.replace('social:', ''))
|
|
|
|
else:
|
|
|
|
params['social_user_id'] = 0
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_feeds is None:
|
2012-02-14 10:34:10 -08:00
|
|
|
classifier_feeds = list(MClassifierFeed.objects(**params))
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_authors is None:
|
2012-02-14 10:34:10 -08:00
|
|
|
classifier_authors = list(MClassifierAuthor.objects(**params))
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_titles is None:
|
2012-02-14 10:34:10 -08:00
|
|
|
classifier_titles = list(MClassifierTitle.objects(**params))
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_tags is None:
|
2012-02-14 10:34:10 -08:00
|
|
|
classifier_tags = list(MClassifierTag.objects(**params))
|
|
|
|
|
|
|
|
feeds = []
|
|
|
|
for f in classifier_feeds:
|
|
|
|
if f.social_user_id and not f.feed_id:
|
|
|
|
feeds.append(('social:%s' % f.social_user_id, f.score))
|
|
|
|
else:
|
|
|
|
feeds.append((f.feed_id, f.score))
|
|
|
|
|
2010-03-23 20:03:40 -04:00
|
|
|
payload = {
|
2012-02-14 10:34:10 -08:00
|
|
|
'feeds': dict(feeds),
|
2010-08-22 18:34:40 -04:00
|
|
|
'authors': dict([(a.author, a.score) for a in classifier_authors]),
|
2010-03-23 20:03:40 -04:00
|
|
|
'titles': dict([(t.title, t.score) for t in classifier_titles]),
|
2010-08-22 18:34:40 -04:00
|
|
|
'tags': dict([(t.tag, t.score) for t in classifier_tags]),
|
2010-03-23 20:03:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return payload
|