def addTopic(request): ''' this function add a new topic with form post data "newTopic" ''' currentUser = getCurrentUser(request) if currentUser: user = User(login=currentUser.login) user = user.findByLogin() if request.POST['newTopic']: inputTopicStr = smart_unicode(request.POST["newTopic"], encoding='utf-8', strings_only=False, errors='strict') inputArray = inputTopicStr.split(' ') for newTopic in inputArray: print "add Topic:" + newTopic topics = user.topics if not (newTopic.lower() in (topic.lower() for topic in topics)): if newTopic is not '': user.topics.append(newTopic.lower()) user.update() print "result: " + str(user.topics) return HttpResponseRedirect('/user/')
def view(request, login): #TODO distinguish those case: the user see his page, the user see another page #And : Distinguish GET/POST method (need to split in different method) context = RequestContext(request) context = userauth.checkSession(request, context) currentUser = userauth.getCurrentUser(context) context['currentUser'] = currentUser if currentUser and currentUser.login == login: print currentUser.login context['isAdmin'] = True imageForm = ImageUploadForm() context['form'] = imageForm else: context['isAdmin'] = False user = User(login=login) user = user.findByLogin() if user is None: print "not found" return userNotFound(request) if not user.picture: user.picture = 'default.png' if user.picture is None: user.picture = 'default.png' context["user"] = user t = loader.get_template('profile.html') return HttpResponse(t.render(context))
def view(request, login) : #TODO distinguish those case: the user see his page, the user see another page #And : Distinguish GET/POST method (need to split in different method) context = RequestContext(request) context = userauth.checkSession(request, context) currentUser = userauth.getCurrentUser(context) context['currentUser']=currentUser if currentUser and currentUser.login == login: print currentUser.login context['isAdmin'] = True imageForm = ImageUploadForm() context['form'] = imageForm else : context['isAdmin'] =False user = User(login=login) user = user.findByLogin() if user is None : print "not found" return userNotFound(request) if not user.picture: user.picture='default.png' if user.picture is None: user.picture='default.png' context["user"] = user t = loader.get_template('profile.html') return HttpResponse(t.render(context))
def getUserInfoWrapper (user) : ''' This function return a Wrapper from a user contain ''' uiw=User() uiw.id=user.id uiw.login=user.login uiw.resume=user.resume return uiw
def getUserInfoWrapper(user): """ This function return a Wrapper from a user contain """ uiw = User() uiw.id = user.id uiw.login = user.login uiw.resume = user.resume return uiw
def signout(request) : sessionId=None if request.COOKIES.__contains__(userauth.COOKIE_KEY) : sessionId=request.COOKIES[userauth.COOKIE_KEY] user=User(sessionId=sessionId) user=user.findBySessionId() user.sessionId='XXX'+str(uuid.uuid1()) user.update() response=HttpResponseRedirect('/') response.delete_cookie(userauth.COOKIE_KEY) return response
def processFormInformation(login,password,email,request) : u = User(login=login,email=email,password=password) u=u.create() print u if u != None : code=sendActivationMail(login,email) u.activationCode=code u.update() message= 'account succesfully created' else : message= 'error: login name already taken' context={'message': message} return render_to_response('index.html', context ,context_instance=RequestContext(request))
def activate(request, code) : user = User(activationCode=code) user = user.findByActivationCode() if user != None: if user.isActivated == False : user.isActivated = True user.update() message = 'your account have been successfully activated' return HttpResponse('your account have been successfully activated') else : return HttpResponse('this account have already been activated') else : return HttpResponse('wrong activation link')
def testBasicPersistence(self): switchToTestDatabase() u = User(login='******') u.create() u = u.findByLogin() id = u.id print id q = Question(asker=id, content="To be or not to be?") #print "before persistence: ",q print q.content question = q.create() deleteTestDatabase()
def testBasicPersistence(self): switchToTestDatabase() u=User(login='******') u.create() u=u.findByLogin() id=u.id print id q=Question(asker=id,content="To be or not to be?") #print "before persistence: ",q print q.content question=q.create() deleteTestDatabase()
def activate(request,code) : user=User(activationCode=code) user=user.findByActivationCode() if user != None: if user.isActivated == False : user.isActivated= True user.update() message = 'your account have been succesfully activated' else : message = 'this account have been already activated' else : message = 'wrong activation link' context={'message': message} return render_to_response('index.html', context ,context_instance=RequestContext(request))
def updateResume(request): ''' this function updates user resume with form post data "newResume" ''' currentUser = getCurrentUser(request) if currentUser: user = User(login=currentUser.login) user = user.findByLogin() newResume = smart_unicode(request.POST["newResume"], encoding='utf-8', strings_only=False, errors='strict') #newResume = request.POST['newResume'].decode('UTF-8') if newResume: user.resume = newResume user.update() return HttpResponseRedirect('/user/')
def session(request): ''' this method allow to retrieve the current logged user information, if any, from the javascript ''' sessionId=None if request.COOKIES.__contains__(userauth.COOKIE_KEY) : sessionId=request.COOKIES[userauth.COOKIE_KEY] user=User(sessionId=sessionId) user=user.findBySessionId() userW={"login":user.login, "followedQuestions":user.followedQuestions, "isOpen":True } return HttpResponse(json.dumps(userW)) else : return HttpResponse(json.dumps({"isOpen":False}))
def pictureUpload(request): if request.method == 'POST': form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): currentUser = getCurrentUser(request) if currentUser: path = "profile/"+currentUser.login+strftime("(%Y-%m-%d_%H.%M.%S)", gmtime())+".jpg" destination = open(STATIC_MEDIA_ROOT+"/"+path, 'wb+') for chunk in request.FILES['picture'].chunks(): destination.write(chunk) destination.close() user = User(login=currentUser.login) user = user.findByLogin() user.picture=path user.update() return HttpResponseRedirect('/user/')
def signin(request) : login=request.POST['login'] password=request.POST['password'] user=User(login=login) user=user.findByLogin() password=encode(password) if login=='' or password=='' : message='Please enter a login and a password' elif not user : message = 'This account does not exist' elif not user.isActivated : message= 'This account has not been activated yet' elif not user.password == password : message= 'wrong login/password combination' else : #every thing went fine, the session can be open return openSession(request,user) #return a response in case an error occurred return render_to_response('index.html', {'message': message},context_instance=RequestContext(request))
def session(request): ''' this method allow to retrieve the current logged user information, if any, from the javascript ''' sessionId = None if request.COOKIES.__contains__(userauth.COOKIE_KEY): sessionId = request.COOKIES[userauth.COOKIE_KEY] user = User(sessionId=sessionId) user = user.findBySessionId() userW = { "login": user.login, "followedQuestions": user.followedQuestions, "topics": user.topics, "isOpen": True } return HttpResponse(json.dumps(userW)) else: return HttpResponse(json.dumps({"isOpen": False}))
def createUser(userName): u = User(login=userName,email='*****@*****.**',password=encode('123123')) u = u.create() u.isActivated=True #allocate ramdom profile image number=random.randrange(1, 30) u.picture='profile/'+str(number)+'.jpg' u.update()
def addTopic(request): ''' this function add a new topic with form post data "newTopic" ''' currentUser = getCurrentUser(request) if currentUser: user = User(login=currentUser.login) user = user.findByLogin() if request.POST['newTopic']: newTopic = smart_unicode(request.POST["newTopic"], encoding='utf-8', strings_only=False, errors='strict') #newTopic = request.POST['newTopic'].decode('UTF-8') print "add Topic:"+newTopic topics = user.topics if not (newTopic.lower() in (topic.lower() for topic in topics)): user.topics.append(newTopic) user.update() print "result: "+str(user.topics) return HttpResponseRedirect('/user/')
def processFormInformation(login, password, email, request) : activationCode = hashlib.sha1(login+email+password).hexdigest() u = User(name=login, email=email, password_sha=password, activationCode=activationCode) print u u = u.create() print u if u != None : sendActivationMail(email=email, activationCode=activationCode) response=HttpResponse() response.status_code=200 response['message'] = 'account successfully created' return response else : message = 'error: login name already taken' context={"message" : "login name already taken"} response=HttpResponse() response['message'] = 'login already taken' response.status_code=412 return response
def deleteTopic(request): ''' this function delete a topic with form post data "deleteTopic" ''' currentUser = getCurrentUser(request) if currentUser: user = User(login=currentUser.login) user = user.findByLogin() if request.POST['deleteTopic']: deleteTopic = request.POST['deleteTopic'] #deleteTopic = smart_unicode(request.POST["deleteTopic"], encoding='utf-8', strings_only=False, errors='strict') print "del Topic:"+deleteTopic topics = user.topics user.topics = [topic for topic in topics if deleteTopic.lower() != topic.lower()] #if deleteTopic.lower() == topic.lower(): # print topics # print topic user.update() print "result: "+str(user.topics) return HttpResponseRedirect('/user/')
def checkSession(request, context={}): """ add some information to the context for the main page, like user infor, current question, etc Only if the user is correctly logged in, otherwise , somme basic info will be add like top questions, etc """ cookieValue = None if request.COOKIES.__contains__(COOKIE_KEY): cookieValue = request.COOKIES[COOKIE_KEY] if cookieValue: user = User(sessionId=cookieValue) user = user.findBySessionId() if user and checkSessionIsNotExpired(user): context["sessionIsOpen"] = True context["user"] = getUserInfoWrapper(user) else: context["sessionIsOpen"] = False context["user"] = None else: print "security: no cookie found" context["sessionIsOpen"] = False return context
def checkSession (request,context={}) : ''' add some information to the context for the main page, like user infor, current question, etc Only if the user is correctly logged in, otherwise , somme basic info will be add like top questions, etc ''' cookieValue=None if request.COOKIES.__contains__(COOKIE_KEY) : cookieValue= request.COOKIES[COOKIE_KEY] if cookieValue : user=User(sessionId=cookieValue) user=user.findBySessionId() if user and checkSessionIsNotExpired(user) : context['sessionIsOpen']=True context['user']=getUserInfoWrapper(user) else : context['sessionIsOpen']=False context['user']=None else : print 'security: no cookie found' context['sessionIsOpen']=False return context
def processFormInformation(login, password, email, request): activationCode = hashlib.sha1(login + email + password).hexdigest() u = User(name=login, email=email, password_sha=password, activationCode=activationCode) print u u = u.create() print u if u != None: sendActivationMail(email=email, activationCode=activationCode) response = HttpResponse() response.status_code = 200 response[ 'message'] = 'account successfully created, you will received your activation mail soon' return response else: message = 'error: login name already taken' context = {"message": "login name already taken"} response = HttpResponse() response['message'] = 'login already taken' response.status_code = 412 return response
def createInitialUserList(): username = '' profileImgCount = 1 f = open('/Users/macuser/Kuestions/kuestionsWS/userlist') for line in f: username = line.replace('\n', '') u = User(login=username,email='*****@*****.**',password=encode('123123')) u = u.create() u.isActivated=True #allocate ramdom profile image u.picture='profile/'+str(profileImgCount)+'.jpg' profileImgCount = profileImgCount + 1 u.update() f.close()
def activate(request, code): user = User(activationCode=code) user = user.findByActivationCode() if user != None: if user.isActivated == False: user.isActivated = True user.update() message = 'your account have been successfully activated' return HttpResponse( 'your account have been successfully activated') else: return HttpResponse('this account have already been activated') else: return HttpResponse('wrong activation link')
def processFormInformation(login, password, email, request): u = User(login=login, email=email, password=password) u = u.create() print u if u != None: code = sendActivationMail(login, email) u.activationCode = code u.update() message = 'account successfully created, you are going to receive your activation mail soon' else: message = 'error: login name already taken' context = {'message': message} return render_to_response('index.html', context, context_instance=RequestContext(request))
def activate(request, code): user = User(activationCode=code) user = user.findByActivationCode() if user != None: if user.isActivated == False: user.isActivated = True user.update() message = 'your account have been succesfully activated' else: message = 'this account have been already activated' else: message = 'wrong activation link' context = {'message': message} return render_to_response('index.html', context, context_instance=RequestContext(request))
def pictureUpload(request): if request.method == 'POST': form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): currentUser = getCurrentUser(request) if currentUser: path = "profile/" + currentUser.login + strftime( "(%Y-%m-%d_%H.%M.%S)", gmtime()) + ".jpg" destination = open(STATIC_MEDIA_ROOT + "/" + path, 'wb+') for chunk in request.FILES['picture'].chunks(): destination.write(chunk) destination.close() user = User(login=currentUser.login) user = user.findByLogin() user.picture = path user.update() return HttpResponseRedirect('/user/')
def deleteTopic(request): ''' this function delete a topic with form post data "deleteTopic" ''' currentUser = getCurrentUser(request) if currentUser: user = User(login=currentUser.login) user = user.findByLogin() if request.POST['deleteTopic']: deleteTopic = request.POST['deleteTopic'] #deleteTopic = smart_unicode(request.POST["deleteTopic"], encoding='utf-8', strings_only=False, errors='strict') print "del Topic:" + deleteTopic topics = user.topics user.topics = [ topic for topic in topics if deleteTopic.lower() != topic.lower() ] #if deleteTopic.lower() == topic.lower(): # print topics # print topic user.update() print "result: " + str(user.topics) return HttpResponseRedirect('/user/')
def userPicture(request, login): user = User(login=login) user = user.findByLogin() if not user: return HttpResponseRedirect('/kuestions/media/profile.png') return HttpResponseRedirect('/kuestions/media/' + user.picture)
def userPicture(request, login): user = User(login=login) user = user.findByLogin() if not user: return HttpResponse('No User', mimetype="image/jpg") return HttpResponseRedirect('/kuestions/media/' + user.picture)
def userPicture(request, login): user=User(login=login) user=user.findByLogin() if not user: return HttpResponseRedirect('/kuestions/media/profile.png') return HttpResponseRedirect('/kuestions/media/'+user.picture)
def userPicture(request, login): user=User(login=login) user=user.findByLogin() if not user: return HttpResponse('No User',mimetype="image/jpg") return HttpResponseRedirect('/kuestions/media/'+user.picture)