2011-10-11 11:29:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division, with_statement
|
|
|
|
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
import ox
|
|
|
|
|
2011-10-11 17:44:45 +00:00
|
|
|
from item import utils
|
|
|
|
|
2011-10-11 11:29:05 +00:00
|
|
|
import managers
|
|
|
|
|
|
|
|
def get_title_sort(title):
|
2011-10-15 15:21:41 +00:00
|
|
|
if isinstance(title, str):
|
|
|
|
title = unicode(title)
|
2011-10-11 19:05:11 +00:00
|
|
|
title = unicodedata.normalize('NFKD', title).strip()
|
|
|
|
if title:
|
|
|
|
title, created = Title.objects.get_or_create(title=title)
|
2011-10-15 15:21:41 +00:00
|
|
|
if created:
|
|
|
|
title.save()
|
2011-10-11 19:05:11 +00:00
|
|
|
sorttitle = unicodedata.normalize('NFKD', title.sorttitle)
|
|
|
|
else:
|
|
|
|
sorttitle = u''
|
2011-10-11 15:40:13 +00:00
|
|
|
return sorttitle
|
2011-10-11 11:29:05 +00:00
|
|
|
|
|
|
|
class Title(models.Model):
|
2011-10-11 11:32:38 +00:00
|
|
|
title = models.CharField(max_length=1000, unique=True)
|
2011-10-11 15:40:13 +00:00
|
|
|
sorttitle = models.CharField(max_length=1000)
|
2011-10-11 17:44:45 +00:00
|
|
|
sortsorttitle = models.CharField(max_length=1000)
|
2011-10-11 15:01:11 +00:00
|
|
|
edited = models.BooleanField(default=False)
|
2011-10-11 11:29:05 +00:00
|
|
|
|
|
|
|
imdbId = models.CharField(max_length=7, blank=True)
|
|
|
|
|
|
|
|
objects = managers.TitleManager()
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2011-10-11 15:40:13 +00:00
|
|
|
if not self.sorttitle:
|
|
|
|
self.sorttitle = ox.get_sort_title(self.title)
|
|
|
|
self.sorttitle = unicodedata.normalize('NFKD', self.sorttitle)
|
2011-10-11 19:05:11 +00:00
|
|
|
self.sortsorttitle = utils.sort_title(self.sorttitle)
|
2011-10-11 11:29:05 +00:00
|
|
|
super(Title, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_or_create(model, title, imdbId=None):
|
|
|
|
if imdbId:
|
|
|
|
q = model.objects.filter(title=title, imdbId=imdbId)
|
|
|
|
else:
|
|
|
|
q = model.objects.all().filter(title=title)
|
|
|
|
if q.count() > 0:
|
|
|
|
o = q[0]
|
|
|
|
else:
|
|
|
|
o = model.objects.create(title=title)
|
|
|
|
if imdbId:
|
|
|
|
o.imdbId = imdbId
|
|
|
|
o.save()
|
|
|
|
return o
|
|
|
|
get_or_create = classmethod(get_or_create)
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return ox.to26(self.id)
|
|
|
|
|
|
|
|
def json(self, keys=None, user=None):
|
|
|
|
j = {
|
|
|
|
'id': self.get_id(),
|
|
|
|
'title': self.title,
|
2011-10-11 15:40:13 +00:00
|
|
|
'sorttitle': self.sorttitle,
|
2011-10-11 11:29:05 +00:00
|
|
|
}
|
|
|
|
if keys:
|
|
|
|
for key in j.keys():
|
|
|
|
if key not in keys:
|
|
|
|
del j[key]
|
|
|
|
return j
|
2011-10-11 15:23:31 +00:00
|
|
|
|
|
|
|
def update_sort_title():
|
|
|
|
for t in Title.objects.all():
|
2011-10-11 15:40:13 +00:00
|
|
|
_sorttitle = ox.get_sort_title(t.title)
|
|
|
|
_sorttitle = unicodedata.normalize('NFKD', _sorttitle)
|
|
|
|
if (not t.edited and _sorttitle != t.sorttitle) or \
|
|
|
|
(t.edited and _sorttitle == t.sorttitle):
|
|
|
|
t.sorttitle = _sorttitle
|
2011-10-11 15:23:31 +00:00
|
|
|
t.edited = False
|
|
|
|
t.save()
|