NewsBlur-viq/apps/categories/models.py

139 lines
4.5 KiB
Python
Raw Permalink Normal View History

from itertools import groupby
2024-04-24 09:50:42 -04:00
import mongoengine as mongo
from apps.reader.models import UserSubscription, UserSubscriptionFolders
2024-04-24 09:50:42 -04:00
from apps.rss_feeds.models import Feed
from utils import json_functions as json
2021-03-25 16:07:52 -04:00
from utils import log as logging
2024-04-24 09:50:42 -04:00
from utils.feed_functions import add_object_to_folder
2024-04-24 09:43:56 -04:00
class MCategory(mongo.Document):
title = mongo.StringField()
description = mongo.StringField()
feed_ids = mongo.ListField(mongo.IntField())
2024-04-24 09:43:56 -04:00
meta = {
2024-04-24 09:43:56 -04:00
"collection": "category",
"indexes": ["title"],
"allow_inheritance": False,
}
2024-04-24 09:43:56 -04:00
def __str__(self):
return "%s: %s sites" % (self.title, len(self.feed_ids))
2024-04-24 09:43:56 -04:00
2021-03-25 16:07:52 -04:00
@classmethod
def audit(cls):
categories = cls.objects.all()
for category in categories:
logging.info(f" ---> Auditing category: {category} {category.feed_ids}")
keep_feed_ids = []
for feed_id in category.feed_ids:
feed = Feed.get_by_id(feed_id)
if feed:
2021-03-25 16:22:44 -04:00
logging.info(f" \t---> Keeping feed: {feed_id} {feed}")
2021-03-25 16:07:52 -04:00
keep_feed_ids.append(feed.pk)
else:
2021-03-25 16:22:44 -04:00
logging.info(f" \t***> Skipping missing feed: {feed_id}")
2021-03-25 16:07:52 -04:00
category.feed_ids = keep_feed_ids
category.save()
@classmethod
def add(cls, title, description):
return cls.objects.create(title=title, description=description)
2024-04-24 09:43:56 -04:00
@classmethod
def serialize(cls, category=None):
categories = cls.objects.all()
if category:
categories = categories.filter(title=category)
2024-04-24 09:43:56 -04:00
data = dict(categories=[], feeds={})
feed_ids = set()
for category in categories:
category_output = {
2024-04-24 09:43:56 -04:00
"title": category.title,
"description": category.description,
"feed_ids": category.feed_ids,
}
2024-04-24 09:43:56 -04:00
data["categories"].append(category_output)
feed_ids.update(list(category.feed_ids))
2024-04-24 09:43:56 -04:00
feeds = Feed.objects.filter(pk__in=feed_ids)
for feed in feeds:
2024-04-24 09:43:56 -04:00
data["feeds"][feed.pk] = feed.canonical()
return data
@classmethod
def reload_sites(cls, category_title=None):
category_sites = MCategorySite.objects.all()
if category_title:
category_sites = category_sites.filter(category_title=category_title)
2024-04-24 09:43:56 -04:00
category_groups = groupby(
sorted(category_sites, key=lambda c: c.category_title), key=lambda c: c.category_title
)
for category_title, sites in category_groups:
2013-05-24 15:41:32 -07:00
try:
category = cls.objects.get(title=category_title)
except cls.DoesNotExist as e:
print(" ***> Missing category: %s" % category_title)
2013-05-24 15:41:32 -07:00
continue
category.feed_ids = [site.feed_id for site in sites]
category.save()
print(" ---> Reloaded category: %s" % category)
2024-04-24 09:43:56 -04:00
@classmethod
def subscribe(cls, user_id, category_title):
category = cls.objects.get(title=category_title)
for feed_id in category.feed_ids:
us, _ = UserSubscription.objects.get_or_create(
2024-04-24 09:43:56 -04:00
feed_id=feed_id,
user_id=user_id,
defaults={
2024-04-24 09:43:56 -04:00
"needs_unread_recalc": True,
"active": True,
},
)
2024-04-24 09:43:56 -04:00
usf, created = UserSubscriptionFolders.objects.get_or_create(
2024-04-24 09:43:56 -04:00
user_id=user_id, defaults={"folders": "[]"}
)
2024-04-24 09:43:56 -04:00
usf.add_folder("", category.title)
folders = json.decode(usf.folders)
for feed_id in category.feed_ids:
feed = Feed.get_by_id(feed_id)
if not feed:
continue
folders = add_object_to_folder(feed.pk, category.title, folders)
usf.folders = json.encode(folders)
usf.save()
2024-04-24 09:43:56 -04:00
class MCategorySite(mongo.Document):
feed_id = mongo.IntField()
category_title = mongo.StringField()
2024-04-24 09:43:56 -04:00
meta = {
2024-04-24 09:43:56 -04:00
"collection": "category_site",
"indexes": ["feed_id", "category_title"],
"allow_inheritance": False,
}
2024-04-24 09:43:56 -04:00
def __str__(self):
feed = Feed.get_by_id(self.feed_id)
return "%s: %s" % (self.category_title, feed)
2024-04-24 09:43:56 -04:00
@classmethod
def add(cls, category_title, feed_id):
2024-04-24 09:43:56 -04:00
category_site, created = cls.objects.get_or_create(category_title=category_title, feed_id=feed_id)
if not created:
print(" ---> Site is already in category: %s" % category_site)
else:
2021-03-25 16:07:52 -04:00
MCategory.reload_sites(category_title)