def registration_email(): emailForm = EmailForm() if emailForm.validate_on_submit(): sleep(3) if request.form.getlist('conditionsAccept') != ['1', '2']: flash(f'Je potřeba souhlasit s podmínkami.', FlashStyle.Danger) return render_template("registrace_email.html", form=emailForm) if DBAccess.ExecuteScalar( 'select id from users where trim(email) ilike %s', (emailForm.email.data.strip(), )) is not None: flash( f'Uživatel {emailForm.email.data} je již zaregistrován, zvolte jiný email.', FlashStyle.Danger) emailForm.email.data = None return render_template("registrace_email.html", form=emailForm) else: ts = URLSafeTimedSerializer(app.config["SECRET_KEY"]) token = ts.dumps(emailForm.email.data.strip(), salt='email-confirm-key') confirm_url = url_for('login_bp.email_confirmation', token=token, _external=True) email_text = f'Prosím klikněte na následující odkaz pro ověření vašeho emailu a pokračování v registraci.<br>Tento odkaz bude platný následujících 24 hodin.<br>{confirm_url} <br> Pokud odkaz nefunguje, prosíme, je nutné ho zkopírovat a celý vložit do vašeho prohlížeče.' SendMail(GetEmail('noreplyMail'), emailForm.email.data.strip(), 'Seniore.org - ověření emailu', email_text) #flash("Na zadanou adresu byl odeslán email s odkazem na pokračování v registraci.",FlashStyle.Success) emailForm.submit.label.text = "Odeslat ověřovací email znovu" return render_template("registrace_email2.html", form=emailForm) return render_template("registrace_email.html", form=emailForm)
def lost_password(): emailForm = EmailForm() if emailForm.validate_on_submit(): sleep(3) if DBAccess.ExecuteScalar('select id from users where email ilike %s', (emailForm.email.data, )) is None: flash( f'Uživatel {emailForm.email.data} nebyl nalezen, zvolte jiný email.', FlashStyle.Danger) emailForm.email.data = None return render_template("registrace_email.html", form=emailForm) else: ts = URLSafeTimedSerializer(app.config["SECRET_KEY"]) token = ts.dumps(emailForm.email.data, salt='email-renew-key') confirm_url = url_for('login_bp.new_password', token=token, _external=True) email_text = f'Prosím klikněte na následující odkaz pro zadání nového hesla.<br>Tento odkaz bude platný následujících 24 hodin.<br>{confirm_url}' SendMail(GetEmail('noreplyMail'), emailForm.email.data, 'Seniore.cz - obnova zapomenutého hesla', email_text) flash( "Na zadanou adresu byl odeslán email s odkazem na obnovu hesla.", FlashStyle.Success) emailForm.submit.label.text = "Odeslat email znovu" return render_template("lost_password.html", form=emailForm) return render_template("lost_password.html", form=emailForm)
def registrace(): form = RegistrationForm() if form.validate_on_submit(): if (form.password.data != form.passwordAgain.data): flash('Hesla nejsou stejná.', FlashStyle.Danger) email = session['confirmed_email'] form.email.data = email return render_template("registrace.html", form=form) email = session['confirmed_email'] form.email.data = email dbUser = DBUser() dbUser.email = form.email.data dbUser.password = form.password.data dbUser.level = 0 if DBAccess.ExecuteScalar('select id from users where email=%s', (dbUser.email, )) is not None: flash( f'Uživatel {dbUser.email} je již zaregistrován, zvolte jiný email.', FlashStyle.Danger) dbUser.email = None form.email.data = None return render_template("registrace.html", form=form) dbUser.salt = salt = DBAccess.ExecuteScalar("select salt()") #md% tranform password use md5 function on password + salt # md5Pass = hashlib.md5((dbUser.password+dbUser.salt).encode()).hexdigest() # dbUser.password = md5Pass bcrypt = Bcrypt() dbUser.password = bcrypt.generate_password_hash( dbUser.password).decode('UTF-8') dbUser.SaveToSession('dbUserRegistration') return redirect(url_for("login_bp.registrace_name")) #email = session.pop('confirmed_email',None) email = session.get('confirmed_email') if (email is None): abort(403) form.email.data = email return render_template("registrace.html", form=form)
def registrace_address(): form = RegistrationFormAddress() if form.validate_on_submit(): dbUser = DBUser.LoadFromSession('dbUserRegistration') dbUser.town = form.town.data dbUser.street = form.street.data # dbUser.street_number = form.street_number.data dbUser.post_code = form.post_code.data kwargs = dbUser.__dict__ address = "{} {} {}".format(kwargs["street"], kwargs["town"], kwargs["post_code"]) # address = "{} {} {} {}".format(kwargs["street"], kwargs["street_number"], kwargs["town"], kwargs["post_code"]) coordinates = GetCoordinates(address) if (coordinates is not None): # dbUser.latitude = coordinates[0] # dbUser.longitude = coordinates[1] dbUser.latitude = round(coordinates[0], 5) dbUser.longitude = round(coordinates[1], 5) x = 1 y = 1 difference = 0.00001 originalLatitude = dbUser.latitude originalLongitue = dbUser.longitude #check if same coordinates already exists while DBAccess.ExecuteScalar( 'select id from users where latitude=%s and longitude=%s', ( dbUser.latitude, dbUser.longitude, )) is not None: #if exists add difference and try again and again.. dbUser.latitude = originalLatitude + x * difference dbUser.longitude = originalLongitue + y * difference if x != -1: x -= 1 elif y != -1: y -= 1 else: x = 1 y = 1 difference += 0.00001 else: flash('Nenalezeny souřadnice pro vaši adresu', FlashStyle.Danger) return render_template("registrace_address.html", form=form) dbUser.SaveToSession('dbUserRegistration') #return redirect(url_for("login_bp.photo")) return redirect(url_for("login_bp.registrace_idCard")) return render_template("registrace_address.html", form=form)
def sluzby_update(): services = DBAccess.ExecuteSQL("select * from services") form = regFormBuilder( services ) # put all services to form, but I need to display it - by for cycle below form.checkBoxes.clear() # not to have duplicates on website form.checkBoxes = [] for index in form.checkBoxIndexes: form.checkBoxes.append( getattr(form, "checkbox%d" % index) ) # displaying checkboxes on # set all existing services with checked button, to be developed # for checkbox in form.checkBoxes: # existing_services = DBAccess.ExecuteScalar( # "select * from users_services where id_users=%s and " # "id_services=%s and id_demand_offer=%s", # (nextId, checkbox.id, form.demandOffer.data), # ) # if service in existing_services: # checkbox.data = True if form.validate_on_submit(): # if validated, save in db nextId = session["id_user"] services_checked = [] for index in form.checkBoxIndexes: checkbox = getattr(form, "checkbox%d" % index) if checkbox.data: existing_combination = DBAccess.ExecuteScalar( "select count(*) from users_services where id_users=%s and " "id_services=%s and id_demand_offer=%s", (nextId, checkbox.id, form.demandOffer.data), ) text = DictionaryDemandOffer.get( form.demandOffer.data, "unknown").lower() if existing_combination == 0: flash( f'Zadaná kombinace {session["user"]}, {text} a {checkbox.label.text} neexistuje.' ) else: DBAccess.ExecuteUpdate( "delete from users_services where id_users = %s and id_services = %s and id_demand_offer= %s", (nextId, checkbox.id, form.demandOffer.data), ) services_checked.append(checkbox.label) kwargs = { "demand_offer": DictionaryDemandOffer.get(form.demandOffer.data, "unknown"), "category": services_checked, } # return redirect(url_for("overview_bp.prehled_all")) return render_template("sluzby_success.html", **kwargs) return render_template("sluzby_update.html", form=form)
def remove_service(): id = request.args.get("id", type=int) #check if there was argument if (id is None): abort(403) #check if service belongs to logged user.. dbUser = DBUser.LoadFromSession('dbUser') user_service = DBAccess.ExecuteScalar( "select id from users_services where id = %s and id_users=%s", (id, dbUser.id)) if (user_service is None): abort(403) #delete service DBAccess.ExecuteUpdate("delete from users_services where id=%s", (id, )) return redirect(url_for("profile_bp.profil"))
def sluzby_upload(): services = DBAccess.ExecuteSQL("select * from services") form = regFormBuilder( services ) # put all services to form, but I need to display it - by for cycle below form.checkBoxes.clear() # not to have duplicates on website for index in form.checkBoxIndexes: form.checkBoxes.append( getattr(form, "checkbox%d" % index) ) # displaying checkboxes on website if form.validate_on_submit(): # if validated, save in db nextId = session["id_user"] services_checked = [] for index in form.checkBoxIndexes: checkbox = getattr(form, "checkbox%d" % index) if checkbox.data: # for every checked services in form, save.. existing_combination = DBAccess.ExecuteScalar( "select count(*) from users_services where id_users=%s and " "id_services=%s and id_demand_offer=%s", (nextId, checkbox.id, form.demandOffer.data), ) text = DictionaryDemandOffer.get( form.demandOffer.data, "unknown" ).lower() if existing_combination > 0: flash( f'Zadaná kombinace {session["user"]}, {text} a {checkbox.label.text} již existuje.', FlashStyle.Danger ) else: DBAccess.ExecuteInsert( "insert into users_services " "(id_users, id_services, id_demand_offer) values (%s, %s, %s)", (nextId, checkbox.id, form.demandOffer.data), ) services_checked.append(checkbox.label) kwargs = { "demand_offer": DictionaryDemandOffer.get(form.demandOffer.data, "unknown"), "category": services_checked, } return render_template("sluzby_success.html", **kwargs) return render_template("sluzby.html", form=form)
def senior_registration(): form = SeniorRegistrationForm() if (form.validate_on_submit()): if form.password.data != form.passwordAgain.data: flash('Hesla nejsou totožná!', FlashStyle.Danger) return render_template('senior_registration.html', form=form) if DBAccess.ExecuteScalar( 'select id from users where trim(email) ilike %s', (form.email.data.strip(), )) is not None: flash( f'Uživatel {form.email.data} je již zaregistrován, zvolte jiný email.', FlashStyle.Danger) form.email.data = None return render_template('senior_registration.html', form=form) dbUser = DBUser() dbUser.first_name = form.first_name.data dbUser.surname = form.surname.data dbUser.email = form.email.data dbUser.telephone = form.telephone.data dbUser.telephone2 = form.telephone2.data dbUser.comment = form.comment.data dbUser.street = form.street.data dbUser.street_number = form.street_number.data if (form.street_number.data == ''): dbUser.street_number = 1 dbUser.town = form.town.data dbUser.post_code = form.post_code.data dbUser.password = form.password.data bcrypt = Bcrypt() dbUser.password = bcrypt.generate_password_hash( dbUser.password).decode('UTF-8') dbUser.level = 1 address = '{} {} {}'.format(dbUser.street, dbUser.town, dbUser.post_code) coordinates = GetCoordinates(address) if (coordinates is not None): dbUser.latitude = round(coordinates[0], 5) dbUser.longitude = round(coordinates[1], 5) x = 1 y = 1 difference = 0.00001 originalLatitude = dbUser.latitude originalLongitue = dbUser.longitude #check if same coordinates already exists while DBAccess.ExecuteScalar( 'select id from users where latitude=%s and longitude=%s', ( dbUser.latitude, dbUser.longitude, )) is not None: #if exists add difference and try again and again.. dbUser.latitude = originalLatitude + x * difference dbUser.longitude = originalLongitue + y * difference if x != -1: x -= 1 elif y != -1: y -= 1 else: x = 1 y = 1 difference += 0.00001 else: flash('Nenalezeny souřadnice pro vaši adresu', FlashStyle.Danger) return render_template('senior_registration.html', form=form) dbUser.salt = salt = DBAccess.ExecuteScalar("select salt()") dbUser.id = DBAccess.GetSequencerNextVal('users_id_seq') dbUser.InsertDB() flash( f'Senior {dbUser.first_name} {dbUser.surname} email: {dbUser.email} vložen do databáze a nastaven jako ověřený.', FlashStyle.Success) return redirect(url_for("login_bp.login")) return render_template('senior_registration.html', form=form)
def profil(): dbUser = DBAccess.GetDBUserById(session["id_user"]) name = f'{dbUser.first_name} {dbUser.surname}' info = dbUser.info mail = dbUser.email phone = dbUser.telephone latitude = str( DBAccess.ExecuteScalar("select latitude from users where id = %s", (session["id_user"], ))) longitude = str( DBAccess.ExecuteScalar("select longitude from users where id = %s", (session["id_user"], ))) username = session["user"] imgCloudUrl = GetImageUrl(session["id_user"]) if request.method == "GET": users_services = DBAccess.ExecuteSQL( "select s.category as category, d.demand_offer as demand_offer,us.id from users_services us" " left join users u on us.id_users = u.id" " left join services s on s.id = us.id_services" " left join demand_offer d on d.id = us.id_demand_offer where u.id = %s", (session["id_user"], )) addServiceText = 'Přidat službu' if (users_services is None): users_services = [] addServiceText = 'Zobrazit mě na mapě' sndmap = Map( identifier="sndmap", style="height:100%;width:100%;margin:0;", lat=latitude, lng=longitude, report_clickpos=True, clickpos_uri="/clickpost/", markers=[{ "icon": "https://maps.google.com/mapfiles/kml/pal2/icon10.png", "lat": latitude, "lng": longitude, "infobox": f"<b>{username}</b><img class=img_mapa src= {imgCloudUrl} />" }]) requests = DBAccess.ExecuteSQL( """select ud.first_name, ud.surname, ud.email, ud.telephone, uo.first_name, uo.surname, uo.email, uo.telephone, s.category, to_char(r.date_time, 'DD-MM-YYYY HH24:MI'), r.add_information, r.timestamp, rs.status, r.id from requests r inner join services s on r.id_services = s.id inner join users ud on r.id_users_demand = ud.id inner join users uo on r.id_users_offer = uo.id inner join requests_status rs on r.id_requests_status = rs.id where ud.id = %s or uo.id =%s """, (session["id_user"], session["id_user"])) if requests == None: requests = [] return render_template("profil.html", users_services=users_services, nazev=imgCloudUrl, sndmap=sndmap, requests=requests, name=name, info=info, mail=mail, phone=phone, addServiceText=addServiceText)
def prehled_all(): services = DBAccess.ExecuteSQL("select * from services order by pageorder") onMap = DBAccess.ExecuteScalar( 'select 1 from users_services where id_users = %s', (session['id_user'], )) != None checked_services_id_list = [] for service in services: checked_services_id_list.append(service[0]) form = regFormBuilder( services ) # put all services to form, but I need to display it - by for cycle below form.checkBoxes.clear() form.demandOffer.choices = [('2', 'poskytují pomoc'), ('1', 'potřebují pomoc')] for index in form.checkBoxIndexes: checkbox = getattr(form, "checkbox%d" % index) if (request.method == 'GET'): checkbox.data = True form.checkBoxes.append(getattr( form, "checkbox%d" % index)) # displaying checkboxes on website if form.validate_on_submit(): checked_services_id_list = [] for index in form.checkBoxIndexes: checkbox = getattr(form, "checkbox%d" % index) if checkbox.data: checked_services_id_list.append(checkbox.id) result = DBAccess.ExecuteSQL( ''' SELECT u.id, u.first_name, u.surname, u.info, d.demand_offer, u.latitude, u.longitude, us.id,s.category FROM users u LEFT JOIN users_services us on us.id_users = u.id LEFT JOIN services s on s.id = us.id_services LEFT JOIN demand_offer d on d.id = us.id_demand_offer WHERE d.id = %s AND us.id_services in %s ORDER BY u.id, us.id ''', (int(form.demandOffer.data), tuple(checked_services_id_list))) if result is None: result = [] flash( "Bohužel v systému nejsou zadány žádné služby dle vašeho filtru.") usersCatDict = {} for row in result: key = list(row) key = tuple(key[:7]) # key = tuple(list(row)[:7]) value = row[-2:] if (key in usersCatDict): usersCatDict[key].append(value) else: usersCatDict[key] = [value] demandOfferText = 'nabízené' mapIconUrl = "https://maps.google.com/mapfiles/ms/icons/green-dot.png" if (int(form.demandOffer.data) == 1): demandOfferText = 'poptávané' mapIconUrl = "https://maps.google.com/mapfiles/ms/icons/blue-dot.png" dbUser = DBAccess.GetDBUserById(session['id_user']) # markery pro kazdeho vyhledaneho markers = [] marker = {} marker["icon"] = "https://maps.google.com/mapfiles/kml/pal2/icon10.png" marker["lat"] = str(dbUser.latitude) marker["lng"] = str(dbUser.longitude) marker[ "infobox"] = f'<b>{dbUser.first_name} </b><br>{dbUser.info}<img class=img_mapa src= {GetImageUrl(dbUser.id)} />' markers.append(marker) for user in usersCatDict.keys(): servicesHTML = '<ul>' for service in usersCatDict[user]: servicesHTML = servicesHTML + f'<li> {service[1]} <a href="/match?id={service[0]}">Kontaktovat</a></li>' servicesHTML = servicesHTML + '</ul>' pictureUrl = GetImageUrl(user[0]) marker = {} marker["icon"] = mapIconUrl marker["lat"] = str(user[5]) marker["lng"] = str(user[6]) marker["infobox"] = f'''<b>{user[1]} </b><p>{user[3]}</p>\ <img class=img_mapa src= {pictureUrl} /><br>\ <p>{demandOfferText} služby: {servicesHTML}</p>''' markers.append(marker) map = Map( identifier="sndmap", style="height:100%;width:100%;margin:0;", lat=str(dbUser.latitude), lng=str(dbUser.longitude), markers=markers ) # get map, zoom on location of actual user, insert markers from select, ie users who provide specific required service return render_template("prehled_all.html", sndmap=map, form=form, onMap=onMap)