Skip to content

Commit

Permalink
move categories into own database
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoegl committed Apr 20, 2013
1 parent 00bcc44 commit 11d7234
Show file tree
Hide file tree
Showing 106 changed files with 120 additions and 49 deletions.
File renamed without changes.
9 changes: 9 additions & 0 deletions couchdb/general/_design/generic/filters/is_type.js
@@ -0,0 +1,9 @@
function(doc, req)
{
if(doc._deleted)
{
return false;
}

return (doc.doc_type && doc.doc_type == req.query.doc_type);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
79 changes: 49 additions & 30 deletions doc/dev/couchdb-views.rst
Expand Up @@ -5,20 +5,19 @@ CouchDB Views
This page describes the views that will be used in the CouchDB based backend of
the gpodder.net webservice.

The views are separated into groups, based on the databases they are indexed
on.

Categories
----------

Doc-Types: Category

**Views**
General
-------

* categories/by_tags
* categories/by_update
This group of views is available on the general database, called ``mygpo`` by
default.


Chapters
--------
^^^^^^^^

Doc-Types: EpisodeUserState

Expand All @@ -28,7 +27,7 @@ Doc-Types: EpisodeUserState


Clients
-------
^^^^^^^

Doc-Types: User

Expand All @@ -38,7 +37,7 @@ Doc-Types: User


Episode Actions
---------------
^^^^^^^^^^^^^^^

Doc-Types: EpisodeUserState

Expand All @@ -51,7 +50,7 @@ Doc-Types: EpisodeUserState


Episode States
--------------
^^^^^^^^^^^^^^

Doc-Types: EpisodeUserState

Expand All @@ -64,7 +63,7 @@ Doc-Types: EpisodeUserState


Episode Statistics
------------------
^^^^^^^^^^^^^^^^^^

Doc-Types: Episode

Expand All @@ -74,7 +73,7 @@ Doc-Types: Episode


Episodes
--------
^^^^^^^^

Doc-Types: Episode

Expand All @@ -89,7 +88,8 @@ Doc-Types: Episode


Favorites
---------
^^^^^^^^^

Doc-Types: EpisodeUserState

**Views**
Expand All @@ -98,7 +98,7 @@ Doc-Types: EpisodeUserState


Heatmap
-------
^^^^^^^

Doc-Types: EpisodeUserState

Expand All @@ -108,7 +108,7 @@ Doc-Types: EpisodeUserState


History
-------
^^^^^^^

Doc-Types: EpisodeUserState, PodcastUserState

Expand All @@ -119,7 +119,7 @@ Doc-Types: EpisodeUserState, PodcastUserState


Listeners
---------
^^^^^^^^^

Doc-Types: EpisodeUserState

Expand All @@ -134,7 +134,7 @@ Doc-Types: EpisodeUserState


Podcast Lists
-------------
^^^^^^^^^^^^^

Doc-Types: PodcastList

Expand All @@ -146,7 +146,7 @@ Doc-Types: PodcastList


Podcast States
--------------
^^^^^^^^^^^^^^

Doc-Types: PodcastUserState

Expand All @@ -158,7 +158,7 @@ Doc-Types: PodcastUserState


Podcasts
--------
^^^^^^^^

Doc-Types: Podcast, PodcastGroup, PodcastSubscriberData

Expand All @@ -179,7 +179,7 @@ Doc-Types: Podcast, PodcastGroup, PodcastSubscriberData


Sanitizing Rules
----------------
^^^^^^^^^^^^^^^^

Doc-Types: SanitizingRule

Expand All @@ -190,7 +190,7 @@ Doc-Types: SanitizingRule


Slugs
-----
^^^^^

Doc-Types: Podcast, PodcastGroup, Episode

Expand All @@ -200,7 +200,7 @@ Doc-Types: Podcast, PodcastGroup, Episode


Subscribers
-----------
^^^^^^^^^^^

Doc-Types: PodcastUserState

Expand All @@ -210,7 +210,7 @@ Doc-Types: PodcastUserState


Subscriptions
-------------
^^^^^^^^^^^^^

Doc-Types: PodcastUserState

Expand All @@ -222,7 +222,7 @@ Doc-Types: PodcastUserState


Suggestions
-----------
^^^^^^^^^^^

Doc-Types: Suggestions

Expand All @@ -232,7 +232,7 @@ Doc-Types: Suggestions


Tags
----
^^^^

Doc-Types: Podcast, PodcastGroup

Expand All @@ -243,7 +243,7 @@ Doc-Types: Podcast, PodcastGroup


Toplists
--------
^^^^^^^^

Doc-Types: Episode, Podcast, PodcastGroup

Expand All @@ -254,7 +254,7 @@ Doc-Types: Episode, Podcast, PodcastGroup


Trending
--------
^^^^^^^^

Doc-Types: Podcast, PodcastGroup

Expand All @@ -264,7 +264,7 @@ Doc-Types: Podcast, PodcastGroup


Users
-----
^^^^^

Doc-Types: User

Expand All @@ -275,9 +275,28 @@ Doc-Types: User


User-Tags
---------
^^^^^^^^^

Doc-Types: PodcastUserState

* usertags/by_podcast
* usertags/podcasts



Categories
----------

This group of views is available on the categories database, called
``mygpo_categories`` by default.


Categories
^^^^^^^^^^

Doc-Types: Category

**Views**

* categories/by_tags
* categories/by_update
5 changes: 5 additions & 0 deletions mygpo/db/couchdb/__init__.py
Expand Up @@ -10,6 +10,11 @@ def get_main_database():
return loading.get_db('core')


def get_categories_database():
""" returns the database that contains Category documents """
return loading.get_db('categories')


def get_database(user=None):
return get_main_database()

Expand Down
18 changes: 13 additions & 5 deletions mygpo/db/couchdb/directory.py
Expand Up @@ -2,7 +2,7 @@
from operator import itemgetter

from mygpo.directory.models import Category
from mygpo.db.couchdb import get_main_database
from mygpo.db.couchdb import get_main_database, get_categories_database
from mygpo.cache import cache_result
from mygpo.db.couchdb.utils import multi_request_view
from mygpo.db import QueryParameterMissing
Expand All @@ -14,10 +14,12 @@ def category_for_tag(tag):
if not tag:
raise QueryParameterMissing('tag')

r = Category.view('categories/by_tags',
db = get_categories_database()
r = db.view('categories/by_tags',
key = tag,
include_docs = True,
stale = 'update_after',
schema = Category
)
return r.first() if r else None

Expand All @@ -31,18 +33,19 @@ def top_categories(offset, count, with_podcasts=False):
if not count:
raise QueryParameterMissing('count')

db = get_categories_database()

if with_podcasts:
r = Category.view('categories/by_update',
r = db.view('categories/by_update',
descending = True,
skip = offset,
limit = count,
include_docs = True,
stale = 'update_after'
stale = 'update_after',
schema = Category,
)

else:
db = get_main_database()
r = db.view('categories/by_update',
descending = True,
skip = offset,
Expand All @@ -61,6 +64,11 @@ def _category_wrapper(r):
return c


def save_category(category):
db = get_categories_database()
db.save_doc(category)


def tags_for_podcast(podcast):
""" all tags for the podcast, in decreasing order of importance """

Expand Down
13 changes: 9 additions & 4 deletions mygpo/db/couchdb/management/commands/sync-design-docs.py
Expand Up @@ -2,7 +2,11 @@

from django.conf import settings
from django.core.management.base import BaseCommand

from couchdbkit import Database
from couchdbkit.loaders import FileSystemDocsLoader
from couchdbkit.ext.django import loading
from restkit import BasicAuth

from mygpo.db.couchdb import get_main_database

Expand All @@ -13,7 +17,8 @@ class Command(BaseCommand):

def handle(self, *args, **options):

path = os.path.join(settings.BASE_DIR, '..', 'couchdb', '_design')
db = get_main_database()
loader = FileSystemDocsLoader(path)
loader.sync(db, verbose=True)
for part, label in settings.COUCHDB_DDOC_MAPPING.items():
path = os.path.join(settings.BASE_DIR, '..', 'couchdb', part, '_design')
db = loading.get_db(label)
loader = FileSystemDocsLoader(path)
loader.sync(db, verbose=True)
Expand Up @@ -3,7 +3,7 @@
from django.core.management.base import BaseCommand

from mygpo.directory.models import Category
from mygpo.db.couchdb.directory import category_for_tag
from mygpo.db.couchdb.directory import category_for_tag, save_category


class Command(BaseCommand):
Expand Down Expand Up @@ -54,4 +54,5 @@ def handle(self, *args, **options):
new_cat.delete()

category.updated = start_time
category.save()

save_category(category)
6 changes: 0 additions & 6 deletions mygpo/directory/models.py
Expand Up @@ -61,12 +61,6 @@ def get_podcasts(self, start=0, end=10):
return podcasts



def save(self, *args, **kwargs):
self.podcasts = sorted(self.podcasts, reverse=True)
super(Category, self).save(*args, **kwargs)


def get_weight(self):
return getattr(self, '_weight', len(self.podcasts))

Expand Down
6 changes: 4 additions & 2 deletions mygpo/directory/tags.py
Expand Up @@ -9,7 +9,8 @@
from mygpo.core.proxy import proxy_object
from mygpo.directory.models import Category
from mygpo.db.couchdb.podcast import podcasts_for_tag
from mygpo.db.couchdb.directory import top_categories, category_for_tag
from mygpo.db.couchdb.directory import top_categories, category_for_tag, \
save_category


class Tag(object):
Expand Down Expand Up @@ -110,4 +111,5 @@ def update_category(podcast):

category.podcasts.insert(0, podcast.get_id())
category.label = category.label.strip()
category.save()

save_category(category)

0 comments on commit 11d7234

Please sign in to comment.