From 50f014f622d9dfae4f6af33138eb34e6402e820e Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 6 Feb 2010 17:04:20 +0530 Subject: [PATCH] get_or_create fix, handle frame upload, remove dead code --- oxdb/backend/load.py | 10 +++++----- oxdb/backend/models.py | 9 +++++---- oxdb/backend/views.py | 45 +++++------------------------------------- 3 files changed, 15 insertions(+), 49 deletions(-) diff --git a/oxdb/backend/load.py b/oxdb/backend/load.py index 0c4b8a42e..d7c7143f6 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.objects.get_or_create(name=i) + country, created = 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.objects.get_or_create(name=i) + language, created = 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.objects.get_or_create(name=i) + location, created = 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.objects.get_or_create(name=i) + genre, created = 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.objects.get_or_create(name=g) + keyword, created = 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 7d3238e3e..511d824d3 100644 --- a/oxdb/backend/models.py +++ b/oxdb/backend/models.py @@ -989,10 +989,10 @@ def timeline_path(f): return os.path.join(url_hash[:2], url_hash[2:4], url_hash[4:6], url_hash, name) def frame_path(f): - position = oxlib.formatTime(f.position*1000).replace(':', '.') + position = oxlib.formatDuration(f.position*1000).replace(':', '.') name = "%s.%s" % (position, 'png') url_hash = f.file.oshash - return os.path.join(url_hash[:2], url_hash[2:4], url_hash[4:6], url_hash, name) + return os.path.join(url_hash[:2], url_hash[2:4], url_hash[4:6], url_hash, 'frames', name) FILE_TYPES = ( (0, 'unknown'), @@ -1236,8 +1236,9 @@ class ArchiveFile(models.Model): try: f = model.objects.by_oshash(oshash=oshash) except model.DoesNotExist: - file = File.objects.get_or_create(oshash) - file.save() + file, created = File.objects.get_or_create(oshash) + if created: + file.save() f = model.objects.create(archive=archive, file=file) f.save() return f diff --git a/oxdb/backend/views.py b/oxdb/backend/views.py index 80bae70c4..21f272c74 100644 --- a/oxdb/backend/views.py +++ b/oxdb/backend/views.py @@ -484,17 +484,19 @@ def api_upload(request): #video, timeline, frame ''' form = UploadForm(request.POST, request.FILES) if form.is_valid(): - data = json.loads(form.cleaned_data['data']) + data = json.loads(request.POST['data']) oshash = data['oshash'] f = get_object_or_404(models.File, oshash=oshash) if data['item'] == 'frame': ff = form.cleaned_data['file'] position = data['position'] - frame = models.Frame.objects.get_or_create(file=f, position=position) + frame, created = models.Frame.objects.get_or_create(file=f, position=position) + if not created and frame.frame: + frame.frame.delete() frame.frame.save(ff.name, ff) frame.save() response = {'status': {'code': 200, 'text': 'ok'}} - response['url'] = still.url() + response['url'] = frame.frame.url return render_to_json_response(response) if data['item'] == 'timeline': pass @@ -664,43 +666,6 @@ def find_files(request): return render_to_json_response(response) -class TimelineForm(forms.Form): - timeline = forms.FileField() - -#FIXME: should everybody be able to overwrite timelines? -#@login_required_json -def add_timeline(request, oshash): - response = {'status': 500} - f = get_object_or_404(models.File, oshash=oshash) - - form = TimelineForm(request.POST, request.FILES) - if form.is_valid(): - ff = form.cleaned_data['timeline'] - f.timeline.save(ff.name, ff) - response = {'status': 200} - response['url'] = f.timeline.url() - return render_to_json_response(response) - - -class VideoForm(forms.Form): - video = forms.FileField() - -#@login_required_json -def add_video(request, oshash): - response = {'status': 500} - f = get_object_or_404(models.File, oshash=oshash) - - form = VideoForm(request.POST, request.FILES) - if form.is_valid(): - ff = form.cleaned_data['video'] - f.stream128.save(ff.name, ff) - response = {'status': 200} - response['url'] = f.stream128.url() - return render_to_json_response(response) - - - - def apidoc(request): ''' this is used for online documentation at http://127.0.0.1:8000/api/