예제 #1
0
def _is_file_on_disk(filename):
    total_path = os.path.join(app.config['DEMO_STORAGE_DIR'], filename)
    demo = Demo.get_from_filename(filename)
    file_exists = False
    category = "success"
    msg = total_path
    title = "Error"
    if demo:
        file_exists = True
        title = "Whoops!"
        msg = "<a href='%s'>That Demo already exists</a>!" % \
              (url_for('view_demo', demo=demo.id))
        category = 'warning'
    elif os.path.exists(total_path):
        file_exists = True
        title = "That Demo already exists!"
        msg = "That Demo <strong>file</strong> already exists, but hasn't been added to the repository. " \
              "Perhaps you'd like to <a href='%s'>import it</a>?" % (url_for("import_demo"))
        category = 'warning'
    return file_exists, title, msg, category
예제 #2
0
def _do_upload_demo_file(the_file):
    """
    Returns: Demo Object/False, Message, Message Type (to be passed to flash)
    """
    if not the_file:
        return False, "You must select a demo to upload!", "error"
    if not allowed_file(the_file.filename):
        return False, "DOH! Only .dem files are allowed!", "error"
    demo = None
    filename = secure_filename(the_file.filename)
    file_exists, title, total_path, category = _is_file_on_disk(filename)
    if file_exists:
        # NOTE: total_path here will be the message!
        return False, total_path, category

    success, msg = Demo.create_from_name(filename)
    if success:
        category = 'success'
        the_file.save(total_path)
        db.session.commit()
        demo = Demo.get_from_filename(filename)
    else:
        category = 'error'
    return demo, title, msg, category