def autorize(): loged_in = session.get("loged_in") if loged_in: email = session.get("email") password = session.get("password") pers = Session.query(Person).filter_by(email=email, password=password).first() pers.last_activity = int(time.time()) Session.commit() return True if "email" and "password" in request.cookies: email = request.cookies.get("email") password = request.cookies.get("password") elif request.method == "POST" and "email" in request.POST: email = request.POST.get("password") password = md5(request.POST.get("password")) else: return False pers = Session.query(Person).filter_by(email=email, password=password).first() if pers: print "pers is ", pers session["email"] = email session["password"] = password session["loged_in"] = True session["name"] = pers.name session["person_id"] = pers.id session.save() return True return False
def __before__(self): c.chanel='' c.messages=[] c.user={} c.search={} self.chanel_q=Session.query(Chanel) self.person_q=Session.query(Person) self.message_q=Session.query(Message)
def add_message(self): if request.method=='GET': return '' person_id=session.get('person_id') person_name=session.get('name') chanel_name=request.POST.get('chanel') chanel=self.chanel_q.filter_by(name=chanel_name).first() body=request.POST.get('body') to=request.POST.get('to') message=Message(body=body, date=get_date(), timestamp=int(time.time()), to=to, type='public', person_id=person_id, person_name=person_name,chanel_id=chanel.id) Session.add(message) Session.commit() c.body=body c.name=person_name c.date=get_date() # return render('ajax_add_message.mako') redirect('/chanel/'+chanel_name)
def signup(self): if request.method == "GET": redirect("/") email = self.form_result.get("email") name = self.form_result.get("name") password = self.form_result.get("password") result = self.person_q.filter_by(name=name).first() if result: return "This nickname is used by another one" result2 = self.person_q.filter_by(email=email).first() if result2: return "This email registered yet. Do u forget your password?" pers = Person(name=name, email=email, password=md5(password)) Session.add(pers) Session.commit() response.set_cookie("email", email) response.set_cookie("password", md5(password)) redirect("/chanel/main")
def chat_route(self, id): autorize() chanel=self.chanel_q.filter_by(name=id).first() if not chanel: return 'This chanel does not exist' user_id=session.get('person_id') if user_id!=None: user=self.person_q.filter_by(id=user_id).first() user.chanel_id=chanel.id Session.commit() session['chanel']={'id':chanel.id, 'name':chanel.name} session.save() c.user['name']=session.get('name') chanels=self.chanel_q.filter_by(owner=c.user['name']).all() c.user['chanels']=chanels c.title='#'+id c.chanel=id c.timestamp=int(time.time()) return render('croute.mako')
def create_chat(self): USER_CHATS_LIMIT=10 if request.method=='GET': return '' chat_name=request.POST.get('chat_name') result_query=self.chanel_q.filter_by(name=chat_name).first() if result_query: return 'This chanel name exists' owner_email=session.get('email') owner=self.person_q.filter_by(email=owner_email).first() if not owner: redirect('/auth/signin') chanels=self.chanel_q.filter_by(owner=owner.name).all() if len(chanels)>USER_CHATS_LIMIT: return 'You can`t create a chat' chanel=Chanel(name=chat_name, owner=owner.name, created='test') Session.add(chanel) Session.commit() redirect('/chanel/'+chat_name)
def main(): generator=create_generator() i=0 for person in generator: i+=1 person.chanel_id=1 Session.add(person) if i==50: Session.commit() i=0 if i!=0: Session.commit()
def create_generator(): last_activity=int(time.time())-600 pers=Session.query(Person).filter(Person.last_activity<last_activity, Person.chanel_id>1).all() for i in pers: yield i
def __before__(self): self.person_q = Session.query(Person)