def new_setting(user_service: UserService): light_form = LightSettingsForm() if light_form.validate_on_submit(): user_service.add_setting(light_form.status.data, light_form.red.data, light_form.green.data, light_form.blue.data) return redirect(url_for('.index'))
def task(task_id: int, user_service: UserService): task = user_service.get_task(task_id) form = TaskForm.form_for_task(task) if form.validate_on_submit(): user_service.update_task(task, form.data) return redirect(url_for('.index')) return render_template('task.html', form=form, task_url=url_for('.task', task_id=task_id, is_new=False))
def edit_lights(device_id: int, user: User, user_service: UserService): device = Device.by_id(device_id) edit_form = create_edit_form(device.lights, user.tasks) if edit_form.validate_on_submit(): user_service.update_lights(device_id, edit_form.data) # regenerate the form, in case there were size changes edit_form = create_edit_form(device.lights, user.tasks) return render_template('edit.html', form=edit_form, device_id=device.id)
def index(user: User, user_service: UserService): form = GaugeForm(user=user) if form.validate_on_submit(): user_service.add_gauge(form.task_id.data, form.min_val.data, form.max_val.data, form.nickname.data) return render_template('gauges.html', form=GaugeForm(user=user), gauges=user.gauges) return render_template('gauges.html', form=form, gauges=user.gauges)
def user(app, faker, password): user = UserService.create_user(faker.email(), password) yield user ambrose.db.session.delete(user) ambrose.db.session.commit()
def new_message(message_type: str, user: User, user_service: UserService): form = MessageForm.new_message_form(message_type, user=user) if form.validate_on_submit(): user_service.create_message(message_type, form.data) return redirect(url_for('.index')) params = { 'form': form, 'message_type': message_type, 'message_url': url_for('.new_message', message_type=message_type), 'is_new': True, 'variables': Message.class_variables_for(message_type) } return render_template('message.html', **params)
def delete(self, device_id): user = AuthService.current_api_user() if not user: abort(401) UserService(user).delete_device(device_id) return 'No content', 204
def register_device(user_service: UserService): settings = RegisterDeviceSchema().load(request.json) device = user_service.add_device(settings['name'], settings['lights'], settings['gauges'], settings['messages']) return {'access_token': AuthService.jwt(device)}
def get(self, message_id): user = AuthService.current_api_user() if message_id is None: retval = MessageSchema().dump(user.messages, many=True) else: msg = UserService(user).get_message(message_id) retval = MessageSchema.dump(msg) return jsonify(retval)
def register(): form = RegisterForm() if form.validate_on_submit(): user = UserService.create_user(form.username.data, form.password.data) AuthService.login_user(user) return redirect(url_for('.index')) return redirect(url_for('.login'))
def get(self, task_id): user = AuthService.current_api_user() if task_id is None: retval = TaskSchema().dump(user.tasks, many=True) else: msg = UserService(user).get_task(task_id) retval = TaskSchema.dump(msg) return jsonify(retval)
def edit_message(message_id: int, user: User, user_service: UserService): message = user_service.get_message(message_id) if message is None: abort(404) form = MessageForm.new_message_form(message.type, user=user, obj=message) if form.validate_on_submit(): user_service.update_message(message, form.data) return redirect(url_for('.index')) params = { 'form': form, 'message_url': url_for('.edit_message', message_id=message_id), 'is_new': False, 'variables': message.variables } return render_template('message.html', **params)
def index(user: User, user_service: UserService): settings = [{ 'status': s.status, 'red': s.color_red, 'green': s.color_green, 'blue': s.color_blue, 'setting_id': s.id } for s in user.light_settings] edit_form = EditSettingsForm(data={'settings': settings}) if edit_form.validate_on_submit(): user_service.edit_settings(edit_form.settings.data) return redirect(url_for('.index')) return render_template('settings.html', edit_form=edit_form, new_form=LightSettingsForm())
def inner(*args, **kwargs): added_kwargs = {} if 'user' in signature.parameters: added_kwargs['user'] = flask_login.current_user if 'user_service' in signature.parameters: added_kwargs['user_service'] = UserService( flask_login.current_user) return func(*args, **added_kwargs, **kwargs)
def get_status() -> Dict[str, Any]: device = get_current_user() user = device.user retval = { "lights": LightService(user.light_settings).lights_for_device(device), "messages": [m.value for m in user.messages], 'gauges': user.gauges } UserService(user).mark_device_visit(device) return retval