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
|
2010-08-21 23:49:36 -04:00
|
|
|
from apps.rss_feeds.models import Feed, StoryAuthor, Tag
|
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
|
|
|
|
2010-01-07 00:04:12 +00:00
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
class ClassifierTitle(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
2010-01-07 00:04:12 +00:00
|
|
|
score = models.SmallIntegerField()
|
2010-01-05 16:07:11 +00:00
|
|
|
title = models.CharField(max_length=255)
|
|
|
|
feed = models.ForeignKey(Feed)
|
2010-06-27 21:55:11 -04:00
|
|
|
# original_story = models.ForeignKey(Story, null=True)
|
2010-01-05 16:07:11 +00:00
|
|
|
creation_date = models.DateTimeField(auto_now=True)
|
|
|
|
|
2010-04-03 17:59:00 -04:00
|
|
|
unique_together = (('user', 'feed', 'title'),)
|
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return '%s: %s (%s)' % (self.user, self.title, self.feed)
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
class MClassifierTitle(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
|
|
|
feed_id = mongo.IntField()
|
|
|
|
title = mongo.StringField(max_length=255)
|
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_title',
|
2010-09-07 17:55:40 -07:00
|
|
|
'indexes': ['feed_id', 'user_id', ('user_id', 'feed_id')],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
class ClassifierAuthor(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
2010-01-07 00:04:12 +00:00
|
|
|
score = models.SmallIntegerField()
|
2010-01-05 16:07:11 +00:00
|
|
|
author = models.ForeignKey(StoryAuthor)
|
|
|
|
feed = models.ForeignKey(Feed)
|
2010-06-27 21:55:11 -04:00
|
|
|
# original_story = models.ForeignKey(Story, null=True)
|
2010-01-05 16:07:11 +00:00
|
|
|
creation_date = models.DateTimeField(auto_now=True)
|
|
|
|
|
2010-04-03 17:59:00 -04:00
|
|
|
unique_together = (('user', 'feed', 'author'),)
|
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return '%s: %s (%s)' % (self.user, self.author.author_name, self.feed)
|
2010-01-21 13:12:29 -05:00
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
class MClassifierAuthor(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
|
|
|
feed_id = mongo.IntField()
|
|
|
|
author = mongo.StringField(max_length=255, unique_with=('user_id', 'feed_id'))
|
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_author',
|
2010-09-07 17:55:40 -07:00
|
|
|
'indexes': ['feed_id', 'user_id', ('user_id', 'feed_id')],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
2010-01-21 13:12:29 -05:00
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
class ClassifierFeed(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
2010-01-07 00:04:12 +00:00
|
|
|
score = models.SmallIntegerField()
|
2010-01-05 16:07:11 +00:00
|
|
|
feed = models.ForeignKey(Feed)
|
2010-06-27 21:55:11 -04:00
|
|
|
# original_story = models.ForeignKey(Story, null=True)
|
2010-01-05 16:07:11 +00:00
|
|
|
creation_date = models.DateTimeField(auto_now=True)
|
|
|
|
|
2010-04-03 17:59:00 -04:00
|
|
|
unique_together = (('user', 'feed'),)
|
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return '%s: %s' % (self.user, self.feed)
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
class MClassifierFeed(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
|
|
|
feed_id = mongo.IntField(unique_with='user_id')
|
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_feed',
|
2010-09-07 17:55:40 -07:00
|
|
|
'indexes': ['feed_id', 'user_id', ('user_id', 'feed_id')],
|
2010-08-22 18:34:40 -04:00
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
2010-01-21 13:12:29 -05:00
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
class ClassifierTag(models.Model):
|
|
|
|
user = models.ForeignKey(User)
|
2010-01-07 00:04:12 +00:00
|
|
|
score = models.SmallIntegerField()
|
2010-01-05 16:07:11 +00:00
|
|
|
tag = models.ForeignKey(Tag)
|
|
|
|
feed = models.ForeignKey(Feed)
|
2010-06-27 21:55:11 -04:00
|
|
|
# original_story = models.ForeignKey(Story, null=True)
|
2010-01-05 16:07:11 +00:00
|
|
|
creation_date = models.DateTimeField(auto_now=True)
|
|
|
|
|
2010-04-03 17:59:00 -04:00
|
|
|
unique_together = (('user', 'feed', 'tag'),)
|
|
|
|
|
2010-01-05 16:07:11 +00:00
|
|
|
def __unicode__(self):
|
2010-01-21 13:12:29 -05:00
|
|
|
return '%s: %s (%s)' % (self.user, self.tag.name, self.feed)
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
class MClassifierTag(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
|
|
|
feed_id = mongo.IntField()
|
|
|
|
tag = mongo.StringField(max_length=255, unique_with=('user_id', 'feed_id'))
|
|
|
|
score = mongo.IntField()
|
|
|
|
creation_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'classifier_tag',
|
2010-09-07 17:55:40 -07:00
|
|
|
'indexes': ['feed_id', 'user_id', ('user_id', 'feed_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):
|
|
|
|
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'])
|
|
|
|
return classifier.score
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def apply_classifier_feeds(classifiers, feed):
|
2011-01-07 19:06:36 -05:00
|
|
|
feed_id = feed if isinstance(feed, int) else feed.pk
|
2010-01-21 13:12:29 -05:00
|
|
|
for classifier in classifiers:
|
2011-01-07 19:06:36 -05:00
|
|
|
if classifier.feed_id == feed_id:
|
2010-08-22 18:34:40 -04:00
|
|
|
# print 'Feeds: %s -- %s' % (classifier.feed_id, feed.pk)
|
2010-01-21 13:12:29 -05:00
|
|
|
return classifier.score
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def apply_classifier_authors(classifiers, story):
|
|
|
|
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'])
|
2010-01-21 13:12:29 -05:00
|
|
|
return classifier.score
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def apply_classifier_tags(classifiers, story):
|
|
|
|
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'])
|
2010-01-21 13:12:29 -05:00
|
|
|
return classifier.score
|
2010-03-23 20:03:40 -04:00
|
|
|
return 0
|
|
|
|
|
2010-08-22 18:34:40 -04:00
|
|
|
def get_classifiers_for_user(user, feed_id, classifier_feeds=None, classifier_authors=None, classifier_titles=None, classifier_tags=None):
|
|
|
|
if classifier_feeds is None:
|
|
|
|
classifier_feeds = MClassifierFeed.objects(user_id=user.pk, feed_id=feed_id)
|
2010-10-27 19:22:38 -04:00
|
|
|
else: classifier_feeds.rewind()
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_authors is None:
|
|
|
|
classifier_authors = MClassifierAuthor.objects(user_id=user.pk, feed_id=feed_id)
|
2010-10-27 19:22:38 -04:00
|
|
|
else: classifier_authors.rewind()
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_titles is None:
|
|
|
|
classifier_titles = MClassifierTitle.objects(user_id=user.pk, feed_id=feed_id)
|
2010-10-27 19:22:38 -04:00
|
|
|
else: classifier_titles.rewind()
|
2010-08-22 18:34:40 -04:00
|
|
|
if classifier_tags is None:
|
|
|
|
classifier_tags = MClassifierTag.objects(user_id=user.pk, feed_id=feed_id)
|
2010-10-27 19:22:38 -04:00
|
|
|
else: classifier_tags.rewind()
|
|
|
|
|
2010-03-23 20:03:40 -04:00
|
|
|
payload = {
|
2010-08-22 18:34:40 -04:00
|
|
|
'feeds': dict([(f.feed_id, f.score) for f in classifier_feeds]),
|
|
|
|
'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
|