def create_users_and_products(self): ''' Create a dummy user and persist it. Fetch it from the database and verify that the identity map holds Check that the id is created ''' session = self.session user1 = User(id="TestUser1", password="******") user2 = User(id="TestUser2", password="******") products = [Product(id=_id) for _id in ["SQUASHCITY", "SUBTITLES"]] prod_user1, prod_user2 = products, products[1:] user1.products.extend(prod_user1) user2.products.extend(prod_user2) session.add(user1) session.add(user2) session.commit() q_products = [ user_prod.product for user_prod in session.query(UserProduct).join(User).filter( User.id == "TestUser1").order_by(UserProduct.product_id).all() ] self.assertEqual(prod_user1, q_products) q_products = [ user_prod.product for user_prod in session.query(UserProduct).join(User).filter( User.id == "TestUser2").order_by(UserProduct.product_id).all() ] self.assertEqual(prod_user2, q_products)
def register(): # should do more validation here! try: vorname = request.form['vorname'] nachname = request.form['nachname'] geb = parse_date(request.form['geburtstag']) except Exception as e: raise e return "Unable to parse form...", 500 # check date in range if geb.year not in range(1890, 2020): return "Unrealistisches Geburtsjahr", 400 # look for unique username in db requested_usrname = vorname + nachname + unicode(geb.date()) usr = User.query.filter_by(username=requested_usrname).first() if usr is None: # register new user try: new_user = User(vorname, nachname, geb) db.session.add(new_user) db.session.commit() except: return "Unable to save " + unicode( new_user) + " to database...", 503 # initial score of 0 new_user.score = 0 return new_user.make_json_data(), 201 else: # return existing user info return usr.make_json_data(), 200
def test_user_model(self): user = User() user.video_affinity = 0.07 user.text_affinity = 0.03 user.game_affinity = 0.01 user.put() db_user = User.query().get() self.assertEquals(0.07, db_user.video_affinity) self.assertEquals(0.03, db_user.text_affinity) self.assertEquals(0.01, db_user.game_affinity)
def authenticate(self, username, password): active = User.select().where(User.active == True) try: user = active.where(User.username == username).get() except User.DoesNotExist: return False else: if not check_password(password, user.password): return False return user
def get_logged_in_user(self): if session.get('logged_in'): if getattr(g, 'user', None): return g.user try: return User.select().where( User.active == True, User.id == session.get('user_pk')).get() except User.DoesNotExist: pass
def get(self, *args, **kwargs): username = self.get_current_user() user = User.objects(name=eval(username)).get() if not user.type == 'admin': self.render('index.html',info = 'not_admin') else: dev = user.dev time_t = [time.strftime('%H:%M',time.localtime(i.makeTime)) for i in dev.temperatures[-(setting.web['last']):]] temp = [i.temperature for i in dev.temperatures[-(setting.web['last']):]] time_h = [time.strftime('%H:%M',time.localtime(i.makeTime)) for i in dev.humiditys[-(setting.web['last']):]] humi = [i.humidity for i in dev.humiditys[-(setting.web['last']):]] self.render('index.html',info = zip(time_t,temp,time_h,humi),deviceId=dev.deviceId,username=username)
def post(self): username = self.get_argument("username") password = self.get_argument("password") try: user = User.objects(name=username).get() if user.password == password: self.set_current_user(username) self.write('11') else: self.write('E01') except: self.write('E00')
def get(self, *params, **kwargs): if not len(params) == 2: self.write('E02') else: username,deviceId = params try: user = User.objects(name=username).get() dev = user.dev temp = dev.temperatures g = lambda x,y:x*100+y temp = dict(zip([reduce(g,time.localtime(i.makeTime)[-8:-4]) for i in temp[-(setting.web['last']):]],[i.temperature for i in temp[-(setting.web['last']):]])) self.write(temp) except Exception,e: self.write('E00')
def get(self, *params, **kwargs): if not len(params) == 2: self.write('E02') else: username,deviceId = params try: user = User.objects(name=username).get() dev = user.dev humi = dev.humiditys g = lambda x,y:x*100+y humi = dict(zip([reduce(g,time.localtime(i.makeTime)[-8:-4]) for i in humi[-(setting.web['last']):]],[i.humidity for i in humi[-(setting.web['last']):]])) print humi self.write(humi) except Exception,e: self.write('E00')
def get(self, *params, **kwargs): if not len(params) == 2: self.write('E02') else: username,deviceId = params try: user = User.objects(name=username).get() dev = user.dev coAlert = dev.coAlert chAlert = dev.chAlert guardAlert = dev.guardAlert alert = map(lambda x:'1' if x else '0',[coAlert,chAlert,guardAlert]) alert = reduce(lambda x,y:x+y,alert) self.write(alert) except: self.write('E00')
def get_current_user(self, url): ''' Return a User object with details of the current authenticated user, including a url for logging out. If user has not yet been authenticated, return a User object with a url to allow user to log in. ''' e, n, ui, li, lo, oid, wrd = (None, None, None, None, None, -1, False) user = self.db.user ia = False if user is None: li = self.ae.create_login_url(url) else: ia = user.is_admin e, n, ui = user.email(), user.nickname(), user.user_id() # make sure ListOwnerDb record exists owner = self.db.get_owner_by_user(user) req = None if owner is None: if not user.is_admin: req = self.db.get_req_by_user(user) if req is None: req = self.db.add_req(user) else: owner = self.db.add_owner(user) oid = owner.key().id() if owner is not None else -1 wrd = req.denied if req is not None else False base = get_base_url(url) lo = self.ae.create_logout_url('%s/#Goodbye:' % base) return User(email=e, nickname=n, user_id=ui, login_url=li, logout_url=lo, owner_id=oid, was_req_denied=wrd, is_admin=ia)
import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) from core.model import User from core.baseservice import BaseService service = BaseService() def home_content(username): #返回用户家目录 path = os.path.join(BASE_DIR, "home", username) os.makedirs(path) return path def add_user(username): #保存新用户到用户信息表 path = os.path.join(BASE_DIR, "db", "user.txt") with open(path, "a", newline="\n") as f: f.write(username+"\n") if __name__ == '__main__': #初始化用户 u1 = User("user1", "123", home_content("user1"), 1*1024*1024, 1*1024*1024) u2 = User("user2", "123", home_content("user2"), 10*1024*1024, 10*1024*1024) u3 = User("user3", "123", home_content("user3"), 100*1024*1024, 100*1024*1024) u4 = User("user4", "123", home_content("user4"), 500*1024*1024, 500*1024*1024) u = (u1, u2, u3, u4) for i in range(4): service.save_user(u[i]) add_user(u[i].name)
def new_user(): userid = request.form['param2'] user = User(id=userid, password="") db.session.add(user) db.session.commit() return redirect(url_for('get_users'))
def perfect_compatibility(self): user1 = User("*****@*****.**", 4) user2 = User("*****@*****.**", 4) score = compatibility(user1, user2) self.assertEqual(10, score)