Exemple #1
0
def main():
    usage = 'usage: %s [options] <application>'
    parser = OptionParser(usage=usage)
    parser.set_defaults(url='https://127.0.0.1:8000/server.py', attributes=[], expiration=86400)
    parser.add_option('-u', '--url', dest='url', action='store', type='string', help='Server URL')
    parser.add_option('-a', '--attribute', dest='attributes', action='append', type='string', help='Vector attributes')
    parser.add_option('-l', '--lifetime', dest='expiration', action='store', type='int', help='Lifetime of request')
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error('Need to specify application name')

    data = sys.stdin.read()
    application = args[0]

    print >>sys.stderr, args[0]

    attrs = []
    for attr in options.attributes:
        attrs.append(tuple(attr.split(':', 1)))
    key = submit(options.url, data, application, attrs, options.expiration)
    sys.stdout.write(key)
Exemple #2
0
def main():
    usage = 'usage: %s [options] <application>'
    parser = OptionParser(usage=usage)
    parser.set_defaults(url='https://127.0.0.1:8000/server.py',
                        attributes=[],
                        expiration=86400)
    parser.add_option('-u',
                      '--url',
                      dest='url',
                      action='store',
                      type='string',
                      help='Server URL')
    parser.add_option('-a',
                      '--attribute',
                      dest='attributes',
                      action='append',
                      type='string',
                      help='Vector attributes')
    parser.add_option('-l',
                      '--lifetime',
                      dest='expiration',
                      action='store',
                      type='int',
                      help='Lifetime of request')
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error('Need to specify application name')

    data = sys.stdin.read()
    application = args[0]

    print >> sys.stderr, args[0]

    attrs = []
    for attr in options.attributes:
        attrs.append(tuple(attr.split(':', 1)))
    key = submit(options.url, data, application, attrs, options.expiration)
    sys.stdout.write(key)
Exemple #3
0
def upload(request):
    if not check_credentials(request):
        return HttpResponseRedirect('/login')

    vector_ids = request.REQUEST.get('vector_ids')
    if vector_ids:
        vectors = vector_ids.split(';')
    else:
        vectors = None

    args = {
        'token': request.session['token'],
        'userid': request.session['userid'],
        'username': request.session['username'],
        'vector_ids': vector_ids,
        'title': request.REQUEST.get('title'),
        'expiration': request.REQUEST.get('expiration'),
        'vectors': vectors
    }

    submit_button = request.REQUEST.get('submit')

    if submit_button is None:
        return render_to_response('upload.tpl', args)

    title = request.REQUEST.get('title')
    vector_ids = request.REQUEST.get('vector_ids')
    numtags = request.REQUEST.get('numtags')
    expiration = request.REQUEST.get('expiration')

    try:
        for idx in range(max(int(numtags), 200)):
            name = 'check%d' % idx
            if name in request.REQUEST:
                args[name] = ", checked: true"
    except ValueError:
        pass

    if title is None or len(title) == 0:
        args['error'] = 'You must enter a title'
        return render_to_response('upload.tpl', args)

    if expiration is None:
        args['expiration'] = 'You must specify an expiration time'
        return render_to_response('upload.tpl', args)

    if vector_ids is None or numtags is None:
        args['error'] = 'Form error'
        return render_to_response('upload.tpl', args)

    title = title.strip()
    filenames = vector_ids.split(';')
    for filename in filenames:
        if not is_valid_filename(filename):
            args['error'] = 'You must select photos to upload'
            return render_to_response('upload.tpl', args)

    try:
        numtags = int(numtags.strip())
    except ValueError:
        logger.error('numtags field in not a number')
        args['error'] = 'Inconsistent upload state. Please try again.'
        return render_to_response('upload.tpl', args)

    numtags = max(numtags,
                  200)  # Hard clamp on the number of tags, to prevent DoS
    tags = []
    for idx in range(numtags):
        name = 'check%d' % idx
        if name in request.REQUEST:
            tags.append(request.REQUEST[name])
    if len(tags) < 3:
        args['error'] = 'Please select at least 3 tags from the list'
        return render_to_response('upload.tpl', args)

    attributes = map(lambda tag: ('tag', tag), tags)

    try:
        expiration = int(expiration.strip())
    except ValueError:
        args['error'] = 'Please enter a valid number of hours'
        return render_to_response('upload.tpl', args)

    if expiration > 24 or expiration < 0:
        args['error'] = 'Expiration can be at most 24 hours'
        return render_to_response('upload.tpl', args)

    expiration = 60 * 60 * expiration

    for filename in filenames:
        try:
            vector = open(filename, 'rb').read()
            os.unlink(filename)
            key = submit(DONATION_SERVER, vector, APPLICATION_NAME, attributes,
                         expiration)
            logger.info('(%s / %s) Uploading photo %s',
                        request.session['userid'], request.session['token'],
                        key)
        except Exception as e:
            logger.error('(%s / %s) Error donating photo: %s',
                         request.session['userid'], request.session['token'],
                         e.message)
            args[
                'error'] = 'Cannot contact upload server. Please try again later.'
            return render_to_response('upload.tpl', args)

        wait_for_key(key, title, request.session['token'], tags)

    args['expiration'] = expiration / (60 * 60)
    return render_to_response('process.tpl', args)
Exemple #4
0
def upload(request):
    if not check_credentials(request):
        return HttpResponseRedirect('/login')

    vector_ids = request.REQUEST.get('vector_ids')
    if vector_ids:
        vectors = vector_ids.split(';')
    else:
        vectors = None

    args = {'token': request.session['token'],
            'userid': request.session['userid'],
            'username': request.session['username'],
            'vector_ids': vector_ids,
            'title': request.REQUEST.get('title'),
            'expiration': request.REQUEST.get('expiration'),
            'vectors': vectors}

    submit_button = request.REQUEST.get('submit')

    if submit_button is None:
        return render_to_response('upload.tpl', args)

    title = request.REQUEST.get('title')
    vector_ids = request.REQUEST.get('vector_ids')
    numtags = request.REQUEST.get('numtags')
    expiration = request.REQUEST.get('expiration')

    try:
        for idx in range(max(int(numtags), 200)):
            name = 'check%d' % idx
            if name in request.REQUEST:
                args[name] = ", checked: true"
    except ValueError:
        pass

    if title is None or len(title) == 0:
        args['error'] = 'You must enter a title'
        return render_to_response('upload.tpl', args)

    if expiration is None:
        args['expiration'] = 'You must specify an expiration time'
        return render_to_response('upload.tpl', args)

    if vector_ids is None or numtags is None:
        args['error'] = 'Form error'
        return render_to_response('upload.tpl', args)

    title = title.strip()
    filenames = vector_ids.split(';')
    for filename in filenames:
        if not is_valid_filename(filename):
            args['error'] = 'You must select photos to upload'
            return render_to_response('upload.tpl', args)

    try:
        numtags = int(numtags.strip())
    except ValueError:
        logger.error('numtags field in not a number')
        args['error'] = 'Inconsistent upload state. Please try again.'
        return render_to_response('upload.tpl', args)

    numtags = max(numtags, 200)     # Hard clamp on the number of tags, to prevent DoS
    tags = []
    for idx in range(numtags):
        name = 'check%d' % idx
        if name in request.REQUEST:
            tags.append(request.REQUEST[name])
    if len(tags) < 3:
        args['error'] = 'Please select at least 3 tags from the list'
        return render_to_response('upload.tpl', args)

    attributes = map(lambda tag: ('tag', tag), tags)

    try:
        expiration = int(expiration.strip())
    except ValueError:
        args['error'] = 'Please enter a valid number of hours'
        return render_to_response('upload.tpl', args)

    if expiration > 24 or expiration < 0:
        args['error'] = 'Expiration can be at most 24 hours'
        return render_to_response('upload.tpl', args)

    expiration = 60*60*expiration

    for filename in filenames:
        try:
            vector = open(filename, 'rb').read()
            os.unlink(filename)
            key = submit(DONATION_SERVER, vector, APPLICATION_NAME, attributes, expiration)
            logger.info('(%s / %s) Uploading photo %s', request.session['userid'], request.session['token'], key)
        except Exception as e:
            logger.error('(%s / %s) Error donating photo: %s', request.session['userid'], request.session['token'], e.message)
            args['error'] = 'Cannot contact upload server. Please try again later.'
            return render_to_response('upload.tpl', args)

        wait_for_key(key, title, request.session['token'], tags)

    args['expiration'] = expiration/(60*60)
    return render_to_response('process.tpl', args)