def answer(request, id=None): if request.method == 'GET': return generate_response(message='Not Implemented', status=status.HTTP_501_NOT_IMPLEMENTED) if request.method == 'POST': return generate_response(message='Not Implemented', status=status.HTTP_501_NOT_IMPLEMENTED)
def newsfeed(request): cleanup_notification() # Check user login if not request.user.is_authenticated: return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': user = User.objects.get(username=request.user.username) profile = Profile.objects.get(user=user) every_notifications = Notification.objects.filter(profile=profile, read=False)\ .order_by('-created') serializer = NotificationSerializer(every_notifications, many=True) response = generate_response(serializer.data, status=status.HTTP_200_OK) every_notifications.update(pushed=True) return response if request.method == 'PUT': nid = request.data['nid'] notification = Notification.objects.get(id=nid) notification.read = True notification.save() return generate_response(status=status.HTTP_200_OK)
def signin(request): if request.method == 'POST': username = request.data.get('username', None) password = request.data.get('password', None) user = authenticate(username=username, password=password) if user is not None: login(request, user) return generate_response(status=status.HTTP_200_OK) else: return generate_response(message='Username or password is wrong', status=status.HTTP_401_UNAUTHORIZED)
def userinfo(request, id=None): if request.method == 'GET': if not id: id = request.user.id base_user = User.objects.filter(pk=id) if not base_user: return generate_response(message="No such user", status=status.HTTP_400_BAD_REQUEST) serializer = UserProfileSerializer(base_user[0]) return generate_response(serializer.data, status=status.HTTP_200_OK)
def watchtags(request): # Check user login if not request.user.is_authenticated: return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'POST': user = User.objects.get(username=request.user.username) profile = Profile.objects.get(user=user) profile.watch_tags = request.data['tags'] profile.save() return generate_response(status=status.HTTP_200_OK)
def signup(request): if request.method == 'POST': username = request.data.get('username', None) password = request.data.get('password', None) email = request.data.get('email', None) if not (username and password and email): return generate_response(message='Parameters are not given', status=status.HTTP_401_UNAUTHORIZED) success = create_user(username=username, password=password, email=email) if not success: return generate_response(message='Duplicate username or email', status=status.HTTP_401_UNAUTHORIZED) return generate_response(status=status.HTTP_201_CREATED)
def tags(request): tag_dict = {} every_informations = InformationPost.objects.all() every_questions = QuestionPost.objects.all() for info in every_informations: for tag in info.tags: if tag in tag_dict.keys(): tag_dict[tag] = tag_dict[tag] + 1 else: tag_dict[tag] = 1 for question in every_questions: for tag in question.tags: if tag in tag_dict.keys(): tag_dict[tag] = tag_dict[tag] + 1 else: tag_dict[tag] = 1 sortedArr = sorted(tag_dict.items(), key=operator.itemgetter(1), reverse=True) data = [] for x in sortedArr: data.append(x[0]) response_data = {'tags': data} return generate_response(response_data, status=status.HTTP_200_OK)
def main(): memory_stack = [] general_script, script, memory_inputs, exit_inputs = setup( GENERAL_SCRIPT_PATH, SCRIPT_PATH) # Get first user input in_str = input("Eliza: Welcome.\nYou: ") in_str_l = in_str.lower() # Main execution loop while in_str_l not in exit_inputs: # str.lower().islower() is a fast way of checking # if a string contains any characters of the alphabet. # Source: https://stackoverflow.com/a/59301031 if not in_str_l.islower(): response = prepare_response( 'Eliza: Please, use letters. I am human, after all.') elif in_str_l == 'reset': reset_all_last_used_reassembly_rule(script) response = prepare_response('Eliza: Reset complete.') else: response = generate_response(in_str, script, general_script['substitutions'], memory_stack, memory_inputs) # Get next user input in_str = input(response) in_str_l = in_str.lower() print("Eliza: Goodbye.\n")
def vote(request, id=None): if request.method == 'GET': upvotes = Vote.objects.filter(post=id, vote_type='upvote').count() downvotes = Vote.objects.filter(post=id, vote_type='downvote').count() data = {'upvotes': upvotes, 'downvotes': downvotes} return generate_response(data=data, status=status.HTTP_200_OK) if request.method == 'POST': user = User.objects.get(username=request.user.username) profile = Profile.objects.get(user=user) if Vote.objects.filter(post=id, voter=profile.id).count() > 0: return generate_response( message='You have already vote on this post', status=status.HTTP_400_BAD_REQUEST) data = request.data.copy() data['voter'] = profile.id data['post'] = id data['weight'] = 1 information = InformationPost.objects.get(pk=id) author = Profile.objects.get(pk=information.author.id) if data['vote_type'] == 'upvote': author.credit += 2 else: author.credit -= 2 author.save() serializer = VoteSerializer(data=data) if not serializer.is_valid(): return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) serializer.save() return generate_response(data=serializer.data, status=status.HTTP_201_CREATED) return Response({'message': 'NOT IMPLEMENTED'}, status=status.HTTP_501_NOT_IMPLEMENTED)
def credit(request, username): if request.method == 'GET': user = User.objects.get(username=username) profile = Profile.objects.get(user=user) profile.credit = profile.credit + 1000 if settings.DEBUG: profile.save() return generate_response(status=status.HTTP_200_OK)
def verify(request, username): if request.method == 'GET': user = User.objects.get(username=username) profile = Profile.objects.get(user=user) profile.verified = True if settings.DEBUG: profile.save() return generate_response(status=status.HTTP_200_OK)
def questions(request): cleanup_question() every_questions = QuestionPost.objects.filter(due__gte=timezone.localtime()).order_by('-created') serializer = QuestionPostSerializer(every_questions, many=True) for x in serializer.data: x.pop('content', None) x['author'] = Profile.objects.get(pk=x['author']).user.username return generate_response(serializer.data, status=status.HTTP_200_OK)
def informations_with_tag(request, tag): every_informations = InformationPost.objects.\ filter(tags__contains=[tag], due__gte=timezone.localtime()).\ order_by('-created') serializer = InformationPostSerializer(every_informations, many=True) for x in serializer.data: x.pop('content', None) x.pop('hidden_content', None) x['author'] = Profile.objects.get(pk=x['author']).user.username return generate_response(serializer.data, status=status.HTTP_200_OK)
def signin(request): if request.method == 'POST': username = request.data.get('username', None) password = request.data.get('password', None) if User.objects.filter(username=username).count() == 0: return generate_response(message='Username or password is wrong', status=status.HTTP_401_UNAUTHORIZED) profile = Profile.objects.get(user=User.objects.get(username=username)) if profile.verified is False: return generate_response(message='Please verify email first', status=status.HTTP_401_UNAUTHORIZED) user = authenticate(username=username, password=password) if user is not None: login(request, user) return generate_response(status=status.HTTP_200_OK) else: return generate_response(message='Username or password is wrong', status=status.HTTP_401_UNAUTHORIZED)
def answer(request, id=None): if request.method == 'GET': answers = QuestionAnswer.objects.filter(question=id) serializer = QuestionAnswerSerializer(answers, many=True) mutable_data = serializer.data.copy() for x in mutable_data: x['author'] = Profile.objects.get(pk=x['author']).user.username return generate_response(data=mutable_data, status=status.HTTP_200_OK) if request.method == 'POST': mutable_data = request.data.copy() mutable_data['question'] = mutable_data['qid'] user = User.objects.get(username=request.data['username']) mutable_data['author'] = Profile.objects.get(user=user).id serializer = QuestionAnswerSerializer(data=mutable_data) if not serializer.is_valid(): return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) serializer.save() # add new notification to author question = QuestionPost.objects.get(pk=mutable_data['qid']) message_string = 'You have new answer to question : ' + question.title generate_notification(profile_id=question.author.id, notification_type='new_answer', content_id=mutable_data['qid'], message=message_string) return generate_response(data=serializer.data, status=status.HTTP_201_CREATED)
def signup(request): if request.method == 'POST': username = request.data.get('username', None) password = request.data.get('password', None) email = request.data.get('email', None) if not (username and password and email): return generate_response(message='Parameters are not given', status=status.HTTP_401_UNAUTHORIZED) if User.objects.filter(email=email).count() > 0: return generate_response(message='Duplicate email', status=status.HTTP_401_UNAUTHORIZED) ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" salted_username = username for i in range(5): salted_username = salted_username + random.choice(ALPHABET) hashstring = md5(salted_username.encode()).hexdigest() success = create_user(username=username, password=password, email=email, hashstring=hashstring) if not success: return generate_response(message='Duplicate username or email', status=status.HTTP_401_UNAUTHORIZED) mail_subject = '[SNUWagon] Please confirm your email' message = 'http://www.snuwagon.com/api/auth/verification/' + hashstring to = [email] email = EmailMessage(mail_subject, message, to=to) email.send() return generate_response(message='Please authenticate email', status=status.HTTP_201_CREATED)
def question(request, id=None): # Check user login if not request.user.is_authenticated: return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': try: question_object = QuestionPost.objects.get(pk=id) serializer = QuestionPostSerializer(question_object) # return author name for 'author' field mutable_data = serializer.data.copy() author = Profile.objects.get(pk=mutable_data['author']) mutable_data['author'] = author.user.username return generate_response(mutable_data, status=status.HTTP_200_OK) except Exception as e: return generate_response( status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'POST': mutable_data = request.data.copy() user = User.objects.get(username=request.data['username']) profile = Profile.objects.get(user=user) mutable_data['author'] = profile.id mutable_data['resolved'] = False serializer = QuestionPostSerializer(data=mutable_data) if serializer.is_valid(): serializer.save() return generate_response(data=serializer.data, status=status.HTTP_201_CREATED) return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': # Check if corresponding question exists results = QuestionPost.objects.filter(pk=id) if results.count() < 1: return generate_response(message='No such question found', status=status.HTTP_400_BAD_REQUEST) results.delete() return generate_response(message='Question deleted', status=status.HTTP_200_OK)
def question(request, id=None): # Check user login if not request.user.is_authenticated: return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': try: question_object = QuestionPost.objects.get(pk=id) serializer = QuestionPostSerializer(question_object) # return author name for 'author' field mutable_data = serializer.data.copy() author = Profile.objects.get(pk=mutable_data['author']) mutable_data['author'] = author.user.username return generate_response(mutable_data, status=status.HTTP_200_OK) except Exception as e: return generate_response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'POST': mutable_data = request.data.copy() user = User.objects.get(username=request.data['username']) profile = Profile.objects.get(user=user) mutable_data['author'] = profile.id mutable_data['resolved'] = False serializer = QuestionPostSerializer(data=mutable_data) if serializer.is_valid(): serializer.save() return generate_response(data=serializer.data, status=status.HTTP_201_CREATED) return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': # Check if corresponding question exists results = QuestionPost.objects.filter(pk=id) if results.count() < 1: return generate_response(message='No such question found', status=status.HTTP_400_BAD_REQUEST) results.delete() return generate_response(message='Question deleted', status=status.HTTP_200_OK)
def signout(request): if request.method == 'GET': logout(request) return generate_response(status=status.HTTP_200_OK)
def information(request, id=None): # Check user login # if not request.user.is_authenticated: # return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': try: information_object = InformationPost.objects.get(pk=id) serializer = InformationPostSerializer(information_object) # return author name for 'author' field mutable_data = serializer.data.copy() mutable_data['author_id'] = mutable_data['author'] author = Profile.objects.get(pk=mutable_data['author']) mutable_data['author'] = author.user.username request_user = User.objects.get(username=request.user.username) request_profile = Profile.objects.get(user=request_user) mutable_data['hidden_bought'] = \ BoughtInformation.objects.filter(profile=request_profile, post=id).count() > 0 return generate_response(mutable_data, status=status.HTTP_200_OK) except Exception as e: return generate_response( status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'POST': mutable_data = request.data.copy() if 'tags' not in mutable_data.keys(): mutable_data['tags'] = [] user = User.objects.get(username=request.data['username']) profile = Profile.objects.get(user=user) mutable_data['author'] = profile.id if int(mutable_data['sponsor_credit']) > profile.credit: return generate_response(message='Not enough credits', status=status.HTTP_400_BAD_REQUEST) # Remove credit from user profile.credit -= int(mutable_data['sponsor_credit']) profile.save() serializer = InformationPostSerializer(data=mutable_data) if not serializer.is_valid(): return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) serializer.save() # generate notifications notification_dict = {} for tag in mutable_data['tags']: for x in Profile.objects.filter(watch_tags__contains=[tag]): if x not in notification_dict.keys(): notification_dict[x] = tag for profile in notification_dict.keys(): message_string = 'There is new information about #' + notification_dict[ profile] generate_notification( profile_id=profile.id, notification_type='new_information_about_tag', content_id=serializer.data['id'], message=message_string) return generate_response(data=serializer.data, status=status.HTTP_201_CREATED) if request.method == 'DELETE': # Check if corresponding information exists results = InformationPost.objects.filter(pk=id) if results.count() < 1: return generate_response(message='No such information found', status=status.HTTP_400_BAD_REQUEST) results.delete() return generate_response(message='Information deleted', status=status.HTTP_200_OK) if request.method == 'PUT': user = User.objects.get(username=request.user.username) profile = Profile.objects.get(user=user) information = InformationPost.objects.get(id=id) # Not enough Credit if information.hidden_content_cost > profile.credit: return generate_response(message='Not enough credits', status=status.HTTP_400_BAD_REQUEST) # Remove credit from user profile.credit -= int(information.hidden_content_cost) profile.save() # Add credit to author information.author.credit = information.author.credit + information.hidden_content_cost information.author.save() mutable_data = {'profile': profile.id, 'post': information.id} serializer = BoughtInformationSerializer(data=mutable_data) if not serializer.is_valid(): return generate_response(message='Unexpected error (Poke KJP)', status=status.HTTP_400_BAD_REQUEST) serializer.save() # generate notification message_string = 'Your information ' + information.title + ' is bought!' generate_notification(profile_id=information.author.id, notification_type='information_bought', content_id=information.id, message=message_string) return generate_response(message='Update successful', status=status.HTTP_200_OK)
def informations(request): every_informations = InformationPost.objects.all() serializer = InformationPostSerializer(every_informations, many=True) return generate_response(serializer.data, status=status.HTTP_200_OK)
def question(request, id=None): # Check user login if not request.user.is_authenticated: return generate_response(message='Not logged in', status=status.HTTP_403_FORBIDDEN) if request.method == 'GET': try: if not QuestionPost.objects.filter(pk=id).exists(): return generate_response(message="No question found", status=status.HTTP_404_NOT_FOUND) question_object = QuestionPost.objects.get(pk=id) serializer = QuestionPostSerializer(question_object) # return author name for 'author' field mutable_data = serializer.data.copy() author = Profile.objects.get(pk=mutable_data['author']) mutable_data['author'] = author.user.username return generate_response(mutable_data, status=status.HTTP_200_OK) except Exception as e: return generate_response( message="error", status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'POST': mutable_data = request.data.copy() if 'tags' not in mutable_data.keys(): mutable_data['tags'] = [] user = User.objects.get(username=request.data['username']) profile = Profile.objects.get(user=user) if int(mutable_data['bounty']) > profile.credit: return generate_response(message='Not enough credits', status=status.HTTP_400_BAD_REQUEST) mutable_data['author'] = profile.id mutable_data['resolved'] = False # Remove credit from user profile.credit -= int(mutable_data['bounty']) profile.save() serializer = QuestionPostSerializer(data=mutable_data) if not serializer.is_valid(): return generate_response(message='Invalid parameters', status=status.HTTP_400_BAD_REQUEST) serializer.save() # generate notifications notification_dict = {} for tag in mutable_data['tags']: for x in Profile.objects.filter(watch_tags__contains=[tag]): if x not in notification_dict.keys(): notification_dict[x] = tag for profile in notification_dict.keys(): message_string = 'There is new question about #' + notification_dict[ profile] generate_notification(profile_id=profile.id, notification_type='new_question_about_tag', content_id=serializer.data['id'], message=message_string) return generate_response(data=serializer.data, status=status.HTTP_201_CREATED) if request.method == 'DELETE': # Check if corresponding question exists results = QuestionPost.objects.filter(pk=id) if results.count() < 1: return generate_response(message='No such question found', status=status.HTTP_400_BAD_REQUEST) results.delete() return generate_response(message='Question deleted', status=status.HTTP_200_OK) if request.method == 'PUT': try: qid = request.data['qid'] aid = request.data['aid'] question = QuestionPost.objects.get(pk=qid) answer = QuestionAnswer.objects.get(pk=aid) # update resolved (question.resolved, question.selected) = (True, answer) question.save() # update each user's credit cost = question.bounty question_writer = Profile.objects.get(pk=question.author.id) answer_writer = Profile.objects.get(pk=answer.author.id) question_writer.credit = question_writer.credit + (cost // 10) question_writer.save() answer_writer.credit = answer_writer.credit + cost answer_writer.save() # add new notification to author question = QuestionPost.objects.get(pk=qid) message_string = 'Your answer to ' + question.title + ' is selected!' generate_notification(profile_id=answer.author.id, notification_type='answer_selected', content_id=qid, message=message_string) return generate_response(message='Update successful', status=status.HTTP_200_OK) except Exception as e: print(e) return generate_response( message="error", status=status.HTTP_500_INTERNAL_SERVER_ERROR)