Esempio n. 1
0
def process_asset():

    if (request.POST.get('name','').strip() and 
        request.POST.get('uri','').strip() and
        request.POST.get('mimetype','').strip()
        ):

        asset = Asset()
        asset.name =  request.POST.get('name','').decode('UTF-8')
        asset.uri = request.POST.get('uri','').strip()
        asset.mimetype = request.POST.get('mimetype','').strip()

        # Make sure it's a valid resource
        uri_check = urlparse(asset.uri)
        #Support local assets both as /home/pi/image path #1.png and file:///home/pi/url%20path.png , Note special chars in absolute path.
        local_and_exists = ((uri_check.scheme == "" and path.exists(asset.uri)) or (uri_check.scheme == "file" and path.exists(uri_check.path)))
        if not (uri_check.scheme == "http" or uri_check.scheme == "https" or local_and_exists):
            header = "Ops!"
            message = "URL must be HTTP or HTTPS or absolute path to local file."
            return template('message', header=header, message=message)

        if not local_and_exists:
            file = req_head(asset.uri)

        # Only proceed if fetch was successful.
        if local_and_exists or file.status_code == 200:
            asset.asset_id = md5(asset.name + asset.uri).hexdigest()

            asset.start_date = ""
            asset.end_date = ""
            asset.duration = ""

            asset.INSERT(config)

            header = "Yay!"
            message =  "Added asset (" + asset.asset_id + ") to the database."
            return template('message', header=header, message=message)
            
        else:
            header = "Ops!"
            message = "Unable to fetch file."
            return template('message', header=header, message=message)
    else:
        header = "Ops!"
        message = "Invalid input."
        return template('message', header=header, message=message)
Esempio n. 2
0
def update_asset():

    if (request.POST.get('asset_id','').strip() and 
        request.POST.get('name','').strip() and
        request.POST.get('uri','').strip() and
        request.POST.get('mimetype','').strip()
        ):

        asset = Asset()
        asset.asset_id =  request.POST.get('asset_id','').strip()
        asset.name = request.POST.get('name','').decode('UTF-8')
        asset.uri = request.POST.get('uri','').strip()
        asset.mimetype = request.POST.get('mimetype','').strip()

        try:
            asset.duration = request.POST.get('duration','').strip()
        except:
            asset.duration = None

        try:
            input_start = request.POST.get('start','')
            asset.start_date = datetime.strptime(input_start, '%Y-%m-%d @ %H:%M')
        except:
            asset.start_date = None

        try:
            input_end = request.POST.get('end','').strip()
            asset.end_date = datetime.strptime(input_end, '%Y-%m-%d @ %H:%M')
        except:
            asset.end_date = None

        asset.UPDATE(config)

        header = "Yes!"
        message = "Successfully updated asset."
        return template('message', header=header, message=message)

    else:
        header = "Ops!"
        message = "Failed to update asset."
        return template('message', header=header, message=message)