get_or_create fix, handle frame upload, remove dead code

This commit is contained in:
j 2010-02-06 17:04:20 +05:30
parent 7c622bfd36
commit 50f014f622
3 changed files with 15 additions and 49 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.objects.get_or_create(name=i) country, created = 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.objects.get_or_create(name=i) language, created = 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.objects.get_or_create(name=i) location, created = 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.objects.get_or_create(name=i) genre, created = 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.objects.get_or_create(name=g) keyword, created = 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

@ -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) return os.path.join(url_hash[:2], url_hash[2:4], url_hash[4:6], url_hash, name)
def frame_path(f): def frame_path(f):
position = oxlib.formatTime(f.position*1000).replace(':', '.') position = oxlib.formatDuration(f.position*1000).replace(':', '.')
name = "%s.%s" % (position, 'png') name = "%s.%s" % (position, 'png')
url_hash = f.file.oshash 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 = ( FILE_TYPES = (
(0, 'unknown'), (0, 'unknown'),
@ -1236,8 +1236,9 @@ 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.objects.get_or_create(oshash) file, created = File.objects.get_or_create(oshash)
file.save() if created:
file.save()
f = model.objects.create(archive=archive, file=file) f = model.objects.create(archive=archive, file=file)
f.save() f.save()
return f return f

View file

@ -484,17 +484,19 @@ def api_upload(request): #video, timeline, frame
''' '''
form = UploadForm(request.POST, request.FILES) form = UploadForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
data = json.loads(form.cleaned_data['data']) data = json.loads(request.POST['data'])
oshash = data['oshash'] oshash = data['oshash']
f = get_object_or_404(models.File, oshash=oshash) f = get_object_or_404(models.File, oshash=oshash)
if data['item'] == 'frame': if data['item'] == 'frame':
ff = form.cleaned_data['file'] ff = form.cleaned_data['file']
position = data['position'] 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.frame.save(ff.name, ff)
frame.save() frame.save()
response = {'status': {'code': 200, 'text': 'ok'}} response = {'status': {'code': 200, 'text': 'ok'}}
response['url'] = still.url() response['url'] = frame.frame.url
return render_to_json_response(response) return render_to_json_response(response)
if data['item'] == 'timeline': if data['item'] == 'timeline':
pass pass
@ -664,43 +666,6 @@ def find_files(request):
return render_to_json_response(response) 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): def apidoc(request):
''' '''
this is used for online documentation at http://127.0.0.1:8000/api/ this is used for online documentation at http://127.0.0.1:8000/api/