def get(self, request: HttpRequest, userid: str, title: str, count: str): count = int(count) if count < 0: count *= -1 try: user = Users.objects.get(user_id__exact=userid) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='userid {} is not found'.format(userid)) post_count = user.posts_set.count() if count >= post_count: count = post_count - 1 posts = user.posts_set.filter(message_title__icontains=title)[:count] post_list = [] for post in posts: p = dict({ 'postid': post.post_id, 'message': post.message, 'title': post.message_title, 'views': post.view_count, 'likes': post.like_count, 'imageurl': post.image_url, 'date': post.creation_date.isoformat() }) post_list.append(p) return JSONResponse.new(code=200, message='success', posts=post_list)
def get(self, request: HttpRequest, userid: str, time_stamp: str, count: str): count = int(count) if count < 0: count *= -1 try: user = Users.objects.get(user_id__exact=userid) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='userid {} is not found'.format(userid)) try: # we can cast to an int because we dont need anymore percision than month and year search_date = datetime.fromtimestamp(int(time_stamp)) except (OverflowError, OSError): return JSONResponse.new(code=400, message='recieved incorrect time stamp {}'.format(time_stamp)) post_count = user.posts_set.count() if count >= post_count: count = post_count - 1 posts = user.posts_set.filter(creation_date__date__gt=datetime.date(search_date))[:count] post_list = [] for post in posts: p = dict({ 'postid': post.post_id, 'message': post.message, 'title': post.message_title, 'views': post.view_count, 'likes': post.like_count, 'imageurl': post.image_url, 'date': post.creation_date.isoformat() }) post_list.append(p) return JSONResponse.new(code=200, message='success', posts=post_list)
def post(self, request: HttpRequest): try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='json decode error, bad data sent to the server') try: user = Users.objects.get(user_id__exact=req_json.get('userid')) comment = Comments.objects.get( comment_id__exact=req_json.get('commentid')) except ObjectDoesNotExist: return JSONResponse.new( code=400, message='user {} or comment {} is not found'.format( req_json.get('userid'), req_json.get('commentid'))) if user.is_active is False: return JSONResponse.new( code=400, message='user id {} must be logged in'.format(user.user_id)) if user.user_id != comment.author_id: return JSONResponse.new( code=400, message='user {} is not the authoer of comment {}'.format( user.user_id, comment.author_id)) comment.delete() return JSONResponse.new(code=200, message='success', commentid=comment.comment_id)
def get(self, request: HttpRequest, postid: str): postid = int(postid) try: post = Posts.objects.get(post_id__exact=postid) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='postid {} was not found'.format(postid)) return JSONResponse.new(code=200, message='success', postid=post.post_id, likecount=post.like_count)
def get(self, request: HttpRequest, commentid: str): commentid = int(commentid) try: comment = Comments.objects.get(comment_id__exact=commentid) except ObjectDoesNotExist: return JSONResponse.new( code=400, message='comment id {} is not found'.format(commentid)) return JSONResponse.new(code=200, message='success', count=comment.like_count)
def post(self, request: HttpRequest): S3 = AWS('snap-life') try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new(code=400, message='request decode error, bad data sent to the server') try: user = Users.objects.get(user_id__exact=req_json.get('userid', '')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='user id {} is not found.'.format(req_json['userid'])) if user.is_active is False: return JSONResponse.new(code=400, message='user id {} must be logged in'.format(user.user_id)) #create new post and assign to the user new_post = Posts() new_post.post_id = uuid4().time_mid new_post.author_username = user.user_name new_post.author_profile_url = user.profile_url if req_json.get('image', None) is not None and len(req_json.get('image', '')) > 0 : image_name = '{}{}.png'.format(user.user_id, new_post.post_id) url = S3.upload_image(image_name, req_json.get('image')) new_post.image_name = image_name new_post.image_url = url else: new_post.image_name = '' new_post.image_url = '' new_post.message = req_json.get('message') new_post.message_title = req_json.get('title', '') new_post.save() user.posts_set.add(new_post) p = dict({ 'postid': new_post.post_id, 'message': new_post.message, 'title': new_post.message_title, 'views': new_post.view_count, 'likes': new_post.like_count, 'imageurl': new_post.image_url, 'date': new_post.creation_date.isoformat(), 'author': new_post.author_username, 'authoravatar': new_post.author_profile_url, 'comments': [] }) return JSONResponse.new(code=200, message='success', post=p)
def get(self, request: HttpRequest, postid: str): postid = int(postid) try: post = Posts.objects.get(post_id__exact=postid) count = post.comments_set.count() except ObjectDoesNotExist: return JSONResponse.new(code=400, message='post id {} is not found'.format(postid)) comment_list = [] comments = post.comments_set.all() for comment in comments: comment_list.append(comment.comment_id) return JSONResponse.new(code=200, message='success', count=count, commentids=comment_list)
def post(self, request: HttpRequest): try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new(code=400, message='json decode error, bad data sent to the server') try: user = Users.objects.get(user_id__exact=req_json.get('userid')) post = Posts.objects.get(post_id__exact=req_json.get('postid')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='postid {} is not found'.format(req_json.get('postid'))) if user.is_active is False: return JSONResponse.new(code=400, message='user id {} must be logged in'.format(user.user_id)) post.like_count += 1 post.save(update_fields=['like_count']) return JSONResponse.new(code=200, message='success', likecount=post.like_count)
def get(self, request: HttpRequest, userid: str, count: str): count = int(count) if count < 0: count *= -1 try: user = Users.objects.get(user_id__exact=userid) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='userid {} is not found'.format(userid)) posts = user.posts_set.all() post_list = [] for follow in user.following.all(): following_posts = follow.posts_set.all() posts = posts | following_posts for post in posts[:count]: comment_list = [] comments = post.comments_set.all() for comment in comments: comment_list.append({ 'message': comment.message, 'author': comment.author_name, 'date': comment.creation_date.isoformat() }) p = dict({ 'postid': post.post_id, 'message': post.message, 'title': post.message_title, 'views': post.view_count, 'likes': post.like_count, 'imageurl': post.image_url, 'date': post.creation_date.isoformat(), 'author': post.author_username, 'authoravatar': post.author_profile_url, 'comments': comment_list }) post_list.append(p) return JSONResponse.new(code=200, message='success', posts=post_list)
def post(self, request: HttpRequest): aws = AWS('snap-life') try: resp_json = json.loads(request.body.decode('utf-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='request decode error, bad data sent to the server') try: user = Users.objects.get( user_name__exact=resp_json.get('username')) user_id = user.user_id except ObjectDoesNotExist: return JSONResponse.new(code=400, message='user {} is not found'.format( resp_json['username'])) if self._verify_user_password(user, resp_json.get('password')): try: del request.session['{}'.format(user.user_id)] except KeyError: pass all_post = user.posts_set.all() post_key_names = [] for post in all_post: if post.image_name: post_key_names.append(post.image_name) aws.remove_images(post_key_names) aws.remove_profile_image(user.user_name) user.delete() else: return JSONResponse.new( code=400, message='username {}, or password is incorrect'.format( resp_json.get('username'))) return JSONResponse.new(code=200, message='success', userid=user_id)
def post(self, request: HttpRequest): try: request_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='request decode error, bad data sent to the server') try: user = Users.objects.get( user_name__exact=request_json.get('username')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='user {} is not found'.format( request_json.get('username'))) if request.session.get('{}'.format(user.user_id), False) is True and user.is_active is True: return JSONResponse.new( code=200, message='user {} is already signed in'.format( request_json.get('username')), userid=user.user_id, firstname=user.first_name, lastname=user.last_name) if self._verify_user_password(user, request_json.get('password')): user.last_login_date = timezone.now() user.is_active = True request.session['{}'.format(user.user_id)] = True user.save() else: message = 'Username \"{}\" or password is incorrect'.format( request_json.get('username')) return JSONResponse.new(code=403, message=message) return JSONResponse.new(code=200, message='success', userid=user.user_id, firstname=user.first_name, lastname=user.last_name)
def post(self, request: HttpRequest): try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='json decode error, bad data sent to the server') message = req_json.get('message') if message is None or len(message) > 255: return JSONResponse.new( code=400, message='message bad data: {}'.format(message)) try: post = Posts.objects.get(post_id__exact=req_json.get('postid')) user = Users.objects.get(user_id__exact=req_json.get('userid')) except ObjectDoesNotExist: return JSONResponse.new( code=400, message='post {} or user {} was not found'.format( req_json.get('postid'), req_json.get('userid'))) if user.is_active is False: return JSONResponse.new( code=400, message='user id {} must be logged in'.format(user.user_id)) comment = Comments() comment.comment_id = uuid4().time_mid comment.author_id = user.user_id comment.author_name = user.user_name comment.message = message comment.save() post.comments_set.add(comment) return JSONResponse.new(code=200, message='success', commentid=comment.comment_id)
def post(self, request: HttpRequest): try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new(code=400, message='json decode error, bad data sent to the server') try: post = Posts.objects.get(post_id__exact=req_json.get('postid')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='post {} is not found'.format(req_json.get('postid'))) post.report_count += 1 post.save(update_fields=['report_count']) # send an email to the reporter send_mail( 'SnapLife post reported', 'Thank you for reporting post {}. This post will now be under review'.format(post.message_title), settings.EMAIL_HOST_USER, [req_json.get('email')], fail_silently=True ) send_mail( 'Report for post ID {}, post name {}'.format(post.post_id, post.image_name), 'Reported by {}\nReport count: {}\nMessage: {}\n Author: {}\nURL: {}'.format( req_json.get('email'), post.report_count, post.message, post.author_username, post.image_url ), settings.EMAIL_HOST_USER, [settings.EMAIL_HOST_USER], fail_silently=True ) return JSONResponse.new(code=200, message='success', count=post.report_count)
def post(self, request: HttpRequest): try: request_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='request decode error, bad data sent to the server') try: user = Users.objects.get(user_id__exact=request_json.get('userid')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='user {} is not found'.format( request_json.get('userid'))) user.is_active = False user.save() try: del request.session['{}'.format(user.user_id)] except KeyError: pass return JSONResponse.new(code=200, message='success', userid=user.user_id)
def post(self, request: HttpRequest): try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new(code=400, message='request decode error, bad data sent to the server') try: user = Users.objects.get(user_id__exact=req_json['userid']) post = user.posts_set.get(post_id__exact=req_json['postid']) except ObjectDoesNotExist: return JSONResponse.new(code=400, message='userid {} or postid {} was not found'.format(req_json['userid'], req_json['postid'])) if user.is_active is False: return JSONResponse.new(code=400, message='user id {} must be logged in'.format(user.user_id)) new_title = req_json.get('title') new_message = req_json.get('message') if new_title is not None: if len(new_title) <= 100: post.message_title = new_title else: return JSONResponse.new(code=400, message='title length incorrect {}'.format(len(new_title))) if new_message is not None: if len(new_message) <= 254: post.message = new_message else: return JSONResponse.new(code=400, message='message length incorrect {}'.format(len(new_message))) post.save(update_fields=['message_title', 'message']) p = dict({ 'postid': post.post_id, 'message': post.message, 'title': post.message_title, 'views': post.view_count, 'likes': post.like_count, 'imageurl': post.image_url, 'date': post.creation_date.isoformat() }) return JSONResponse.new(code=200, message='success', post=p)
def post(self, request: HttpRequest): S3 = AWS('snap-life') try: req_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new(code=400, message="request decode error, bad data sent to the server") try: user = Users.objects.get(user_id__exact=req_json.get('userid', '')) except ObjectDoesNotExist: return JSONResponse.new(code=400, message="userid {} is not found".format(req_json['userid'])) if user.is_active is False: return JSONResponse.new(code=400, message='user id {} must be logged in'.format(user.user_id)) try: if req_json.get('postid'): post = user.posts_set.get(post_id__exact=int(req_json['postid'])) elif req_json.get('title'): post = user.posts_set.get(message_title__exact=req_json['title']) else: return JSONResponse.new(code=400, message='postid or title must be present') comments = post.comments_set.all() except ObjectDoesNotExist: return JSONResponse.new(code=400, message='postid {} is not found'.format(req_json['postid'])) S3.remove_image(key_name=post.image_name) for comment in comments: comment.delete() post.delete() return JSONResponse.new(code=200, message='success', postcount=user.posts_set.count())
def post(self, request: HttpRequest): try: request_json = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: return JSONResponse.new( code=400, message='request decode error, bad data sent to the server') # these are required keys _user_name = request_json.get('username') _first_name = request_json.get('firstname') _last_name = request_json.get('lastname') _password = request_json.get('password') try: self._check_required_inputs( [_user_name, _first_name, _last_name, _password]) except ValueError as err: return JSONResponse.new(code=400, message='{}'.format(err.args[0])) try: Users.objects.get(user_name__exact=_user_name) except ObjectDoesNotExist: # GOOD, lets create a new user new_user = Users() salt = token_hex(16) signer = Signer(salt=salt) new_user.user_id = uuid4().time_mid new_user.first_name = _first_name new_user.last_name = _last_name new_user.user_name = _user_name new_user.salt_hash = salt new_user.password_hash = signer.signature(_password) new_user.email = request_json.get( 'email', '{}@noemail.set'.format(_user_name)) new_user.about = request_json.get('about', '') new_user.last_login_date = timezone.now() new_user.is_active = True if request_json.get('profilepic') is not None: aws = AWS('snap-life') key_name = '{}.png'.format(request_json.get('profilepic')) url = aws.upload_profile_image(new_user.user_name, key_name) new_user.profile_url = url else: new_user.profile_url = 'static/assets/usericon.png' try: new_user.save() request.session['{}'.format(new_user.user_id)] = True except IntegrityError as err: # if this is because we have a collision with our random numbers # hash, userID etc. re-create them del request.session['{}'.format(new_user.user_id)] return JSONResponse.new( code=500, message='username and email need to be unique') else: return JSONResponse.new( code=400, message='username {} is already taken'.format(_user_name)) return JSONResponse.new(code=200, message='success', userid=new_user.user_id)
def get(self, request: HttpRequest): return JSONResponse.new(code=200, message='success')