def upload_picture(request): try: if request.method == 'POST': image = request.FILES.get('image') if image: image_basename = os.path.basename(image.name) open('images\\' + image_basename, 'wb').write(image.file.read()) image_location = 'images\\' + image_basename path = os.path.join(settings.BASE_DIR, image_location) image_obj = mongo_client_obj.insert_one_data( UploadedImage, {'image_address': path}) return JsonResponse( { '_id': str(image_obj._id), 'address': image_obj.image_address }, status=status.HTTP_200_OK) else: return JsonResponse({ '_id': None, 'address': None }, status=status.HTTP_200_OK) except Exception as e: logger.error("upload_picture : " + str(e)) raise Exception(str(e))
def follow(following_id, follower_id): following_id = ObjectId(following_id) follower_id = ObjectId(follower_id) mongo_client_obj.insert_one_data(FollowingRelation, {'following': following_id, 'follower': follower_id}) # increase the number of follower and following follower_obj = mongo_client_obj.update_data(Profile, {'_id': following_id}, {"$inc": {'number_of_follower': 1}}, upsert=False) following_obj = mongo_client_obj.update_data(Profile, {'_id': follower_id}, {"$inc": {'number_of_following': 1}}, upsert=False) return "followed"
def request_to_follow(applicant_user, requested_user): try: request_obj = mongo_client_obj.insert_one_data(FollowRequest, {'applicant_user': applicant_user, 'requested_user': requested_user}) return "requested" except Exception as e: logger.error("request_to_follow/base : " + str(e)) raise Exception(str(e))
def like_post(author_id, post_id): try: author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)}) like_obj = mongo_client_obj.insert_one_data(Like, {'author': { 'username': author_obj.username, 'picture': author_obj.picture}, 'post_id': ObjectId(post_id)}) # increase the number of post's like mongo_client_obj.update_data(Post, {'_id': ObjectId(post_id)}, {"$inc": {'likes': 1}}, upsert=False) return "like" except Exception as e: logger.error("like_post/base" + str(e)) raise Exception(str(e))
def create_post(data): try: post_obj = mongo_client_obj.insert_one_data(Post, data) profile_obj = mongo_client_obj.update_data(Profile, {"_id": post_obj.publisher}, {"$inc": {"number_of_posts": 1}}, upsert=False) publish_on_home_page(post_obj.publisher, post_obj._id) post_dict = make_dict_embedded_post(post_obj) profile_dict = make_dict_embedded_profile(profile_obj) output_create_post_obj = OutputPost(**{'publisher': profile_dict, 'post': post_dict}) return output_create_post_obj except Exception as e: logger.error("create_post/base :" + str(e)) raise Exception(str(e))
def create_profile(username, first_name, last_name, picture, picture_id, private): try: data = { 'username': username, 'first_name': first_name, 'last_name': last_name, 'picture': picture, 'picture_id': ObjectId(picture_id), 'private': private, 'date_of_join': time.time() } profile_obj = mongo_client_obj.insert_one_data(Profile, data) profile_dict = make_dict_embedded_profile(profile_obj) output_profile_obj = OutputProfile(**profile_dict) return output_profile_obj except Exception as e: logger.error("create_profile/base : " + str(e)) raise Exception(str(e))
def post_comment(post_id, comment_text, tags, author_id, date): try: author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)}) data = { 'post_id': post_id, 'comment_text': comment_text, 'author': { 'username': author_obj.username, 'picture': author_obj.picture }, 'tags': tags, 'date': date } comment_obj = mongo_client_obj.insert_one_data(Comment, data) comment_dict = make_dict_embedded_comment(author_obj.username, author_obj.picture, comment_obj.comment_text, comment_obj.date) output_comment = EmbeddedComment(**comment_dict) return output_comment except Exception as e: logger.error("post_comment/base : " + str(e)) raise Exception(str(e))