From 76368153babe42f23b65a1cf4e53e7d9a120fdc4 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 3 Feb 2010 18:13:41 +0530 Subject: [PATCH] use django get_or_create where possible --- oxdb/backend/load.py | 10 +++++----- oxdb/backend/models.py | 27 +-------------------------- oxdb/backend/views.py | 2 +- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/oxdb/backend/load.py b/oxdb/backend/load.py index 3ab4d650d..0c4b8a42e 100644 --- a/oxdb/backend/load.py +++ b/oxdb/backend/load.py @@ -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() diff --git a/oxdb/backend/models.py b/oxdb/backend/models.py index dacb08a76..059922dce 100644 --- a/oxdb/backend/models.py +++ b/oxdb/backend/models.py @@ -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() diff --git a/oxdb/backend/views.py b/oxdb/backend/views.py index e8da90310..e5be67c3b 100644 --- a/oxdb/backend/views.py +++ b/oxdb/backend/views.py @@ -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: