This commit is contained in:
rolux 2011-11-02 08:27:54 +00:00
commit 7c37c1efde
6 changed files with 87 additions and 56 deletions

View file

@ -126,6 +126,12 @@ class Annotation(models.Model):
super(Annotation, self).save(*args, **kwargs) super(Annotation, self).save(*args, **kwargs)
if set_public_id: if set_public_id:
self.set_public_id() self.set_public_id()
Clip.objects.filter(**{
'id': self.clip.id,
self.layer.name: False
}).update(**{self.layer.name: True})
#how expensive is this? #how expensive is this?
#update_matching_events.delay(self.value) #update_matching_events.delay(self.value)
#update_matching_places.delay(self.value) #update_matching_places.delay(self.value)

View file

@ -165,10 +165,6 @@ class ClipManager(Manager):
if conditions: if conditions:
qs = qs.filter(conditions) qs = qs.filter(conditions)
if 'keys' in data: if 'keys' in data:
public_layers = [l['id'] for l in filter(lambda k: k in self.model.layers, data['keys']):
for l in filter(lambda l: not l.get('private', False), qs = qs.filter(**{l: True})
settings.CONFIG['layers'])]
filter_layers = filter(lambda k: k in public_layers, data['keys'])
if filter_layers:
qs = qs.filter(annotations__layer__name__in=filter_layers)
return qs return qs

View file

@ -9,37 +9,7 @@ from archive import extract
import managers import managers
class Clip(models.Model): class MetaClip:
'''
CREATE INDEX clip_clip_title_idx ON clip_clip (title ASC NULLS LAST);
CREATE INDEX clip_clip_director_idx ON clip_clip (director ASC NULLS LAST);
'''
class Meta:
unique_together = ("item", "start", "end")
objects = managers.ClipManager()
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
public_id = models.CharField(max_length=128, unique=True)
aspect_ratio = models.FloatField(default=0)
item = models.ForeignKey('item.Item', related_name='clips')
#seconds
start = models.FloatField(default=-1, db_index=True)
end = models.FloatField(default=-1)
duration = models.FloatField(default=0, db_index=True)
#get from annotation
hue = models.FloatField(default=0, db_index=True)
saturation = models.FloatField(default=0, db_index=True)
lightness = models.FloatField(default=0, db_index=True)
volume = models.FloatField(default=0, null=True, db_index=True)
director = models.CharField(max_length=1000, db_index=True)
title = models.CharField(max_length=1000, db_index=True)
def update_calculated_values(self): def update_calculated_values(self):
self.duration = self.end - self.start self.duration = self.end - self.start
if self.duration > 0: if self.duration > 0:
@ -60,7 +30,9 @@ class Clip(models.Model):
streams = self.item.streams() streams = self.item.streams()
if streams: if streams:
self.aspect_ratio = streams[0].aspect_ratio self.aspect_ratio = streams[0].aspect_ratio
super(Clip, self).save(*args, **kwargs) for l in self.layers:
setattr(self, l, self.annotations.filter(layer__name=l).count()>0)
models.Model.save(self, *args, **kwargs)
def json(self, keys=None): def json(self, keys=None):
j = {} j = {}
@ -104,3 +76,40 @@ class Clip(models.Model):
def __unicode__(self): def __unicode__(self):
return self.public_id return self.public_id
class Meta:
unique_together = ("item", "start", "end")
attrs = {
'__module__': 'clip.models',
'Meta': Meta,
'objects': managers.ClipManager(),
'created': models.DateTimeField(auto_now_add=True),
'modified': models.DateTimeField(auto_now=True),
'public_id': models.CharField(max_length=128, unique=True),
'aspect_ratio': models.FloatField(default=0),
'item': models.ForeignKey('item.Item', related_name='clips'),
#seconds
'start': models.FloatField(default=-1, db_index=True),
'end': models.FloatField(default=-1),
'duration': models.FloatField(default=0, db_index=True),
#get from annotation
'hue': models.FloatField(default=0, db_index=True),
'saturation': models.FloatField(default=0, db_index=True),
'lightness': models.FloatField(default=0, db_index=True),
'volume': models.FloatField(default=0, null=True, db_index=True),
'director': models.CharField(max_length=1000, db_index=True),
'title': models.CharField(max_length=1000, db_index=True),
}
public_layers = [l['id']
for l in filter(lambda l: not l.get('private', False),
settings.CONFIG['layers'])]
for name in public_layers:
attrs[name] = models.BooleanField(default=False, db_index=True)
Clip = type('Clip', (MetaClip,models.Model), attrs)
Clip.layers = public_layers

View file

@ -82,19 +82,21 @@ def findClips(request):
if 'keys' in data: if 'keys' in data:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
qs = qs.select_related('item__sort') #qs = qs.select_related('item__sort')
response['data']['items'] = [p.json(keys=data['keys']) for p in qs] ids = []
keys = filter(lambda k: k not in models.Clip.layers, data['keys'])
def add(p):
ids.append(p.id)
return p.json(keys=keys)
response['data']['items'] = [add(p) for p in qs]
keys = data['keys'] keys = data['keys']
public_layers = [l['id']
for l in filter(lambda l: not l.get('private', False),
settings.CONFIG['layers'])]
def add_annotations(layer, qs): def add_annotations(layer, qs):
for a in qs.values('public_id', 'value', 'clip__public_id'): for a in qs.values('public_id', 'value', 'clip__public_id'):
for i in response['data']['items']: for i in response['data']['items']:
if i['id'] == a['clip__public_id']: if i['id'] == a['clip__public_id']:
if not i[layer]: if not layer in i:
i[layer] = [] i[layer] = []
i[layer].append({ i[layer].append({
'id': a['public_id'], 'id': a['public_id'],
@ -103,10 +105,10 @@ def findClips(request):
if response['data']['items']: if response['data']['items']:
if 'annotations' in keys: if 'annotations' in keys:
add_annotations('annotations', add_annotations('annotations',
Annotation.objects.filter(layer__name__in=public_layers, clip__in=qs)) Annotation.objects.filter(layer__name__in=models.Clip.layers, clip__in=ids))
for layer in filter(lambda l: l in keys, public_layers): for layer in filter(lambda l: l in keys, models.Clip.layers):
add_annotations(layer, add_annotations(layer,
Annotation.objects.filter(layer__name=layer, clip__in=qs)) Annotation.objects.filter(layer__name=layer, clip__in=ids))
elif 'position' in query: elif 'position' in query:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
ids = [i.public_id for i in qs] ids = [i.public_id for i in qs]

View file

@ -1,4 +1,5 @@
Hi {{sitename}} admin, {{name}} <{{email}}> sent a contact message:
someone sent you a message:
{{message}} {{message}}
--
{{sitename}}

View file

@ -534,21 +534,38 @@ def contact(request):
} }
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
if 'email' in data and 'message' in data: name = data.get('name', '')
email = data['email'] email = data.get('email', '')
if request.user.is_authenticated():
if not name:
name = request.user.username
if not email:
email = request.user.email
if 'message' in data and data['message'].strip():
email_from = settings.CONFIG['site']['email']['system']
email_to = [settings.CONFIG['site']['email']['contact'], ]
template = loader.get_template('contact_email.txt') template = loader.get_template('contact_email.txt')
context = RequestContext(request, { context = RequestContext(request, {
'sitename': settings.SITENAME,
'email': email, 'email': email,
'message': data['message'], 'message': data['message'].strip(),
'name': name,
'sitename': settings.SITENAME,
}) })
message = template.render(context) message = template.render(context)
subject = '%s contact: %s' % (settings.SITENAME, data['subject']) subject = (u'[%s Contact] %s' % (settings.SITENAME, data.get('subject', ''))).strip()
response = json_response(text='message sent') response = json_response(text='message sent')
try: try:
send_mail(subject, message, email, [settings.DEFAULT_FROM_EMAIL, ]) send_mail(subject.strip(), message, email_from, email_to)
except BadHeaderError: except BadHeaderError:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
if request.user.is_authenticated() \
and 'receipt' in data \
and data['receipt']:
message = data['message'].strip()
try:
send_mail(subject.strip(), message, email_from, [email])
except:
pass
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)