def manageFollowQuestion(request): ''' post has two parameter : the question Id and an action, fo(follow) or un(unfollow) ''' context=checkSession(request) user = getCurrentUser(context) if user is None: response=HttpResponse() return response questionId=request.POST["questionId"] action=request.POST["action"] user=user.findByLogin() print 'action: '+action if action == 'fo' : contain=True try : user.followedQuestions.index(questionId) except ValueError : contain=False if not contain : print 'appending question '+questionId user.followedQuestions.append(questionId) user.update() return HttpResponse(json.dumps({'followed':True})) if action == 'un': try : user.followedQuestions.remove(questionId) user.update() except ValueError : pass return HttpResponse(json.dumps({'followed':False}))
def get(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() #first, collectRows user=user.findByLogin() rows=[] print user.followedQuestions for questionId in user.followedQuestions: print questionId t=TimeLineEvent(question=questionId) view=t.findByQuestion() for row in view.rows : rows.append(row) #then, sort it notSorted=True max=len(rows) while notSorted: notSorted=False i=0 while i < (max-1) : if isSup(rows[i],rows[i+1]): temp=rows[i] rows[i]=rows[i+1] rows[i+1]=temp notSorted=True i+=1 print "sorted" values=[] for row in rows : values.append(row.value) return HttpResponse(json.dumps(values))
def post(request) : #get current user context = checkSession(request) user = getCurrentUser(context) #get question data questionTitle = smart_unicode(request.POST["title"], encoding='utf-8', strings_only=False, errors='strict') questionDescription = smart_unicode(request.POST["description"], encoding='utf-8', strings_only=False, errors='strict') if not user: message = 'you must be logged in first!' elif questionTitle == "": message = 'a question needs words!' else: #create question q = Question(asker=user.login, title=questionTitle, description=questionDescription) if request.POST.__contains__("tags") : tags=smart_unicode(request.POST["tags"], encoding='utf-8', strings_only=False, errors='strict') topics=tags.split(',') q.topics=topics print q try : q.create() q=q.findById() user=user.findByLogin() user.followedQuestions.append(q.id); user.update() message = 'question successfully posted' except Exception as e: message = e #remove the displayed question response = HttpResponse(); response["message"] = message; response["questionId"] = q.id; return response;
def post(request): # get current user context = checkSession(request) user = getCurrentUser(context) # get question data questionTitle = smart_unicode(request.POST["title"], encoding="utf-8", strings_only=False, errors="strict") questionDescription = smart_unicode( request.POST["description"], encoding="utf-8", strings_only=False, errors="strict" ) if not user: message = "you must be logged in first!" elif questionTitle == "": message = "a question needs words!" else: # create question q = Question(asker=user.login, title=questionTitle, description=questionDescription) if request.POST.__contains__("tags"): tags = smart_unicode(request.POST["tags"], encoding="utf-8", strings_only=False, errors="strict") topics = tags.split(",") q.topics = topics try: q.create() except Exception as e: message = e else: message = "question successfully posted" response = HttpResponse() response["message"] = message return response
def get(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() #first, collectRows user = user.findByLogin() rows = [] print user.followedQuestions for questionId in user.followedQuestions: print questionId t = TimeLineEvent(question=questionId) view = t.findByQuestion() for row in view.rows: rows.append(row) #then, sort it notSorted = True max = len(rows) while notSorted: notSorted = False i = 0 while i < (max - 1): if isSup(rows[i], rows[i + 1]): temp = rows[i] rows[i] = rows[i + 1] rows[i + 1] = temp notSorted = True i += 1 print "sorted" values = [] for row in rows: values.append(row.value) return HttpResponse(json.dumps(values))
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 getCurrentUser(request): ''' this function returns the current user ''' context = RequestContext(request) context = userauth.checkSession(request, context) currentUser = userauth.getCurrentUser(context) return currentUser
def postAnswer(request): #obtain question by ID questionId = request.POST["questionId"] q = Question(id=questionId) q = q.findById() #create answer ID by hashing (userId, questionId) context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse( json.dumps({ 'error': 1, 'errorMessage': 'You need to be logged in to post an answer' })) answerId = sha1(user.id + questionId).hexdigest() #check if answer ID already exists #this means that this user already posted an answer for this question -> abort post #the following is deactivated for development purposes ''' for answer in q.answers: if answer.id == answerId: return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'})) ''' content = request.POST["answer"] newAnswer = {'content': content, 'id': answerId, 'poster': user.login} q.answers.append(newAnswer) q.update() #time line event creation t = TimeLineEvent() t.user = user.login t.action = "POST" t.questionTitle = Question(id=questionId).findById().content t.answer = answerId t.question = questionId t.create() print t print 'answer added to question: ' + str(q) #unwrap answer dictionaries so that we can serialize into json answerList = [] for answer in q.answers: answerList.append(answer.unwrap()) return HttpResponse(json.dumps(answerList))
def rateAnswer(request): context=checkSession(request) user = getCurrentUser(context) questionId = request.POST["questionId"] if user is None: response=HttpResponse(getAnswersJson(questionId)) response['errorMessage']='You must be logged in to rate an answer' return response ratingType = request.POST["type"] answerId = request.POST["answerId"] r=Rating(_id=sha1(user.id+answerId).hexdigest()) if r.findById() : response=HttpResponse(getAnswersJson(questionId)) response['errorMessage']='You have already rated this answer' return response q = Question(id=questionId) q = q.findById() updated = False for answer in q.answers: if answer.id == answerId: if answer.score is None: answer.score =0 if ratingType == 'increment': answer.score += 1 else: answer.score -= 1 q.update() updated = True errorMessage = '' if updated: r.create(); else: errorMessage = 'could not update rating' #unwrap answer dictionaries so that we can serialize into json answerList = [] for answer in q.answers: answerList.append(answer.unwrap()) response = HttpResponse(json.dumps(answerList)) response['errorMessage'] = errorMessage response['editedId'] = answerId return response
def displayFollowedQuestions(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() user = user.findByLogin() questionList = [] for questionId in user.followedQuestions: q = Question(id=questionId) q = q.findById() questionList.append({"id": q.id, "content": q.content}) return HttpResponse(json.dumps(questionList))
def postAnswer(request): context=checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You need to be logged in to post an answer'})) #obtain question by ID questionId = request.POST["questionId"] q = Question(id=questionId) q = q.findById() #create answer ID by hashing (userId, questionId) answerId = sha1(user.id + questionId).hexdigest() #check if answer ID already exists #this means that this user already posted an answer for this question -> abort post #the following is deactivated for development purposes for answer in q.answers: if answer.id == answerId: return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'})) content = smart_unicode(request.POST["answer"], encoding='utf-8', strings_only=False, errors='strict') newAnswer = {'content': content, 'id': answerId, 'poster':user.login} q.answers.append(newAnswer) q.update() #time line event creation t=TimeLineEvent() t.user=user.login t.action="POST" t.questionTitle=Question(id=questionId).findById().title t.answer=answerId t.question=questionId t.create() print t print 'answer added to question: ' + str(q) #unwrap answer dictionaries so that we can serialize into json answerList = [] for answer in q.answers: answerList.append(answer.unwrap()) response = HttpResponse(json.dumps(answerList)) response['lastAddedId'] = newAnswer['id'] return response
def displayFollowedQuestions(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() user = user.findByLogin() questionList = [] for questionId in user.followedQuestions: q = Question(id=questionId) q = q.findById() questionList.append({ 'id': q.id, 'content': q.content, }) return HttpResponse(json.dumps(questionList))
def postAnswer(request): # obtain question by ID questionId = request.POST["questionId"] q = Question(id=questionId) q = q.findById() # create answer ID by hashing (userId, questionId) context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse(json.dumps({"error": 1, "errorMessage": "You need to be logged in to post an answer"})) answerId = sha1(user.id + questionId).hexdigest() # check if answer ID already exists # this means that this user already posted an answer for this question -> abort post # the following is deactivated for development purposes """ for answer in q.answers: if answer.id == answerId: return HttpResponse(json.dumps({'error':1, 'errorMessage': 'You have already posted an answer for this Kuestion!'})) """ content = request.POST["answer"] newAnswer = {"content": content, "id": answerId, "poster": user.login} q.answers.append(newAnswer) q.update() # time line event creation t = TimeLineEvent() t.user = user.login t.action = "POST" t.questionTitle = Question(id=questionId).findById().content t.answer = answerId t.question = questionId t.create() print t print "answer added to question: " + str(q) # unwrap answer dictionaries so that we can serialize into json answerList = [] for answer in q.answers: answerList.append(answer.unwrap()) return HttpResponse(json.dumps(answerList))
def displayFollowedQuestions(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() user = user.findByLogin() questionList = [] for questionId in user.followedQuestions: q = Question(id=questionId) q = q.findById() questionList.append({ 'id': q.id, 'title': q.title, 'asker': q.asker, 'postDate': q.postDate.isoformat(), }) return HttpResponse(json.dumps(questionList))
def post(request): #get current user context = checkSession(request) user = getCurrentUser(context) #get question data questionTitle = smart_unicode(request.POST["title"], encoding='utf-8', strings_only=False, errors='strict') questionDescription = smart_unicode(request.POST["description"], encoding='utf-8', strings_only=False, errors='strict') if not user: message = 'you must be logged in first!' elif questionTitle == "": message = 'a question needs words!' else: #create question q = Question(asker=user.login, title=questionTitle, description=questionDescription) if request.POST.__contains__("tags"): tags = smart_unicode(request.POST["tags"], encoding='utf-8', strings_only=False, errors='strict') topics = tags.split(',') q.topics = topics try: q.create() except Exception as e: message = e else: message = 'question successfully posted' response = HttpResponse() response["message"] = message return response
def get(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() #first, collectRows user = user.findByLogin() rows = [] for questionId in user.followedQuestions: t = TimeLineEvent(question=questionId) view = t.findByQuestion() for row in view.rows: rows.append(row) #then, sort it notSorted = True max = len(rows) while notSorted: notSorted = False i = 0 while i < (max - 1): if isSup(rows[i], rows[i + 1]): temp = rows[i] rows[i] = rows[i + 1] rows[i + 1] = temp notSorted = True i += 1 #timeline concatenation #create a copy of the list temp = [] for row in rows: temp.append(row) dir(temp) if len(temp) == 0: return HttpResponse() #the bloc class is used as a data wraper for the timeline info class Bloc: pass b = Bloc() b.questionId = temp[0].value['question'] results = [] b.count = 0 b.questionTitle = temp[0].value['questionTitle'] b.date = temp[0].value['eventDate'] b.users = [] currentBloc = b #concatenate it by continuous block, and count the number of occurencies for row in temp: if row.value['question'] == currentBloc.questionId: currentBloc.count += 1 if currentBloc.users.__contains__(row.value['user']) == False: currentBloc.users.append(row.value['user']) else: results.append(currentBloc) currentBloc = Bloc() currentBloc.count = 1 currentBloc.questionTitle = row.value['questionTitle'] currentBloc.date = row.value['eventDate'] currentBloc.questionId = row.value['question'] currentBloc.users = [] currentBloc.users.append(row.value['user']) results.append(currentBloc) #generating the final timeline timeline = [] for item in results: timeline.append({ 'questionTitle': item.questionTitle, 'date': item.date, 'questionId': item.questionId, 'answerCount': item.count, 'users': item.users }) print json.dumps(timeline) #limit the size of the time line to 20 values = [] i = 0 for item in timeline: values.append(item) if i > TIMELINE_SIZE: break i += 1 return HttpResponse(json.dumps(values))
def get(request): context = checkSession(request) user = getCurrentUser(context) if user is None: return HttpResponse() #first, collectRows user = user.findByLogin() rows = [] for questionId in user.followedQuestions: t = TimeLineEvent(question=questionId) view = t.findByQuestion() for row in view.rows : rows.append(row) #then, sort it notSorted = True max = len(rows) while notSorted: notSorted = False i = 0 while i < (max - 1) : if isSup(rows[i], rows[i + 1]): temp = rows[i] rows[i] = rows[i + 1] rows[i + 1] = temp notSorted = True i += 1 #timeline concatenation #create a copy of the list temp = [] for row in rows: temp.append(row) dir(temp) if len(temp)==0: return HttpResponse() #the bloc class is used as a data wraper for the timeline info class Bloc: pass b = Bloc() b.questionId = temp[0].value['question'] results = [] b.count = 0; b.questionTitle = temp[0].value['questionTitle'] b.date = temp[0].value['eventDate'] b.users = [] currentBloc=b #concatenate it by continuous block, and count the number of occurencies for row in temp: if row.value['question'] == currentBloc.questionId: currentBloc.count += 1 if currentBloc.users.__contains__(row.value['user']) == False: currentBloc.users.append(row.value['user']) else : results.append(currentBloc) currentBloc = Bloc() currentBloc.count = 1 currentBloc.questionTitle = row.value['questionTitle'] currentBloc.date = row.value['eventDate'] currentBloc.questionId = row.value['question'] currentBloc.users=[] currentBloc.users.append(row.value['user']) results.append(currentBloc) #generating the final timeline timeline = [] for item in results: timeline.append({'questionTitle':item.questionTitle, 'date':item.date, 'questionId':item.questionId, 'answerCount':item.count, 'users':item.users}) print json.dumps(timeline) #limit the size of the time line to 20 values = [] i = 0 for item in timeline: values.append(item) if i > TIMELINE_SIZE : break i += 1; return HttpResponse(json.dumps(values))
def addKeyword(request): context = checkSession(request, context) user = getCurrentUser(context) user = user.findById() fav = request.GET['favorite'] user.topics.append(fav)
def addKeyword(request): context=checkSession(request,context) user = getCurrentUser(context) user = user.findById() fav=request.GET['favorite'] user.topics.append(fav);