def update_localisation(): # handle authetication problems handle_authentication(False) form = Users.forms.Localisation() fields = extract_fields(form) if request.method == 'GET': # pre fill all fields for field in fields: k = getattr(form, field) k.data = getattr(current_user, field) else: if form.validate_on_submit(): # if needed add the town to Town table if form.town.data not in Town.list_all(): form.town.data = form.town.data.capitalize()\ .strip().replace(" ", " ").replace(" ", " ") db.session.add(Town(town=form.town.data)) # update User attrs for field in fields: setattr(current_user, field, getattr(form, field).data) return commit_flash_redirect('users.manage_user') else: flash_all_errors(form) return render_template('user/update/update_localisation.html', title="Localisation", form=form, fields=fields, country_list=Country.list_all(), town_list=Town.list_all())
def create_search(): # handle connections problems handle_authentication(False) # form and fields form = SearchsForms.Create() # fields = extract_fields(form) if request.method == 'GET': pass else: if form.validate_on_submit(): # if needed add the town to Town table for i in (1, 2, 3, 4, 5): town = getattr(form, f"town_{i}").data if town not in Town.list_all(): town = town.capitalize()\ .strip().replace(" ", " ").replace(" ", " ") db.session.add(Town(town=town)) search = Search(user_id=current_user.id) # upadte user attrs search.name = form.name.data search.mob = form.mob.data for feat in ["country", "departement", "region", "town"]: _data = [ getattr(form, f"{feat}_{i}").data for i in [1, 2, 3, 4, 5] ] setattr(search, feat, ", ".join([i for i in _data if i])) form.key_words.data = form.key_words.data.strip().upper()\ .replace("-", " ").replace(" ", " ").replace(" ", " ") if form.key_words.data: kwords = form.key_words.data kwords = kwords.lower().strip()\ .replace("-", " ").replace(" ", " ").replace(" ", " ") kwords = ", ".join([i.strip() for i in kwords.split(",")]) form.key_words.data = kwords search.key_words = form.key_words.data db.session.add(search) db.session.commit() flash("search updated", "success") return redirect( url_for("searchs.manage_search", search_id=search.id)) else: flash_all_errors(form) form.submit.label.text = "Create" return render_template('search/create_search.html', title="Create Search", mode="Create", form=form, country_list=Country.list_all(), town_list=Town.list_all(), departement_list=Departement.list_all(), region_list=Region.list_all())
def validate_country(self, country): Validator.required(country) if country.data not in Country.list_all(): raise ValidationError('Ce pays est inconnu')
def validate_nationality(self, nationality): Validator.required(nationality) if nationality.data not in Country.list_all(): raise ValidationError('Ce pays est inconnu')
def country(country): if country.data not in Country.list_all(): raise ValidationError("Ce pays n'existe pas")
def update_search(search_id): # handle connections problems handle_authentication(False) # form and fields form = SearchsForms.Update() search = Search.query.filter_by(id=search_id).first() # authetication problem if current_user.id != search.user_id: flash("sorry this is not your search, you are not allowed", "danger") return redirect(url_for("main.edhunt")) if request.method == 'GET': # prefill the form form.name.data = search.name form.mob.data = search.mob for feat in ['country', 'town', 'departement', 'region']: if getattr(search, feat) is None: setattr(search, feat, "") _data = [i.strip() for i in getattr(search, feat).split(",")] _data = _data + ([ "", ] * (5 - len(_data))) [ setattr(getattr(form, f"{feat}_{i+1}"), "data", j) for i, j in enumerate(_data) ] form.key_words.data = search.key_words else: if form.validate_on_submit(): # if needed add the town to Town table for i in (1, 2, 3, 4, 5): town = getattr(form, f"town_{i}").data if town not in Town.list_all(): town = town.capitalize()\ .strip().replace(" ", " ").replace(" ", " ") db.session.add(Town(town=town)) # upadte user attrs search.name = form.name.data search.mob = form.mob.data for feat in ["country", "departement", "region", "town"]: _data = [ getattr(form, f"{feat}_{i}").data for i in [1, 2, 3, 4, 5] ] setattr(search, feat, ", ".join([i for i in _data if i])) form.key_words.data = form.key_words.data.strip().upper()\ .replace("-", " ").replace(" ", " ").replace(" ", " ") if form.key_words.data: kwords = form.key_words.data kwords = kwords.lower().strip()\ .replace("-", " ").replace(" ", " ").replace(" ", " ") kwords = ", ".join([i.strip() for i in kwords.split(",")]) form.key_words.data = kwords search.key_words = form.key_words.data db.session.commit() flash("search updated", "success") return redirect( url_for("searchs.manage_search", search_id=search.id)) else: flash_all_errors(form) form.submit.label.text = "Update" return render_template('search/update/update_search.html', title="Update Search", mode="Update", form=form, country_list=Country.list_all(), town_list=Town.list_all(), region_list=Region.list_all(), departement_list=Departement.list_all(), search_id=search.id)