コード例 #1
0
def action_data_out_by_device():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceDataOutByDeviceForm.from_json(content)

    if form.validate():
        device = Device.query.get(form.device_id.data)

        if device:
            items = DeviceData.query.filter(
                DeviceData.device_id == form.device_id.data,
                DeviceData.type == form.type.data,
                DeviceData.created_at >= form.start_dt.data,
                DeviceData.created_at <= form.end_dt.data
            ).all()

            if items:
                labels = map(lambda item: item.created_at.strftime(form.format_dt.data), items)
                items = map(lambda item: item.value, items)

                return response.success(data={
                    'labels': labels,
                    'datasets': [{
                        'items': items
                    }]
                })
            else:
                return response.not_success('empty')
        else:
            return response.not_success('not-found')
    else:
        return response.from_form(form)
コード例 #2
0
def action_data_out_by_device():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceDataOutByDeviceForm.from_json(content)

    if form.validate():
        device = Device.query.filter(
            Device.token == form.device_token.data).first()

        if device:
            if form.order.data == "asc":
                order_by = asc(DeviceData.created_at)
            else:
                order_by = desc(DeviceData.created_at)

            items = (DeviceData.query.filter(
                DeviceData.device_id == device.id,
                DeviceData.type == form.type.data,
                DeviceData.created_at >= form.start_dt.data,
                DeviceData.created_at <= form.end_dt.data,
            ).order_by(order_by).limit(form.amount.data).all())

            if items:
                if form.format_dt.data:
                    labels = map(
                        lambda item: item.created_at.strftime(form.format_dt.
                                                              data),
                        items,
                    )
                else:
                    labels = map(
                        lambda item: int(
                            calendar.timegm(item.created_at.timetuple()) * 1000
                        ),
                        items,
                    )

                items = map(lambda item: item.value, items)

                return response.success(data={
                    "labels": labels,
                    "datasets": [{
                        "items": items
                    }]
                })
            else:
                return response.not_success("empty")
        else:
            return response.not_success("not-found")
    else:
        return response.from_form(form)
コード例 #3
0
def action_delete(account):
    if not account.is_admin:
        return response.unauthorized()

    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = AccountDeleteForm.from_json(content)

    if form.validate():
        if form.id.data > 1:
            found_account = Account.query.get(form.id.data)

            if found_account:
                db.session.delete(found_account)
                db.session.flush()
                db.session.commit()

                return response.success()
            else:
                return response.not_success("not-found")
        else:
            return response.with_validate_error(
                "id", ["Cannot delete this account."])
    else:
        return response.from_form(form)
コード例 #4
0
def action_update(account):
    if not account.is_admin:
        return response.unauthorized()

    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = AccountUpdateForm.from_json(content)

    if form.validate():
        found_account = Account.query.get(form.id.data)

        if found_account:
            found_account.name = form.name.data
            found_account.token = form.token.data
            found_account.email = form.email.data
            found_account.photo_url = form.photo_url.data
            found_account.is_admin = form.is_admin.data
            found_account.updated_at = datetime.utcnow()

            if form.password.data:
                found_account.set_password(form.password.data)

            db.session.flush()
            db.session.commit()

            found_account = Account.query.get(found_account.id)

            return response.success(
                data={"account": found_account.to_dict("update")})
        else:
            return response.not_success("not-found")
    else:
        return response.from_form(form)
コード例 #5
0
def action_update():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceUpdateForm.from_json(content)

    if form.validate():
        device = Device.query.get(form.id.data)

        if device:
            device.name = form.name.data
            device.updated_at = datetime.utcnow()

            db.session.flush()
            db.session.commit()

            device = Device.query.get(device.id)

            return response.success(data={
                'device': device.to_dict('update')
            })
        else:
            return response.not_success('not-found')
    else:
        return response.from_form(form)
コード例 #6
0
def action_token():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceGetForm.from_json(content)

    if form.validate():
        device = Device.query.get(form.id.data)

        if device:
            return response.success(data={"token": device.get_jwt_encoded()})
        else:
            return response.not_success("not-found")
    else:
        return response.from_form(form)
コード例 #7
0
def action_login():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    email = content.get("email")
    password = content.get("password")

    account = Account.query.filter_by(email=email).first()

    if account and account.check_password(password):
        encoded_jwt = account.get_jwt_encoded()

        return response.success(
            data={"account": account.to_dict("login"), "token": encoded_jwt}
        )
    else:
        return response.not_success("validate")
コード例 #8
0
def action_get():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceGetForm.from_json(content)

    if form.validate():
        device = Device.query.get(form.id.data)

        if device:
            return response.success(data={
                'device': device.to_dict('get')
            })
        else:
            return response.not_success('not-found')
    else:
        return response.from_form(form)
コード例 #9
0
def action_login():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    email = content.get('email')
    password = content.get('password')

    account = Account.query.filter_by(email=email).first()

    if account and account.check_password(password):
        encoded_jwt = account.get_jwt_encoded()

        return response.success(data={
            'account': account.to_dict('login'),
            'token': encoded_jwt
        })
    else:
        return response.not_success('validate')
コード例 #10
0
def action_delete():
    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = DeviceDeleteForm.from_json(content)

    if form.validate():
        device = Device.query.get(form.id.data)

        if device:
            db.session.delete(device)
            db.session.flush()
            db.session.commit()

            return response.success()
        else:
            return response.not_success("not-found")
    else:
        return response.from_form(form)
コード例 #11
0
def action_token(account):
    if not account.is_admin:
        return response.unauthorized()

    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = AccountGetForm.from_json(content)

    if form.validate():
        found_account = Account.query.get(form.id.data)

        if found_account:
            return response.success(
                data={"token": found_account.get_jwt_encoded()})
        else:
            return response.not_success("not-found")
    else:
        return response.from_form(form)
コード例 #12
0
def action_get(account):
    if not account.is_admin:
        return response.unauthorized()

    content = request.get_json(silent=True)
    content = content if content is not None else {}

    form = AccountGetForm.from_json(content)

    if form.validate():
        found_account = Account.query.get(form.id.data)

        if found_account:
            return response.success(
                data={'account': found_account.to_dict('get')})
        else:
            return response.not_success('not-found')
    else:
        return response.from_form(form)