def upload(): user = Users.query.filter_by(username=session.get('username')).first_or_404() data = { 'user': user } if request.method == 'POST': upload_files = request.files.getlist('file[]') data_now = datetime.now().strftime("%Y%m%d%H%M%S") print(upload_files,data_now) for uploadfile in upload_files: upload_file(uploadfile) print(data_now,uploadfile) upload_logger.info("{0} upload file:{1} @{2}".format(session['username'].encode('utf-8'), uploadfile.filename,data_now)) return render_template('admin/misc/upload.html',**data)
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
def create_thread(tid): """Create a new thread.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): print("form", form.data) image_filename = upload_file(form["image"].data) thread = Thread( tale_id=tid, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) db.session.add(thread) db.session.commit() # Creates effects for thread in database # effects = createEffects(request.json["effects"], thread) # Creates choices for thread in database choices = createChoicesFromString(form["choices"].data, thread) # Create locks for choices in database # locks = createLocks(request.json["locks"], thread) return jsonify(thread=thread.to_dict(), choices=choices) else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_status(): """Create a new status""" form = StatusForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) # duration = form["duration"].data status = Status( 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, # duration=duration, ) # TODO Option to add status assets, meters, conditions? db.session.add(status) db.session.commit() return status.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_player(cid, uid): """Create a player character entity.""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( chronicle_id=cid, type="character", category="player", title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) # current_user.p_characters.append(entity) db.session.commit() # print("\n\nPC PC PC PC PC") # pprint(entity.to_dict()) return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_tale(tid): """Edit a tale.""" form = TaleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) tale = Tale.query.get(tid) tale.title = form["title"].data tale.description = form["description"].data tale.color = form["color"].data tale.icon = form["icon"].data tale.image = image_filename db.session.commit() return tale.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_choice(chid): """Edit a choice.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) choice = Choice.query.get(chid) choice.title = form["title"].data choice.prev_thread_id = form["prev_thread"].data choice.next_thread_id = form["next_thread"].data choice.color = form["color"].data choice.icon = form["icon"].data choice.image = image_filename db.session.commit() return choice.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_entity(eid): """Edit an entity.""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity.query.get(eid) entity.category = form["category"].data, entity.title = form["title"].data entity.description = form["description"].data entity.color = form["color"].data entity.icon = form["icon"].data entity.image = image_filename db.session.commit() return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_tale(cid): """Create a new tale that belongs to a chronicle""" form = TaleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) tale = Tale( chronicle_id=cid, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) db.session.add(tale) db.session.commit() return tale.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_choice(thid): """Create a choice.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) choice = Choice( title=form["title"].data, prev_thread_id=form["prev_thread"].data, next_thread_id=form["next_thread"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename ) db.session.add(choice) db.session.commit() return choice.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
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
def edit_meter(plid): """Edit a meter.""" form = MeterForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) meter = Meter.query.get(plid) meter.title = form["title"].data meter.description = form["description"].data meter.color = form["color"].data meter.image = image_filename meter.min = form["min"].data meter.max = form["max"].data meter.base = form["base"].data meter.mod = form["mod"].data meter.algo = form["algo"].data db.session.commit() return meter.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_status(sid): """Edit an status.""" form = StatusForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) # duration = form["duration"].data status = Status.query.get(sid) status.category = form["category"].data, status.title = form["title"].data status.description = form["description"].data status.color = form["color"].data status.icon = form["icon"].data status.image = image_filename # status.duration = duration db.session.commit() return status.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def edit_thread(thid): """Edit a thread.""" form = ThreadForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) thread = Thread.query.get(thid) thread.title = form["title"].data thread.description = form["description"].data thread.color = form["color"].data thread.icon = form["icon"].data thread.image = image_filename # updating choices existing_choices = Choice.query.filter(Choice.prev_thread_id == thread.id).all() existing_choice_ids = [c.id for c in existing_choices] editted_choice_ids = [c.id for c in request.json["choices"]] for choice in existing_choices: print("\n\nDELETE", choice) if choice.id not in editted_choice_ids: # deleting removed choices db.session.delete(choice) else: print("\n\nUPDATE", choice) # updating existing choices [matching_choice] = [c for c in request.json["choices"] if c["id"] and c["id"] == choice.id] choice.title = matching_choice["title"] choice.color = matching_choice["color"] choice.icon = matching_choice["icon"] choice.image = matching_choice["image"] request.json["choices"].remove(matching_choice) # adding new choices choices = createChoices(request.json["choices"], thread) print("\n\nCHOICES?", choices) db.session.commit() return jsonify(thread=thread.to_dict(), choices=choices) else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_entity(entity): """Create a new entity""" form = EntityForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) entity = Entity( user=current_user, type=entity, category=form["category"].data, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) # TODO Option to add entity assets, meters, conditions? db.session.add(entity) db.session.commit() return entity.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_meter(cid): """Create a new meter that belongs to a chronicle""" form = MeterForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) meter = Meter( title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, min=form["min"].data, max=form["max"].data, base=form["base"].data, mod=form["mod"].data, algorithm=form["algo"].data, ) db.session.add(meter) db.session.commit() return meter.to_dict() else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def create_chronicle(): """Create a new chronicle for a user.""" form = ChronicleForm() form["csrf_token"].data = request.cookies["csrf_token"] if form.validate_on_submit(): image_filename = upload_file(form["image"].data) chronicle = Chronicle( user_id=current_user.id, title=form["title"].data, description=form["description"].data, color=form["color"].data, icon=form["icon"].data, image=image_filename, ) tale = Tale(chronicle=chronicle, title=f"{chronicle.title}'s First Tale") thread = Thread( tale=tale, title=f"Untitled", description="Edit this scene and link threads with choices!") db.session.add(chronicle) db.session.add(tale) db.session.add(thread) db.session.commit() tale.first_thread_id = thread.id db.session.commit() return chronicle.to_dict() # jsonify( # chronicle={chronicle.id: chronicle.to_dict()}, # tale={tale.id: tale.to_dict()}, # thread={thread.id: thread.to_dict()} # ) else: return {"errors": validation_errors_to_messages(form.errors)}, 401
def update_patient(patient, form, files): """Update a patient record with information from submitted form.""" for field_name, class_name in [ ('income_sources', IncomeSource), ('phone_numbers', PhoneNumber), ('addresses', Address), ('emergency_contacts', EmergencyContact), ('household_members', HouseholdMember), ('employers', Employer), ('document_images', DocumentImage) ]: if form[field_name]: # If the last row in a many-to-one section doesn't have any data, don't save it if not bool([val for key, val in form[field_name][-1].data.iteritems() if ( val != '' and val is not None and key != 'id' and not (key in ['state', 'employee']) and not ( type(val) is FileStorage and val.filename == '' ) )]): form[field_name].pop_entry() # Add a new child object for each new item in a many-to-one section new_row_count = len(form[field_name].entries) - getattr(patient, field_name).count() if new_row_count > 0: for p in range(new_row_count): getattr(patient, field_name).append(class_name()) # When a user clicks the delete icon on a many-to-one row, it clears # all the data in that row. If any existing rows have no data, delete # them from patient object and then from the form. for row in form[field_name]: if not bool([val for key, val in row.data.iteritems() if ( val != '' and val is not None and key != 'id' and not (key in ['state', 'employee']) )]): row_index = int(row.name[-1]) # Delete from patient object db.session.delete(getattr(patient, field_name)[row_index]) # Deletion from form FieldList requires popping all entries # after the one to be removed, then readding them to_re_add = [] for _ in range(len(form[field_name].entries) - row_index): to_re_add.append(form[field_name].pop_entry()) to_re_add.pop() for row in to_re_add: form[field_name].append_entry(data=row.data) # Upload any new document images for index, entry in enumerate(form.document_images): if entry.file_name.data and entry.file_name.data.filename: entry.file_name.data = upload_file(entry.file_name.data) else: entry.file_name.data = patient.document_images[index].file_name # Populate the patient object with all the updated info form.populate_obj(patient) return