def update(service_id): """Update Servicees""" service = Service.query.get(service_id) if service is None: # return error message return {"error": "Error when updating an service"} form = ServiceForm(obj=request.json, prefix="service") form.category_ids.choices = CategoryHandler.list_for_select() if form.validate(): service.name = form.name.data service.description = form.description.data service.is_active = form.is_active.data service.updated = datetime.datetime.now() category_ids = form.category_ids.data service.set_categoiry_ids(category_ids) db.session.commit() # upload the image ServiceHandler.upload(service, form) # success, return new item return {"item": service.serialize()} # return form errors return {"errors": form.errors}
def users_get(username): """Get user information detail""" account = User.query.get_or_404(username) g.global_values["CURRENT_USERNAME"] = account.username service_form = ServiceForm(prefix="service") service_form.category_ids.choices = CategoryHandler.list_for_select() #Service urls g.global_values["SERVICE_LIST_URL"] = f'{url_for("api.services_list_mine")}?username={account.username}' g.global_values["SERVICE_INSERT_URL"] = f'{url_for("api.services_insert")}?username={account.username}' g.global_values["SERVICE_UPDATE_URL"] = f'{url_for("api.services_update" , service_id=0)}?username={account.username}' g.global_values["SERVICE_DELETE_URL"] = f'{url_for("api.services_delete" , service_id=0)}?username={account.username}' schedule_form = ScheduleForm(prefix="schedule") appointment_form = AppointmentForm(prefix="appointment") g.global_values["APPOINTMENT_LIST_URL"] = f'/api/appointments/{account.username}{"?is_provider=1" if account.is_provider else ""}' g.global_values["APPOINTMENT_UPDATE_URL"] = '/api/appointments/0' g.global_values["APPOINTMENT_DELETE_URL"] = '/api/appointments/0' return render_template('admin/users/get.html', account=account, service_form=service_form, schedule_form=schedule_form, appointment_form=appointment_form)
def categories_list(): """Show all the categories""" items = CategoryHandler.list( only_active=(False if login_admin_username() is not None else True)) return jsonify(items=[item.serialize() for item in items])
def categories_insert(): """New Category""" item = CategoryHandler.insert() if "item" in item: return (jsonify(item), 201) return (jsonify(item), 200)
def my_services(): """Get all my services""" if not g.user.is_provider: flash("Unauthorized visit!", FLASH_GROUP_DANGER) return redirect(url_for("home.dashboard")) form = ServiceForm(prefix="service") form.category_ids.choices = CategoryHandler.list_for_select() return render_template("home/services/list.html", form=form)
def services_list(): """Service List""" service_form = ServiceSearchForm() service_form.categories.choices.extend(CategoryHandler.list_for_select()) service_url = "/services" service_form.term.data = request.args.get("term", "") service_form.categories.data = request.args.get("categories", "0") appointment_form = AppointmentForm(prefix="appointment") return render_template("home/services.html", service_url=service_url, service_form=service_form, appointment_form=appointment_form)
def insert(username): """New Service""" form = ServiceForm(obj=request.json, prefix="service") form.category_ids.choices = CategoryHandler.list_for_select() if form.validate(): name = form.name.data description = form.description.data is_active = form.is_active.data category_ids = form.category_ids.data service = Service( username=username, name=name, description=description, is_active=is_active, updated=datetime.datetime.now(), created=datetime.datetime.now() ) db.session.add(service) try: db.session.commit() except: db.session.rollback() # return error message return {"error": "Error when adding an service"} # append the categories if len(category_ids) > 0: service.set_categoiry_ids(category_ids) try: db.session.commit() except: db.session.rollback() ServiceHandler.upload(service, form) # success, return new item return {"item": service.serialize()} # return form errors return {"errors": form.errors}
def index(): # Do some stuff service_form = ServiceSearchForm() service_form.categories.choices.extend(CategoryHandler.list_for_select()) service_url = "/services" provider_form = BaseSearchForm() provider_url = "/providers" appointment_form = AppointmentForm(prefix="appointment") return render_template("home/index.html", service_url=service_url, service_form=service_form, provider_url=provider_url, provider_form=provider_form, appointment_form=appointment_form)
def dashboard(): """Get user dashboard""" g.global_values["PROVIDER_USERNAME"] = g.user.username g.global_values[ "APPOINTMENT_LIST_URL"] = f'/api/appointments/{ g.user.username }?{"is_provider=1" if g.user.is_provider else ""}' g.global_values["APPOINTMENT_UPDATE_URL"] = '/api/appointments/0' g.global_values["APPOINTMENT_DELETE_URL"] = '/api/appointments/0' g.global_values["APPOINTMENT_PER_PAGE"] = '6' appointment_form = AppointmentForm(prefix="appointment") service_form = ServiceForm(prefix="service") service_form.category_ids.choices = CategoryHandler.list_for_select() return render_template("home/users/dashboard.html", account=g.user, appointment_form=appointment_form, service_form=service_form)
def categories_delete(category_id): """Delete category""" return (jsonify(CategoryHandler.delete(category_id)), 200)
def categories_update(category_id): """Edit Category""" item = CategoryHandler.update(category_id) return (jsonify(item), 200)