예제 #1
0
def api_upload(request):
    if request.method == 'POST':
        picture = Picture()
        user = User.objects.get(email=request.POST['email'])
        picture.user = user
        form = UploadFileForm(request.POST, request.FILES, instance=picture)
        if form.is_valid():
            picture.picture = request.FILES['picture']
            if request.POST.has_key('gallery_id'):
                gallery = Gallery.objects.get(id=request.POST['gallery_id'],
                                              user=user)
                picture.gallery = gallery

            picture.picture.name = unidecode(picture.picture.name)
            picture.save()
            rotate_picture(picture)
            picture.update_thumb()

            if request.POST.has_key('description'):
                picture_description = PictureDescription(picture=picture)
                picture_description.text = request.POST['description']
                picture_description.save()

            response = {'status': 'OK'}
            response['data'] = serialize_picture(picture)
            return HttpResponse(json.dumps(response))

    return HttpResponse(content_200)
예제 #2
0
def api_upload(request):
    if request.method == 'POST':
        picture = Picture()
        user = User.objects.get(email=request.POST['email'])
        picture.user = user
        form = UploadFileForm(request.POST, request.FILES, instance=picture)
        if form.is_valid():
            picture.picture = request.FILES['picture']
            if request.POST.has_key('gallery_id'):
                gallery = Gallery.objects.get(id=request.POST['gallery_id'], user=user)
                picture.gallery = gallery

            picture.picture.name = unidecode(picture.picture.name)
            picture.save()
            rotate_picture(picture)
            picture.update_thumb()

            if request.POST.has_key('description'):
                picture_description = PictureDescription(picture=picture)
                picture_description.text = request.POST['description']
                picture_description.save()  

            response = {'status':'OK'}
            response['data'] = serialize_picture(picture)
            return HttpResponse(json.dumps(response))

    return HttpResponse(content_200)
예제 #3
0
def cron_sync():
    print 'start cron_sync ...'
    with app.test_request_context():
        users = User.query.all()
        for user in users:
            print user
            # make sure not conflict
            if get_user_last_activity(user.id) is not None:
                continue
            resp = client.metadata(user.vdisk_token, '/idesktop')
            if isinstance(resp, str):
                print 'error in metadata'
                continue
            result = json.loads(resp.read())
            if result['hash'] == user.vdisk_hash:
                print 'same hash'
                continue
            user.vdisk_hash = result['hash']
            db.session.add(user)
            db.session.commit()

            contents = [content for content in result['contents'] if is_good(content)]
            for content in contents:
                #print content['md5']
                pic = Picture.query.filter_by(hash_id=content['md5']).first()
                if not pic:  # need to upload
                    #upload to upyun
                    ret = client.media(user.vdisk_token, content['path'])
                    if isinstance(ret, str):
                        continue
                    url = json.loads(ret.read())['url']
                    filename = content['md5']
                    filename += '.' + content['path'].split('.')[-1]
                    low_q.enqueue(down_upload, url, filename)

                    # add to db
                    filename = content['path'].split('/')[-1]
                    picture = Picture(filename, content['md5'])
                    picture.in_yun = True
                    picture.user = user
                    picture.users.append(user)
                    db.session.add(picture)
                    db.session.commit()
                    #print filename + '#' + url
                elif user not in pic.users:
                    pic.users.append(user)
                    db.session.add(pic)
                    db.session.commit()
            #handle self delete in local folder
            hashs = [content['md5'] for content in contents]
            for pic in user.downloads.all():
                if pic.hash_id not in hashs:
                    #need delete
                    pic.users.remove(user)
                    db.session.add(pic)
                    db.session.commit()
    print 'stop cron_sync ...'
    ret = {'result':'ok'}
    return jsonify(**ret)
예제 #4
0
def sync():
    resp = client.metadata(g.user.vdisk_token, '/idesktop')
    if isinstance(resp, str):
        flash('get metadata failed!', 'error')
        return redirect(url_for('setting'))
    result = json.loads(resp.read())
    user = g.user
    user.vdisk_hash = result['hash']
    db.session.add(user)
    db.session.commit()
    #handle self add file in local folder
    contents = [content for content in result['contents'] if is_good(content)]
    for content in contents:
        #print content['md5']
        pic = Picture.query.filter_by(hash_id=content['md5']).first()
        if not pic:  # need to upload
            #upload to upyun
            ret = client.media(g.user.vdisk_token, content['path'])
            if isinstance(ret, str):
                continue
            url = json.loads(ret.read())['url']
            filename = content['md5']
            filename += '.' + content['path'].split('.')[-1]
            #get_queue('default').enqueue(down_upload, url, filename)
            low_q.enqueue(down_upload, url, filename)
            #down_upload(url, filename)
            # add to db
            filename = content['path'].split('/')[-1]
            picture = Picture(filename, content['md5'])
            picture.in_yun = True
            picture.user = g.user
            picture.users.append(g.user)
            db.session.add(picture)
            db.session.commit()
            #print filename + '#' + url
        elif g.user not in pic.users:
            pic.users.append(g.user)
            db.session.add(pic)
            db.session.commit()
    #handle self delete in local folder
    hashs = [content['md5'] for content in contents]
    for pic in g.user.downloads.all():
        if pic.hash_id not in hashs:
            #need delete
            pic.users.remove(g.user)
            db.session.add(pic)
            db.session.commit()
    flash('sync completed!', 'success')
    return redirect(url_for('setting'))
예제 #5
0
def upload_picture(request):
    """Upload picture view.
    """
    if request.method == 'POST':
        picture = Picture()
        picture.user = request.user
        form = UploadFileForm(request.POST, request.FILES, instance=picture)
        response = {}

        if form.is_valid():
            picture.picture = request.FILES['picture']
            picture.picture.name = unidecode(picture.picture.name)
            picture.save()

            ### nasty error at 3:45 AM ;/
            try:
                rotate_picture(picture)
                picture.update_thumb()
                picture.save()
            except:
                import sys, traceback
                traceback.print_exc(file=sys.stdout)
                picture.delete()
                response[
                    'status'] = 'UPLOAD ERROR. PUT HELMET ON AND EVACUATE!!!'
                response = json.dumps(response)
                return HttpResponse(response)
            response['url'] = picture.picture_thumb.url
            response['id'] = picture.id
            response = json.dumps(response)
        else:
            response = "Failed to upload"
    else:
        if request.GET.has_key('gallery_id'):
            preffered_gallery = int(request.GET['gallery_id'])
        else:
            preffered_gallery = None
        galleries = Gallery.objects.filter(user=request.user)
        return render(request, "upload.html", {
            'galleries': galleries,
            'preffered_gallery': preffered_gallery
        })
    return HttpResponse(response)
예제 #6
0
파일: views.py 프로젝트: PicUpIt/picup-it
def upload_picture(request):
    """Upload picture view.
    """
    if request.method == "POST":
        picture = Picture()
        picture.user = request.user
        form = UploadFileForm(request.POST, request.FILES, instance=picture)
        response = {}

        if form.is_valid():
            picture.picture = request.FILES["picture"]
            picture.picture.name = unidecode(picture.picture.name)
            picture.save()

            ### nasty error at 3:45 AM ;/
            try:
                rotate_picture(picture)
                picture.update_thumb()
                picture.save()
            except:
                import sys, traceback

                traceback.print_exc(file=sys.stdout)
                picture.delete()
                response["status"] = "UPLOAD ERROR. PUT HELMET ON AND EVACUATE!!!"
                response = json.dumps(response)
                return HttpResponse(response)
            response["url"] = picture.picture_thumb.url
            response["id"] = picture.id
            response = json.dumps(response)
        else:
            response = "Failed to upload"
    else:
        if request.GET.has_key("gallery_id"):
            preffered_gallery = int(request.GET["gallery_id"])
        else:
            preffered_gallery = None
        galleries = Gallery.objects.filter(user=request.user)
        return render(request, "upload.html", {"galleries": galleries, "preffered_gallery": preffered_gallery})
    return HttpResponse(response)