FlightDeck/apps/jetpack/managers.py

93 строки
3.7 KiB
Python
Исходник Обычный вид История

"""
Managers for the Jetpack models
"""
import datetime
2011-02-22 16:09:13 +03:00
import commonware
from django.db import models
from django.conf import settings
2011-02-22 16:09:13 +03:00
from base.managers import QuerySetManager
2011-02-22 16:09:13 +03:00
log = commonware.log.getLogger('f.jetpack.managers')
#TODO: Add Library and Addon managers and use them inside Package and views
class SDKManager(models.Manager):
" manager for SDK class "
2012-01-28 03:30:44 +04:00
def exclude_disabled(self, current=None):
query = self
for disabled in settings.DISABLED_SDKS:
2012-01-28 03:30:44 +04:00
if not current or current != disabled:
query = query.exclude(version=disabled)
return query.all()
2010-10-01 17:56:48 +04:00
class PackageManager(QuerySetManager):
" manager for Package object "
2010-05-03 14:04:44 +04:00
class QuerySet(QuerySetManager.QuerySet):
def active(self, viewer=None):
" filter out inactive packages "
active_q = models.Q(active=True)
notdeleted_q = models.Q(deleted=False)
if viewer:
active_q = active_q | models.Q(author=viewer)
return self.filter(notdeleted_q).filter(active_q)
2011-02-22 16:09:13 +03:00
def active_with_deleted(self, viewer=None):
"""Filter out inactive packages, consider owners of packages
depending on deleted packages
"""
from jetpack.models import PackageRevision
active_q = models.Q(active=True)
notdeleted_q = models.Q(deleted=False)
if viewer:
private_q = models.Q(author=viewer)
active_q = active_q | private_q
undelete_list = [dep.to_packagerevision.package.pk for dep in \
list(PackageRevision.dependencies.through.objects.filter(
from_packagerevision__package__author=viewer,
to_packagerevision__package__deleted=True))]
undelete_q = models.Q(pk__in=undelete_list)
notdeleted_q = notdeleted_q | undelete_q
return self.filter(notdeleted_q).filter(active_q)
2011-02-22 16:09:13 +03:00
def active_with_disabled(self, viewer=None):
"""Filter out inactive packages, consider owners of packages
depending on disabled packages
"""
from jetpack.models import PackageRevision
active_q = models.Q(active=True)
notdeleted_q = models.Q(deleted=False)
if viewer:
private_q = models.Q(author=viewer)
activate_list = [dep.to_packagerevision.package.pk for dep in \
list(PackageRevision.dependencies.through.objects.filter(
from_packagerevision__package__author=viewer,
to_packagerevision__package__active=False))]
activated_q = models.Q(pk__in=activate_list)
active_q = active_q | private_q | activated_q
return self.filter(notdeleted_q).filter(active_q)
def disabled(self):
" filter out active packages "
return self.filter(active=False)
def addons(self):
" return addons only "
return self.filter(type="a")
def libraries(self):
" return libraries only "
return self.filter(type="l")
def sort_recently_active(self):
last_month = datetime.datetime.utcnow() - datetime.timedelta(30)
return self.extra(select={
'rev_count': 'select count(*) from jetpack_packagerevision '
'where jetpack_packagerevision.created_at > \'%s\' '
'and jetpack_packagerevision.package_id = jetpack_package.id'
% last_month.strftime('%Y-%m-%d')
}).order_by('-rev_count', *self.model._meta.ordering)