2009-08-01 14:14:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2009-08-16 12:23:29 +00:00
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
from urllib2 import unquote
|
2010-01-25 14:57:10 +00:00
|
|
|
import json
|
2009-08-16 12:23:29 +00:00
|
|
|
|
2009-08-01 14:14:54 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2009-08-16 12:23:29 +00:00
|
|
|
from django.db.models import Q, Manager
|
2010-07-06 08:51:50 +00:00
|
|
|
|
2009-08-16 12:23:29 +00:00
|
|
|
import models
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2009-08-16 12:23:29 +00:00
|
|
|
def keyType(key):
|
2010-07-03 11:17:19 +00:00
|
|
|
if key in ('released', ):
|
2009-08-16 12:23:29 +00:00
|
|
|
return "date"
|
|
|
|
if key in ('year', 'cast.length'):
|
|
|
|
return "int"
|
|
|
|
if key in ('rating', 'votes'):
|
|
|
|
return "float"
|
|
|
|
return "string"
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2009-08-16 12:23:29 +00:00
|
|
|
class MovieManager(Manager):
|
2009-08-01 14:14:54 +00:00
|
|
|
def get_query_set(self):
|
|
|
|
return super(MovieManager, self).get_query_set()
|
|
|
|
|
2010-02-22 09:25:29 +00:00
|
|
|
def filter_list(self, qs, l, user):
|
|
|
|
if l != "all":
|
|
|
|
l = l.split(":")
|
|
|
|
only_public = True
|
|
|
|
if not user.is_anonymous():
|
|
|
|
if len(l) == 1: l = [request.user.username] + l
|
|
|
|
if request.user.username == l[0]:
|
|
|
|
only_public = False
|
|
|
|
if len(l) == 2:
|
|
|
|
lqs = models.List.objects.filter(name=l[1], user__username=l[0])
|
|
|
|
if only_public:
|
|
|
|
lqs = qls.filter(public=True)
|
|
|
|
if lqs.count() == 1:
|
|
|
|
qs = qs.filter(listitem__list__id=lqs[0].id)
|
|
|
|
return qs
|
|
|
|
|
2010-07-07 22:11:34 +00:00
|
|
|
def find(self, data, user):
|
2010-02-22 09:25:29 +00:00
|
|
|
'''
|
|
|
|
query: {
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
value: "war""
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key: "year",
|
|
|
|
value: "1970-1980,
|
|
|
|
operator: "!="
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "country",
|
|
|
|
value: "f",
|
|
|
|
operator: "^"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
operator: "&"
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
query_operator = data['query'].get('operator', '&')
|
|
|
|
conditions = []
|
|
|
|
for condition in data['query']['conditions']:
|
|
|
|
k = condition.get('key', 'all')
|
2010-07-06 08:04:24 +00:00
|
|
|
k = {'id': 'movieId'}.get(k, k)
|
2010-07-03 11:17:19 +00:00
|
|
|
if not k: k = 'all'
|
2010-02-22 09:25:29 +00:00
|
|
|
v = condition['value']
|
|
|
|
op = condition.get('operator', None)
|
2010-07-03 11:17:19 +00:00
|
|
|
if not op: op = '~'
|
2010-02-22 09:25:29 +00:00
|
|
|
if op.startswith('!'):
|
|
|
|
op = op[1:]
|
|
|
|
exclude = True
|
|
|
|
else:
|
|
|
|
exclude = False
|
|
|
|
if keyType(k) == "string":
|
|
|
|
if op == '=':
|
2010-07-05 08:21:09 +00:00
|
|
|
if k in ('director', 'country', 'language', 'genre', 'keywords', 'location', 'writer', 'producer', 'editor', 'cinematographer'):
|
|
|
|
k = '%s__icontains' % k
|
|
|
|
v = u'|%s|'%v
|
|
|
|
else:
|
|
|
|
k = '%s__iexact' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
elif op == '^':
|
|
|
|
v = v[1:]
|
|
|
|
k = '%s__istartswith' % k
|
|
|
|
elif op == '$':
|
|
|
|
v = v[:-1]
|
|
|
|
k = '%s__iendswith' % k
|
|
|
|
else: # elif op == '~':
|
|
|
|
k = '%s__icontains' % k
|
2010-07-06 08:04:24 +00:00
|
|
|
if not k.startswith('movieId'):
|
|
|
|
k = 'find__%s' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
k = str(k)
|
|
|
|
if exclude:
|
|
|
|
conditions.append(~Q(**{k:v}))
|
|
|
|
else:
|
|
|
|
conditions.append(Q(**{k:v}))
|
|
|
|
else: #number or date
|
|
|
|
def parseDate(d):
|
|
|
|
while len(d) < 3:
|
|
|
|
d.append(1)
|
|
|
|
return datetime(*[int(i) for i in d])
|
|
|
|
if op == '-':
|
|
|
|
v1 = v[1]
|
|
|
|
v2 = v[2]
|
|
|
|
if keyType(k) == "date":
|
|
|
|
v1 = parseDate(v1.split('.'))
|
|
|
|
v2 = parseDate(v2.split('.'))
|
2010-07-01 08:49:08 +00:00
|
|
|
|
|
|
|
k = 'find__%s' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
if exclude: #!1960-1970
|
|
|
|
k1 = str('%s__lt' % k)
|
|
|
|
k2 = str('%s__gte' % k)
|
|
|
|
conditions.append(Q(**{k1:v1})|Q(**{k2:v2}))
|
|
|
|
else: #1960-1970
|
|
|
|
k1 = str('%s__gte' % k)
|
|
|
|
k2 = str('%s__lt' % k)
|
|
|
|
conditions.append(Q(**{k1:v1})&Q(**{k2:v2}))
|
|
|
|
else:
|
|
|
|
if keyType(k) == "date":
|
|
|
|
v = parseDate(v.split('.'))
|
2010-07-01 08:49:08 +00:00
|
|
|
if op == '=':
|
|
|
|
k = '%s__exact' % k
|
|
|
|
elif op == '>':
|
|
|
|
k = '%s__gt' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
elif op == '>=':
|
2010-07-01 08:49:08 +00:00
|
|
|
k = '%s__gte' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
elif op == '<':
|
2010-07-01 08:49:08 +00:00
|
|
|
k = '%s__lt' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
elif op == '<=':
|
2010-07-01 08:49:08 +00:00
|
|
|
k = '%s__lte' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
|
2010-07-01 08:49:08 +00:00
|
|
|
k = 'find__%s' % k
|
2010-02-22 09:25:29 +00:00
|
|
|
k = str(k)
|
|
|
|
if exclude: #!1960
|
|
|
|
conditions.append(~Q(**{k:v}))
|
|
|
|
else: #1960
|
|
|
|
conditions.append(Q(**{k:v}))
|
|
|
|
|
|
|
|
#join query with operator
|
|
|
|
qs = self.get_query_set()
|
|
|
|
#only include movies that have hard metadata
|
|
|
|
qs = qs.filter(available=True)
|
|
|
|
if conditions:
|
|
|
|
q = conditions[0]
|
|
|
|
for c in conditions[1:]:
|
|
|
|
if query_operator == '|':
|
|
|
|
q = q | c
|
|
|
|
else:
|
|
|
|
q = q & c
|
|
|
|
qs = qs.filter(q)
|
|
|
|
|
|
|
|
# filter list, works for own or public lists
|
|
|
|
l = data.get('list', 'all')
|
|
|
|
qs = self.filter_list(qs, l, user)
|
|
|
|
return qs
|
|
|
|
|
2009-10-04 22:00:08 +00:00
|
|
|
class FileManager(Manager):
|
|
|
|
def get_query_set(self):
|
|
|
|
return super(FileManager, self).get_query_set()
|
|
|
|
|
|
|
|
def movie_files(self, movie):
|
|
|
|
q = self.get_query_set()
|
2009-12-31 15:04:32 +00:00
|
|
|
return q.filter(type=1, movie=movie)
|
2009-10-04 22:00:08 +00:00
|
|
|
|
2009-08-16 12:23:29 +00:00
|
|
|
class ArchiveFileManager(Manager):
|
|
|
|
def get_query_set(self):
|
2009-10-04 22:00:08 +00:00
|
|
|
return super(ArchiveFileManager, self).get_query_set()
|
|
|
|
|
|
|
|
def movie_files(self, movie):
|
|
|
|
q = self.get_query_set()
|
|
|
|
return q.filter(file__is_video=True, file__movie=movie)
|
2009-08-01 14:14:54 +00:00
|
|
|
|
2009-08-16 12:23:29 +00:00
|
|
|
def by_oshash(self, oshash):
|
|
|
|
q = self.get_query_set()
|
2009-12-31 15:04:32 +00:00
|
|
|
q = q.filter(file__oshash=oshash)
|
2009-08-16 12:23:29 +00:00
|
|
|
if q.count() == 0:
|
2009-10-04 22:00:08 +00:00
|
|
|
raise models.ArchiveFile.DoesNotExist("%s matching oshash %s does not exist." %
|
|
|
|
(models.ArchiveFile._meta.object_name, oshash))
|
2009-12-31 15:04:32 +00:00
|
|
|
else:
|
|
|
|
return q[0]
|
2009-08-01 14:14:54 +00:00
|
|
|
|