Ejemplo n.º 1
0
def new_asset():

    form = AssetForm()

    # Get new address from wallet and assign to payment address field

    #addr = get_acc_addr(account)

    addr = get_addr()

    form.asset_address.data = addr

    # Add endpoints to SelectField
    form.endpoint.choices = [(endpoint_row.id, endpoint_row.name)
                             for endpoint_row in tbl_endpoints.query.all()]

    # Add services to SelectField
    form.service.choices = [(service_row.id, service_row.name)
                            for service_row in tbl_services.query.all()]

    # Add user accounts to SelectField
    #form.tag_account.choices = [(acc_row.id, acc_row.name) for acc_row in tbl_accounts.query.filter_by(owner=current_user.id)]

    if form.validate_on_submit():
        if form.test_service.data:
            #print("hei")
            test_service(form)
        elif form.submit.data:
            asset = tbl_assets()
            save_asset(asset, form, new=True)
            flash('New asset created sucessfully!!')
            return redirect(url_for('my_assets'))
    return render_template('asset.html', title='New asset', form=form)
Ejemplo n.º 2
0
def index():

    form = AssetForm()

    if debug:
        flash('flashing() works.')

    # get all assets from database,
    assets = Asset.query.all()

    # always false on GET request
    if form.validate_on_submit():

        # TODO: data is sanitized? of what? https://wtforms.readthedocs.io/en/2.2.1/fields/
        new_thing = Asset(name=form.name.data,
                          owner=form.owner.data,
                          properties=form.properties.data,
                          services=form.services.data,
                          events=form.events.data)

        db.session.add(new_thing)
        db.session.commit()
        flash('New thing submitted, reloading...?')
        return redirect(url_for('index'))

    return render_template('index_holygrail.html',
                           title='Home',
                           assets=assets,
                           form=form)
Ejemplo n.º 3
0
def create_asset():
    """Create a new asset"""
    form = AssetForm()
    form["csrf_token"].data = request.cookies["csrf_token"]
    
    if form.validate_on_submit():
        image_filename = upload_file(form["image"].data)
        asset = Asset(
            user=current_user,
            category=form["category"].data,
            title=form["title"].data,
            description=form["description"].data,
            color=form["color"].data,
            icon=form["icon"].data,
            image=image_filename,
            value=form["value"].data,
            max=form["max"].data,
            is_allowed_multiple=form["is_allowed_multiple"].data,
            )
        
        # TODO Option to add asset assets, meters, conditions?
        db.session.add(asset)
        db.session.commit()
        return asset.to_dict()
    else:
        return {"errors": validation_errors_to_messages(form.errors)}, 401
Ejemplo n.º 4
0
def edit_asset(id):

    asset = tbl_assets.query.filter_by(id=id).first_or_404()

    if asset:

        # Check ownership
        if asset.owner == current_user.id:

            form = AssetForm()

            # Add endpoints to SelectField
            form.endpoint.choices = [
                (endpoint_row.id, endpoint_row.name)
                for endpoint_row in tbl_endpoints.query.all()
            ]

            # Add services to SelectField
            form.service.choices = [
                (service_row.id, service_row.name)
                for service_row in tbl_services.query.all()
            ]

            # Add user assets to SelectField
            #form.parent_asset.choices = [(parass_row.id, parass_row.name) for parass_row in tbl_assets.query.filter_by(owner=current_user.id)]

            if form.validate_on_submit():
                if form.test_service.data:
                    test_service(form)
                    #print("hei")
                elif form.submit.data:
                    save_asset(asset, form)
                    flash('Asset updated successfully!')
                    return redirect(url_for('my_assets'))
            elif request.method == 'GET':
                # Populate form fields here
                form.name.data = asset.name
                form.description.data = asset.description
                form.price.data = asset.price / 1000000
                form.asset_address.data = asset.asset_address
                form.settlement_address.data = asset.settlement_address
                form.endpoint.data = asset.endpoint
                form.service.data = asset.service
                form.service_data.data = asset.service_data
                form.enabled.data = asset.enabled

            return render_template('asset.html', title='Edit asset', form=form)

        else:
            flash('You are not authorized to edit this asset!!')
            return render_template('authorization_error.html',
                                   title='Not authorized!!')

    else:
        return 'Error loading #{id}'.format(id=id)
Ejemplo n.º 5
0
def asset_edit(asset_id=None):
    form = AssetForm()

    if form.validate_on_submit():
        return asset_submit(form, asset_id)
    else:
        logging.info('loading current values because: {}'.format(form.errors))
        
        old_asset = assets.GetAsset(a.GetAssetRequest(_id=asset_id))

        # All of this fuckery is needed because the proto object validates field types,
        # so we can't just change the field to a datetime object but need a new object
        asset_dict = MessageToDict(message=old_asset, preserving_proto_field_name=True)
        asset_dict['acquired'] = date.fromtimestamp(old_asset.acquired)
        # Have to delete _id since it's not a valid field for a namedtuple
        del asset_dict['_id']
        asset_obj = namedtuple("Asset", asset_dict.keys()) (*asset_dict.values())
        form = AssetForm(obj=asset_obj)

    return render_template('asset_edit.html', form=form, view='Edit Asset')
Ejemplo n.º 6
0
def edit_asset(aid):
    """Edit an asset."""
    form = AssetForm()
    form["csrf_token"].data = request.cookies["csrf_token"]
    
    if form.validate_on_submit():
        image_filename = upload_file(form["image"].data)
        asset = Asset.query.get(aid)
        asset.category = form["category"].data,
        asset.title = form["title"].data
        asset.description = form["description"].data
        asset.color = form["color"].data
        asset.icon = form["icon"].data
        asset.value = form["value"].data
        asset.max = form["max"].data
        asset.is_allowed_multiple = form["is_allowed_multiple"].data
        asset.image = image_filename
        db.session.commit()
        return asset.to_dict()
    else:
        return {"errors": validation_errors_to_messages(form.errors)}, 401
Ejemplo n.º 7
0
def asset_create():
    form = AssetForm()
    if form.validate_on_submit():
        return asset_submit(form)
    return render_template('asset_edit.html', form=form, view="Create Asset")