use django get_or_create where possible

This commit is contained in:
j 2010-02-03 18:13:41 +05:30
parent 0cf4c0512c
commit 76368153ba
3 changed files with 7 additions and 32 deletions

View file

@ -86,7 +86,7 @@ def loadIMDb(imdbId):
if 'country' in info:
for i in info['country']:
debug("add country", i)
country = models.Country.get_or_create(i)
country = models.Country.objects.get_or_create(name=i)
models.MovieCountry.link(movie, country, position)
position += 1
@ -96,7 +96,7 @@ def loadIMDb(imdbId):
if 'language' in info:
for i in info['language']:
debug("add language", i)
language = models.Language.get_or_create(i)
language = models.Language.objects.get_or_create(name=i)
models.MovieLanguage.link(movie, language, position)
position += 1
@ -105,7 +105,7 @@ def loadIMDb(imdbId):
locations = oxweb.imdb.getMovieLocations(imdbId)
for i in locations:
debug("add location", i)
location = models.Location.get_or_create(i)
location = models.Location.objects.get_or_create(name=i)
location.movies.add(movie)
#Genre
@ -113,7 +113,7 @@ def loadIMDb(imdbId):
if 'genre' in info:
for i in info['genre']:
debug("add genre", i)
genre = models.Genre.get_or_create(i)
genre = models.Genre.objects.get_or_create(name=i)
genre.movies.add(movie)
#Keyword
@ -121,7 +121,7 @@ def loadIMDb(imdbId):
keywords = oxweb.imdb.getMovieKeywords(imdbId)
for g in keywords:
debug("add keyword", g)
keyword = models.Keyword.get_or_create(g)
keyword = models.Keyword.objects.get_or_create(name=g)
keyword.movies.add(movie)
movie.trivia_all.filter(manual=False).delete()

View file

@ -644,13 +644,6 @@ class AlternativeTitle(models.Model):
def json(self):
return (self.title, self.type)
def get_or_create(model, name):
try:
o = model.objects.get(name=name)
except model.DoesNotExist:
o = model.objects.create(name=name)
o.save()
return o
class Person(models.Model):
name = models.CharField(max_length=200)
@ -736,8 +729,6 @@ class Country(models.Model):
def __unicode__(self):
return self.name
get_or_create = classmethod(get_or_create)
def json(self):
return self.name
@ -777,7 +768,6 @@ class Language(models.Model):
def __unicode__(self):
return self.name
get_or_create = classmethod(get_or_create)
def json(self):
return self.name
@ -820,8 +810,6 @@ class Keyword(models.Model):
def __unicode__(self):
return self.name
get_or_create = classmethod(get_or_create)
def json(self):
return self.name
@ -836,8 +824,6 @@ class Genre(models.Model):
def __unicode__(self):
return self.name
get_or_create = classmethod(get_or_create)
def json(self):
return self.name
@ -861,8 +847,6 @@ class Location(models.Model):
def __unicode__(self):
return self.name
get_or_create = classmethod(get_or_create)
def json(self):
return self.name
@ -1074,15 +1058,6 @@ class File(models.Model):
objects = managers.FileManager()
def get_or_create(model, oshash):
try:
f = model.objects.get(oshash=oshash)
except model.DoesNotExist:
f = model.objects.create(oshash=oshash)
f.save()
return f
get_or_create = classmethod(get_or_create)
def __unicode__(self):
return "%s (%s)" % (self.path, self.oshash)
@ -1204,7 +1179,7 @@ class ArchiveFile(models.Model):
try:
f = model.objects.by_oshash(oshash=oshash)
except model.DoesNotExist:
file = File.get_or_create(oshash)
file = File.objects.get_or_create(oshash)
file.save()
f = model.objects.create(archive=archive, file=file)
f.save()

View file

@ -606,7 +606,7 @@ def api_subtitles(request):
srt = data.get('subtitle', None)
if srt:
user = request.user
sub = models.Subtitles.get_or_create(user, oshash, language)
sub = models.Subtitles.objects.get_or_create(user, oshash, language)
sub.srt = srt
sub.save()
else: