def addSession(form): if not checkKeys(form, ['email', 'password']): return msg.errMsg("Please check the inputs.") sessions = SessionModel() users = UserModel() (email, password) = (form['email'], form['password']) usersFounded = users.getUser(email=email, password=password, enable=True) if usersFounded == None: return msg.errMsg("Failed to validate user information.") if len(usersFounded) == 0: return msg.errMsg("Invalid email address or password.") uid = usersFounded[0][0] newSessionID = sessions.createSessionID() startTime = datetime.utcnow() startSessionResult = sessions.startSession(newSessionID, uid, startTime) if startSessionResult == None: return msg.errMsg("Failed to start a new session.") return msg.successMsg({ "sessionID": newSessionID, "uid": uid, "startTime": str(startTime) })
def pontoAberto(user_pin): user_id = UserModel.get_id_by_pin(user_pin) ponto = DB().run_fr( "SELECT inicio FROM ponto WHERE user_id = {} AND fim is NULL;". format(user_id)) if ponto is None: return False return True
def baterPonto(user_pin, tipo): user_id = UserModel.get_id_by_pin(user_pin) if tipo == "entrar": DB().run( "INSERT INTO ponto (user_id) VALUES ({});".format(user_id)) else: DB().run( "UPDATE ponto SET fim = CURRENT_TIMESTAMP WHERE user_id = {} AND fim IS NULL;" .format(user_id))
def index(): if "user_pin" in session: user_pin = session["user_pin"] user = UserModel.get(user_pin) if user: ponto_aberto = PontoModel().pontoAberto(user_pin) return render_template("ponto.html", user=user, ponto_aberto=ponto_aberto) return redirect(url_for("login"))
def fetch_one_by_email_pwd(self, email, pwd): cursor = self.db_conn.connect() sql = """select nid ,username,email,last_login,vip,user_type from UserInfo WHERE email=%s AND pwd=%s""" cursor.execute(sql, (email, pwd)) db_result = cursor.fetchone() self.db_conn.close() if db_result: obj = UserModel(nid=db_result['nid'], username=db_result['username'], email=db_result["email"], last_login=["last_login"], vip_obj=VipModel(db_result["vip"]), user_type_obj=UserTypeModel( db_result['"user_type'])) return obj
def fetch_all_by_user_type(self, *args, **kwargs): ret = None cursor = self.db_conn.connect() sql = """select nid,username,email,last_login,vip,user_type from userinfo where user_type=1""" cursor.execute(sql) db_result = cursor.fetchall() self.db_conn.close() if db_result: ret = UserModel( nid=db_result['nid'], username=db_result['username'], email=db_result['email'], last_login=db_result['last_login'], user_type_obj=UserType(nid=db_result['user_type']), vip_type_obj=VipType(nid=db_result['vip']), ) return ret return db_result
def fetch_one_by_user(self, username, password): ret = None cursor = self.db_conn.connect() sql = """select nid,username,password,email,last_login,vip,user_type from userinfo where username=%s and password=%s""" cursor.execute(sql, (username, password)) db_result = cursor.fetchone() self.db_conn.close() if db_result: ret = UserModel( nid=db_result['nid'], username=db_result['username'], password=db_result['password'], email=db_result['email'], last_login=db_result['last_login'], user_type_obj=UserType(nid=db_result['user_type']), vip_type_obj=VipType(nid=db_result['vip']), ) return ret return db_result
def fetch_customer_by_page(self, start, offset): ret = None cursor = self.db_conn.connect() sql = """select nid,username,email,vip,user_type,password,last_login from userinfo order by nid desc limit %s offset %s """ cursor.execute(sql, (offset, start)) db_result = cursor.fetchall() self.db_conn.close() customer_obj = [] if db_result: for item in db_result: ret = UserModel( nid=item['nid'], username=item['username'], password=item['password'], email=item['email'], last_login=item['last_login'], user_type_obj=UserType(nid=item['user_type']), vip_type_obj=VipType(nid=item['vip']), ) customer_obj.append(ret) return customer_obj return db_result