Album ID is not optional for picture upload.

Upload date is now stored and the last edit date is set to the upload date if not supplied.
This commit is contained in:
tanshu 2015-06-30 15:21:05 +05:30
parent e3ded59e53
commit d4ca3fffa0
2 changed files with 18 additions and 8 deletions

View File

@ -42,7 +42,7 @@
var file = files[i]; var file = files[i];
Upload.upload({ Upload.upload({
url: '/v1/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 file: file
}).progress(function (evt) { }).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);

View File

@ -1,3 +1,4 @@
import datetime
import hashlib import hashlib
import os import os
import shutil import shutil
@ -29,14 +30,19 @@ def html(request):
def upload(request): def upload(request):
input_file = request.POST['file'].file input_file = request.POST['file'].file
name = request.POST['name'] name = request.POST['name']
last_modified_date = request.POST['lastModifiedDate'] last_modified_date = request.POST.get('lastModifiedDate', None)
album_id = uuid.UUID(request.POST['album']) if 'album_id' in request.POST:
upload_file(input_file, name, last_modified_date, album_id, uuid.UUID(authenticated_userid(request))) album = Album.by_id(request.POST['album_id'])
return {'Status': 'OK'} 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') last_modified_date_prop = Property.by_name('last_modified_date')
upload_date_prop = Property.by_name('upload_date')
file_id = uuid.uuid4() file_id = uuid.uuid4()
file_path = pkg_resources.resource_filename('soter', 'upload/o/' + str(file_id) + '.jpg') file_path = pkg_resources.resource_filename('soter', 'upload/o/' + str(file_id) + '.jpg')
temp_file_path = file_path + '~' 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) shutil.copyfileobj(file, output_file)
pic_hash = get_hash(temp_file_path) pic_hash = get_hash(temp_file_path)
pic = Picture(name, pic_hash, user_id, True, file_id) pic = Picture(name, pic_hash, user_id, True, file_id)
pic.albums.append(Album.by_id(album_id)) if album is not None:
pic.properties.append(PictureProperty(property= last_modified_date_prop, data = last_modified_date)) 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) DBSession.add(pic)
try: try:
transaction.commit() transaction.commit()