diff --git a/soter/static/app/album/album.controller.js b/soter/static/app/album/album.controller.js index 86b5a94..28f04e3 100644 --- a/soter/static/app/album/album.controller.js +++ b/soter/static/app/album/album.controller.js @@ -42,7 +42,7 @@ var file = files[i]; Upload.upload({ url: '/v1/upload', - fields: {'album': info.id, name: file.name, lastModifiedDate: file.lastModifiedDate}, + fields: {'album_id': info.id, name: file.name, lastModifiedDate: file.lastModifiedDate}, file: file }).progress(function (evt) { var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); diff --git a/soter/views/picture.py b/soter/views/picture.py index 7269396..9cf260f 100644 --- a/soter/views/picture.py +++ b/soter/views/picture.py @@ -1,3 +1,4 @@ +import datetime import hashlib import os import shutil @@ -29,14 +30,19 @@ def html(request): def upload(request): input_file = request.POST['file'].file name = request.POST['name'] - last_modified_date = request.POST['lastModifiedDate'] - album_id = uuid.UUID(request.POST['album']) - upload_file(input_file, name, last_modified_date, album_id, uuid.UUID(authenticated_userid(request))) - return {'Status': 'OK'} + last_modified_date = request.POST.get('lastModifiedDate', None) + if 'album_id' in request.POST: + album = Album.by_id(request.POST['album_id']) + elif 'album' in request.POST: + album = Album.by_name(request.POST['album']) + else: + album = None + return upload_file(input_file, name, last_modified_date, album, uuid.UUID(authenticated_userid(request))) -def upload_file(file, name, last_modified_date, album_id, user_id): +def upload_file(file, name, last_modified_date, album, user_id): last_modified_date_prop = Property.by_name('last_modified_date') + upload_date_prop = Property.by_name('upload_date') file_id = uuid.uuid4() file_path = pkg_resources.resource_filename('soter', 'upload/o/' + str(file_id) + '.jpg') temp_file_path = file_path + '~' @@ -45,8 +51,12 @@ def upload_file(file, name, last_modified_date, album_id, user_id): shutil.copyfileobj(file, output_file) pic_hash = get_hash(temp_file_path) pic = Picture(name, pic_hash, user_id, True, file_id) - pic.albums.append(Album.by_id(album_id)) - pic.properties.append(PictureProperty(property= last_modified_date_prop, data = last_modified_date)) + if album is not None: + pic.albums.append(album) + if last_modified_date is None: + last_modified_date = datetime.datetime.utcnow() + pic.properties.append(PictureProperty(property=last_modified_date_prop, data=last_modified_date)) + pic.properties.append(PictureProperty(property=upload_date_prop, data=datetime.datetime.utcnow())) DBSession.add(pic) try: transaction.commit()