def post(request): response = RequestResponse() if check_params(request.POST, ['email', 'password']): post_data = request.POST user = Writer.get_user_by_email_password(post_data['email'], post_data['password']) if user is not None and user.is_active: data = Login.create_session_for_user(user) response.set_data(data) response.set_status(200) response.set_message('Session created successfully') elif user is not None and not user.is_active: response.set_status(400) response.set_message('Account not active') else: response.set_status(400) response.set_message('Incorrect credentials') elif check_params(request.POST, ['username', 'password']): post_data = request.POST user = Writer.get_user_by_username_password( post_data['username'], post_data['password']) if user is not None and user.is_active: data = Login.create_session_for_user(user) response.set_data(data) response.set_status(200) response.set_message('Session created successfully') elif user is not None and not user.is_active: response.set_status(400) response.set_message('Account not active') else: response.set_status(400) response.set_message('Incorrect credentials') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, ['name', 'username', 'password', 'email', 'phone']): post_data = request.POST if Writer.objects.filter(email=post_data['email']).exists() or \ Writer.objects.filter(username=post_data['username']).exists(): response.set_status(400) response.set_message('User already registered') else: user = Writer() user.username = post_data['username'] user.name = post_data['name'] user.email = post_data['email'] user.phone = post_data['phone'] user.password = Writer.encrypt_password(post_data['password']) user.save() four_digit_code = random4() new_validation = EmailValidation(writer=user, code=four_digit_code) new_validation.save() try: email_subject = 'Welcome to the Wall!' link = 'http://' + request.META['HTTP_HOST'] + '/api/activate/?email=' + \ user.email + '&auth=' + four_digit_code email_body = "Hi %s,\rwelcome to the wall. Your activation code is %s.\r\r" \ "Or you can directly activate your account by clicking here: %s" \ % (user.name, four_digit_code, link) send(user.name, user.email, email_subject, email_body) except KeyError: data = {'activation_code': four_digit_code} response.set_data(data) response.set_status(200) response.set_message('User successfully added') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def get(request): response = RequestResponse() if check_params(request.GET, ['auth', 'email']): get_data = request.GET if Writer.objects.filter(email=get_data['email']).exists(): writer_object = Writer.objects.get(email=get_data['email']) if not writer_object.is_active: if EmailValidation.objects.filter( code=get_data['auth'], writer=writer_object).exists(): validation_object = EmailValidation.objects.get( code=get_data['auth'], writer=writer_object) writer_object.is_active = True writer_object.save() validation_object.delete() response.set_status(200) response.set_message('User successfully activated') else: response.set_status(400) response.set_message('Validation code incorrect') else: response.set_status(400) response.set_message('Already activated') else: response.set_status(400) response.set_message('User not found') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, ['sid']): post_data = request.POST if Session.objects.filter(code=post_data['sid']).exists(): session_object = Session.objects.get(code=post_data['sid']) session_object.delete() response.set_status(200) response.set_message('Session removed successfully') else: response.set_status(400) response.set_message('Incorrect session') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, []): post_data = request.POST max_per_request = 5 first_post = 0 if 'max_per_request' in post_data: try: max_per_request = int(post_data['max_per_request']) except ValueError: pass if 'first_post' in post_data: try: first_post = int(post_data['first_post']) except ValueError: pass data = {'next_post_serial': first_post + max_per_request} posts = Post.objects.filter( published=True).order_by('-created_at')[first_post:first_post + max_per_request] selected_posts = [] for post in posts: all_comments = Comment.objects.filter( published=True, post=post).order_by('created_at') comments = [] for comment in all_comments: one_comment = { 'commenter': comment.commenter.pk, 'commenter_username': comment.commenter.username, 'comment': comment.content, 'comment_published': comment.created_at.strftime("%A, %d. %B %Y %I:%M%p") } comments.append(one_comment) one_post = { 'post_id': post.id, 'post_title': post.title, 'post_content': post.content, 'post_published': post.created_at.strftime("%A, %d. %B %Y %I:%M%p"), 'post_writer': post.writer.pk, 'post_writer_username': post.writer.username, 'comments': comments } selected_posts.append(one_post) data['posts'] = selected_posts response.set_status(200) response.set_message('Post loaded successfully') response.set_data(data) else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, ['sid', 'pid']): post_data = request.POST user = Writer.get_user_by_session(post_data['sid']) if user is not None: if Post.objects.filter(pk=post_data['pid']).exists(): post_object = Post.objects.get(pk=post_data['pid']) if post_object.writer == user: post_object.delete() response.set_status(200) response.set_message('Post uploaded successfully') else: response.set_status(400) response.set_message('Do not have access to delete') else: response.set_status(400) response.set_message('Post not found. Invalid "pid"') else: response.set_status(400) response.set_message('Incorrect session') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, ['sid', 'title', 'content']): post_data = request.POST user = Writer.get_user_by_session(post_data['sid']) if user is not None: if len(post_data['content']) <= 2048: new_post = Post(writer=user, title=post_data['title'], content=post_data['content']) new_post.save() response.set_status(200) response.set_message('Post uploaded successfully') else: response.set_status(400) response.set_message('Content too large') else: response.set_status(400) response.set_message('Incorrect session') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())
def post(request): response = RequestResponse() if check_params(request.POST, ['sid', 'pid', 'comment']): post_data = request.POST user = Writer.get_user_by_session(post_data['sid']) if user is not None: if Post.objects.filter(pk=post_data['pid']).exists(): post_object = Post.objects.get(pk=post_data['pid']) new_comment = Comment(post=post_object, commenter=user, content=post_data['comment']) new_comment.save() response.set_status(200) response.set_message('Comment added successfully') else: response.set_status(400) response.set_message('Invalid post id') else: response.set_status(400) response.set_message('Incorrect session') else: response.set_status(400) response.set_message('Incorrect Parameters') return Response(response.respond())