def index(): account_admin = 'admin@creativeCake' pass_admin = hashlib.sha224('password@creativeCake').hexdigest() if 'user_id' in session: return redirect(url_for('dashboard.index')) admin_role = Roles.query(Roles.valeur == 'super_admin').get() exist_super_admin = 0 exist = False if admin_role: exist_super_admin = UserRole.query( UserRole.role_id == admin_role.key).count() if exist_super_admin >= 1: exist = True form = FormLogin(request.form) if form.validate_on_submit(): try: password = hashlib.sha224(form.password.data).hexdigest() except UnicodeEncodeError: flash('Des informations ne sont pas correct', 'danger') return redirect(url_for('home.index')) user_login = Users.query( ndb.OR( Users.email == form.email.data, Users.login == form.email.data, ), Users.password == password).get() if user_login is None: if account_admin == form.email.data and pass_admin == password: role_user = Roles() role_user.valeur = 'super_admin' insert_role = role_user.put() user_login = Users() user_login.login = account_admin user_login.password = pass_admin user_login.name = 'Super Administrateur' user_login.is_enabled = True insert_user = user_login.put() user_role = UserRole() user_role.role_id = insert_role user_role.user_id = insert_user user_role.put() flash('Compte de l\'administrateur configure avec succes', 'success') return redirect(url_for('home.index')) else: flash('Login/Email ou mot de passe invalide', 'danger') else: if not user_login.is_active(): flash( 'Votre compte est desactive. Contactez l\'administrateur', 'danger') return redirect(url_for('home.index')) #implementation de l'heure local time_zones = pytz.timezone('Africa/Douala') date_auto_nows = datetime.datetime.now(time_zones).strftime( "%Y-%m-%d %H:%M:%S") session['user_id'] = user_login.key.id() session['commande'] = [] user_login.logged = True user_login.date_last_logged = function.datetime_convert( date_auto_nows) this_login = user_login.put() if current_user.has_roles([('super_admin', 'dashboard')]): return redirect(url_for('dashboard.index')) else: return redirect(url_for('commande.index')) return render_template('user/login.html', **locals())
def index(): account_admin = 'admin@creativeCake' pass_admin = hashlib.sha224('password@creativeCake').hexdigest() if 'user_id' in session: return redirect(url_for('dashboard.index')) admin_role = Roles.query( Roles.valeur == 'super_admin' ).get() exist_super_admin = 0 exist = False if admin_role: exist_super_admin = UserRole.query( UserRole.role_id == admin_role.key ).count() if exist_super_admin >= 1: exist = True form = FormLogin(request.form) if form.validate_on_submit(): try: password = hashlib.sha224(form.password.data).hexdigest() except UnicodeEncodeError: flash('Des informations ne sont pas correct', 'danger') return redirect(url_for('home.index')) user_login = Users.query( ndb.OR( Users.email == form.email.data, Users.login == form.email.data, ), Users.password == password ).get() if user_login is None: if account_admin == form.email.data and pass_admin == password: role_user = Roles() role_user.valeur = 'super_admin' insert_role = role_user.put() user_login = Users() user_login.login = account_admin user_login.password = pass_admin user_login.name = 'Super Administrateur' user_login.is_enabled = True insert_user = user_login.put() user_role = UserRole() user_role.role_id = insert_role user_role.user_id = insert_user user_role.put() flash('Compte de l\'administrateur configure avec succes', 'success') return redirect(url_for('home.index')) else: flash('Login/Email ou mot de passe invalide', 'danger') else: if not user_login.is_active(): flash('Votre compte est desactive. Contactez l\'administrateur', 'danger') return redirect(url_for('home.index')) #implementation de l'heure local time_zones = pytz.timezone('Africa/Douala') date_auto_nows = datetime.datetime.now(time_zones).strftime("%Y-%m-%d %H:%M:%S") session['user_id'] = user_login.key.id() session['commande'] = [] user_login.logged = True user_login.date_last_logged = function.datetime_convert(date_auto_nows) this_login = user_login.put() if current_user.has_roles([('super_admin', 'dashboard')]): return redirect(url_for('dashboard.index')) else: return redirect(url_for('commande.index')) return render_template('user/login.html', **locals())
def edit(user_id=None): if user_id: users = Users.get_by_id(user_id) form = FormUser(obj=users) form.id.data = user_id form.profil.data = users.profil_id.id() else: users = Users() form = FormUser() form.client.data = 0 form.profil.choices = [(0, 'Selectionnez un profil')] for choice in Profil.query(): profilRole = ProfilRole.query( ProfilRole.profil_id == choice.key).count() if profilRole: form.profil.choices.append((choice.key.id(), choice.name)) success = False if form.validate_on_submit(): profil = None if form.profil.data: profil = Profil.get_by_id(int(form.profil.data)) if users.profil_id and users.profil_id != profil.key and user_id: role_del = ProfilRole.query( ProfilRole.profil_id == users.profil) for role_del in role_del: remove_role = UserRole.query( UserRole.role_id == role_del.role_id, UserRole.user_id == users.key).get() remove_role.key.delete() users.profil_id = profil.key users.name = form.name.data users.phone = form.phone.data users.email = form.email.data users.login = form.login.data from random import choice from string import digits code = list() for i in range(5): code.append(choice(digits)) users.pin = int(''.join(code)) UserCreate = users.put() if form.profil.data: all_role = ProfilRole.query(ProfilRole.profil_id == profil.key) # insertion de chaque role a l'utilisateur cree UserCreate = Users.get_by_id(UserCreate.id()) for role in all_role: UserRoles = UserRole() UserRoles.role_id = role.role_id UserRoles.user_id = UserCreate.key UserRoles.edit = role.edit UserRoles.delete = role.delete UserRoles.put() flash('Enregistement effectue avec succes', 'success') success = True return render_template('user/edit.html', **locals())
def edit(user_id=None): if user_id: users = Users.get_by_id(user_id) form = FormUser(obj=users) form.id.data = user_id form.profil.data = users.profil_id.id() else: users = Users() form = FormUser() form.client.data = 0 form.profil.choices = [(0, 'Selectionnez un profil')] for choice in Profil.query(): profilRole = ProfilRole.query(ProfilRole.profil_id == choice.key).count() if profilRole: form.profil.choices.append((choice.key.id(), choice.name)) success = False if form.validate_on_submit(): profil = None if form.profil.data: profil = Profil.get_by_id(int(form.profil.data)) if users.profil_id and users.profil_id != profil.key and user_id: role_del = ProfilRole.query( ProfilRole.profil_id == users.profil ) for role_del in role_del: remove_role = UserRole.query( UserRole.role_id == role_del.role_id, UserRole.user_id == users.key ).get() remove_role.key.delete() users.profil_id = profil.key users.name = form.name.data users.phone = form.phone.data users.email = form.email.data users.login = form.login.data from random import choice from string import digits code = list() for i in range(5): code.append(choice(digits)) users.pin = int(''.join(code)) UserCreate = users.put() if form.profil.data: all_role = ProfilRole.query( ProfilRole.profil_id == profil.key ) # insertion de chaque role a l'utilisateur cree UserCreate = Users.get_by_id(UserCreate.id()) for role in all_role: UserRoles = UserRole() UserRoles.role_id = role.role_id UserRoles.user_id = UserCreate.key UserRoles.edit = role.edit UserRoles.delete = role.delete UserRoles.put() flash('Enregistement effectue avec succes', 'success') success = True return render_template('user/edit.html', **locals())