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

View file

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

View file

@ -606,7 +606,7 @@ def api_subtitles(request):
srt = data.get('subtitle', None) srt = data.get('subtitle', None)
if srt: if srt:
user = request.user 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.srt = srt
sub.save() sub.save()
else: else: