def chatHome(request): username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects(user_id=user["id"])[0] frnds = profile.friends clans = profile.clans_registered friends_list = [] groups_list = [] for friend in frnds: temp = dict() frnd = Profile.objects(user_id=friend)[0] frnd_u = User.objects(id=friend)[0] temp['name'] = frnd["name"] temp['id'] = frnd_u["id"] photo = frnd["photo"].read() my_string = base64.b64encode(photo) temp["photo"] = my_string.decode('utf-8') friends_list.append(temp) print(len(friends_list)) for clan in clans: temp = dict() group = community.objects(id=clan)[0] temp["id"] = group["id"] temp['name'] = group["name"] photo = group["photo"].read() my_string = base64.b64encode(photo) temp["photo"] = my_string.decode('utf-8') groups_list.append(temp) print(len(groups_list)) return render(request, 'chat/home.html', { 'friends_list': friends_list, 'groups_list': groups_list })
def getComments(request): if request.method == 'GET': post_id = request.GET['postId'] username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) post = Post.objects.get(id=post_id) comments = [] for k in post['comments']: comm = Comment.objects.get(id=k) print(comm) temp1 = dict() temp1['comment'] = comm['message'] temp1['reports'] = comm['message'] o = comm['owner'] own = User.objects(id=o)[0] own_p = Profile.objects.get(user_id=o) temp1['owner'] = own_p['name'] photo = own_p["photo"].read() ph_string = base64.b64encode(photo) temp1["photo"] = ph_string.decode('utf-8') comments.append(temp1) count = len(comments) print(count) return render('clans/comments.html', { 'comments': comments, 'count': count })
def getGroupMsgs(request): if request.method == "GET": group_msgs = [] group_id = request.GET['clan_id'] print("Group Id : ", group_id) cu_user = request.session["username"] c_user = User.objects(email=cu_user)[0] uid = c_user['id'] clan = community.objects(id=group_id)[0] print("got clan object") temp = dict() temp['id'] = clan["id"] temp['name'] = clan["name"] photo = clan["photo"].read() my_string = base64.b64encode(photo) temp["photo"] = my_string.decode('utf-8') group_details = temp msgs = clan['messages'] for msg in msgs: print(msg) g_msg = GroupMessage.objects(id=msg)[0] group_msgs.append(g_msg) return render(request, 'chat/msg.html', { 'group_msgs': group_msgs, "group_details": group_details, "uid": uid })
def add_challange(request, clan_id): print("got into add chalng") if request.method == 'POST': print("got into add_chlng POST") if "name" in request.POST and "discription" in request.POST: username = request.session["username"] uid = User.objects(email=username)[0] print("uid", uid["id"]) name = request.POST["name"] complete_date = request.POST["date"] discription = request.POST["discription"] challange = clanChallange(name=name, created_date=datetime.datetime.now(), discription=discription, complete_date=complete_date) challange.owner = uid["id"] challange.community_id = clan_id challange.save() community.objects(id=clan_id).update_one( push__group_challanges=challange.id) return redirect('community:clan-show', clan_id=clan_id) else: return render(request, 'clans/clan_show.html', {"warning": "Please fill all the blanks correctly"}) return redirect('user_auth:home')
def create_clan(request): print("got into create_clan") if request.method == 'POST': print("got into create_clan POST") if "photo" in request.FILES and "name" in request.POST and "discription" in request.POST: username = request.session["username"] uid = User.objects(email=username)[0] print("uid", uid["id"]) name = request.POST["name"] photo = request.FILES["photo"] discription = request.POST["discription"] clan = community(name=name, discription=discription) clan.photo.put(photo, content_type='image/jpeg') clan.Heads.append(uid["id"]) clan.no_of_participants += 1 clan.participants.append(uid["id"]) clan.save() print(clan["id"]) Profile.objects(user_id=uid["id"]).update_one( push__clans_registered=clan["id"]) return redirect('community:clan-home') else: return render(request, 'clans/clans.html', {"warning": "Please fill all the blanks"}) return redirect('user_auth:home')
def clan_show(request, clan_id): username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects(user_id=user["id"])[0] clan = community.objects(id=clan_id)[0] clan_users = [] print(clan) return render(request, 'clans/clan_show.html', {"clan_id": clan_id})
def getPrivMsgs(request): if request.method == 'GET': frnd_id = request.GET['f_id'] cu_user = request.session["username"] c_user = User.objects(email=cu_user)[0] cu_prof = Profile.objects.get(user_id=c_user['id']) frnd_prof = Profile.objects.get(user_id=frnd_id) my_id = c_user['id'] frnd_name = frnd_prof['name'] myphoto = cu_prof["photo"].read() myph = base64.b64encode(myphoto) my_photo = myph.decode('utf-8') frndphoto = frnd_prof["photo"].read() frndph = base64.b64encode(frndphoto) frnd_photo = frndph.decode('utf-8') msgs = cu_prof['messages'] print('Profile msgs', len(msgs)) messages = [] for msg in msgs: print(msg) temp = Message.objects.get(id=msg) temp_r = temp['reciever'] temp_s = temp['sender'] ff_id = frnd_prof['user_id'] print(temp_r == ff_id) print(temp_s == ff_id) if temp_s == ff_id or temp_r == ff_id: temp1 = dict() temp1['message'] = temp['msg'] temp1['sender'] = temp['sender'] temp1['reciever'] = temp['reciever'] temp1['time'] = temp['createdAt'] messages.append(temp1) else: continue print('messages', len(messages)) print('frnd', frnd_name) return render( request, 'chat/priv_msg.html', { 'messages': messages, "friend": frnd_name, "my_id": my_id, "my_photo": my_photo, "frnd_photo": frnd_photo, "friend_id": frnd_id }) else: return HttpResponse('Failure')
def createComment(request): print("create comment") if request.method == "POST": ad_user = request.session["username"] post_id = request.POST["p_id"] cmnt = request.POST["msg"] a_uid = User.objects(email=ad_user)[0] comnt = Comment(message=cmnt, owner=a_uid['id']) comnt.save() Post.objects(id=post_id).update_one(push__comments=comnt["id"]) return HttpResponse('Added comment to the post') else: return HttpResponse('Failure')
def add_user(request): if request.method == 'POST': ad_user = request.session["username"] username = request.POST['uid'] clanid = request.POST['clan_id'] a_uid = User.objects(email=ad_user)[0] a_uid_prof = Profile.objects(user_id=a_uid['id'])[0] uid = User.objects(email=username)[0] uid_prof = Profile.objects(user_id=uid['id'])[0] clan = community.objects(id=clanid)[0] flag = False members = clan.participants if uid["id"] in members: print("User already in group") return HttpResponse('User already in group') else: Profile.objects(user_id=uid["id"]).update_one( push__clans_registered=clanid) print("Profile Updated") community.objects(id=clanid).update_one( push__participants=uid["id"]) print("Clan Participant Added") community.objects(id=clanid).update_one( set__no_of_participants=clan.no_of_participants + 1) print("clan num participants changed") text = a_uid_prof.name text += " added " text += uid_prof.name print("Message: ", text) message = GroupMessage(msg=text, sender=a_uid['id'], group=clanid) message.save() print("message saved") print(message['id']) community.objects(id=clanid).update_one( push__messages=message['id']) return HttpResponse('User added into group')
def sendGrpMsg(request): if request.method == 'POST': text = request.POST['msg'] c_id = request.POST['clan_id'] cu_user = request.session["username"] c_user = User.objects(email=cu_user)[0] c_u_prof = Profile.objects(user_id=c_user['id'])[0] message = GroupMessage(msg=text, sender=c_user['id'], group=c_id) message.save() print("message saved") print(message['id']) community.objects(id=c_id).update_one(push__messages=message['id']) return HttpResponse('Success') else: return HttpResponse('Failure')
def unlike_post(request, post_id): username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) post = Post.objects.get(id=post_id) post.likes -= 1 post.likedBy.remove(user["id"]) post.save() clan = community.objects.all() print(len(clan)) for c in clan: print(c.community_blog) print(post.id) if post.id in c.community_blog: return redirect('community:clan-show', clan_id=c.id)
def getMsgs(request): if request.method == "GET": chat_msgs = [] friend_id = request.GET['f_id'] cu_user = request.session["username"] c_user = User.objects(email=cu_user)[0] c_u_prof = Profile.objects(user_id=c_user['id'])[0] msgs = c_u_prof['messages'] if (msgs): for msg in msgs: c_msg = Message.objects(id=msg)[0] if c_msg['sender'] == friend_id: chat_msgs.append(c_msg) return render(request, 'chat/priv_msg.html')
def exitClan(request, clan_id): print("exitClan") username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) clan = community.objects.get(id=clan_id) print(profile["clans_registered"]) print(clan["id"] in profile["clans_registered"]) Profile.objects(user_id=user["id"]).update_one( pull__clans_registered=clan["id"]) print("success") community.objects(id=clan_id).update_one(pull__participants=user["id"]) print("success") community.objects(id=clan_id).update_one(dec__no_of_participants=1) print("success") clans1 = [] profile1 = Profile.objects.get(user_id=user["id"]) for i in profile1["clans_registered"]: clan1 = community.objects.get(id=i) temp = dict() temp['name'] = clan1['name'] temp['clan_id'] = clan1['id'] temp['description'] = clan1['discription'] photo = clan1["photo"].read() my_string = base64.b64encode(photo) temp['clan_photo'] = my_string.decode('utf-8') list = [] for j in clan1['participants']: p = Profile.objects.get(user_id=j) photo1 = p["photo"].read() my_string1 = base64.b64encode(photo1) list.append(my_string1.decode('utf-8')) temp['members_photos'] = list clans1.append(temp) #print(clans) return render(request, 'clans/clans.html', {"clans1": clans1})
def like(request): if request.method == 'GET': post_id = request.GET['postId'] username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) post = Post.objects.get(id=post_id) if user['id'] not in post['likedBy']: post.likes += 1 post.likedBy.append(user["id"]) post.save() else: post.likes -= 1 post.likedBy.remove(user["id"]) post.save() return HttpResponse('Success') else: return HttpResponse(Failure)
def submit_challanges(request, challange_id): print("submit_challanges") if request.method == 'POST': print("got into submit_challanges") if "photo" in request.FILES: username = request.session["username"] uid = User.objects(email=username)[0] photo = request.FILES["photo"] description = request.POST["discription"] ch = challange(done_by=uid['id'], discription=description) ch.proof_of_completion.put(photo, content_type='image/jpeg') ch.sent_for_review = True ch.clanChallange_id = challange_id ch.save() print(ch.id) clan = clanChallange.objects.get(id=challange_id) clanChallange.objects(id=challange_id).update_one( push__challange=ch.id) clan_id = clan['community_id'] return redirect('community:show_challanges', clan_id=clan_id)
def sendPrivMsg(request): if request.method == 'POST': print("got request") text = request.POST['msg'] frnd_id = request.POST['f_id'] print(text) print(frnd_id) cu_user = request.session["username"] c_user = User.objects(email=cu_user)[0] cu_prof = Profile.objects.get(user_id=c_user['id']) msg = Message(msg=text, sender=c_user['id'], reciever=frnd_id) msg.save() print("message saved") Profile.objects(user_id=c_user['id']).update_one( push__messages=msg['id']) Profile.objects(user_id=frnd_id).update_one(push__messages=msg['id']) print("Profiles changed") return HttpResponse('Success') else: return HttpResponse('Failure')
def post(request, clan_id): print("uploading a post") if request.method == 'POST': print("got into clan_post POST") if "photo" in request.FILES: username = request.session["username"] uid = User.objects(email=username)[0] print("uid", uid["id"]) photo = request.FILES["photo"] description = request.POST["discription"] post = Post(description=description) post.image.put(photo, content_type='image/jpeg') post.owner = uid["id"] post.save() community.objects(id=clan_id).update_one( push__community_blog=post["id"]) return redirect('community:clan-show', clan_id=clan_id) else: return render(request, 'clans/clans.html', {"warning": "Please fill all the blanks"}) print('hiii') return redirect('community:clan-show', clan_id=clan_id)
def single_post(request): if request.method == "GET": print("came into single_post") post_id = request.GET["postId"] username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) temp = dict() post = Post.objects.get(id=post_id) temp['id'] = post['id'] po = post['owner'] po_p = Profile.objects.get(user_id=po) temp['owner'] = po_p['name'] temp['createdAt'] = post['createdAt'] temp['likes'] = post['likes'] temp['likedBy'] = post['likedBy'] temp['is_liked_by_curr_user'] = False if user['id'] in post['likedBy']: temp['is_liked_by_curr_user'] = True temp['comments'] = post['comments'] temp['description'] = post['description'] photo = post["image"].read() my_string = base64.b64encode(photo) temp["photo"] = my_string.decode('utf-8') # temp["owner_photo"] = my_string1.decode('utf-8') o_photo = po_p["photo"].read() o_my_string = base64.b64encode(o_photo) temp["ownerphoto"] = o_my_string.decode('utf-8') my_photo = profile["photo"].read() my_my_string = base64.b64encode(my_photo) temp["ownerphoto"] = my_my_string.decode('utf-8') comments = [] for k in post['comments']: comm = Comment.objects.get(id=k) print(comm) temp1 = dict() temp1['comment'] = comm['message'] temp1['reports'] = comm['message'] o = comm['owner'] own = User.objects(id=o)[0] own_p = Profile.objects.get(user_id=o) temp1['owner'] = own_p['name'] photo = own_p["photo"].read() ph_string = base64.b64encode(photo) temp1["photo"] = ph_string.decode('utf-8') comments.append(temp1) count = len(comments) print(count) return render( request, 'clans/modal.html', { 'myphoto': my_photo, 'post': temp, 'comments': comments, 'count': count }) else: return HttpResponse("failure")
def clan_show(request, clan_id): username = request.session["username"] user = User.objects(email=username)[0] profile = Profile.objects.get(user_id=user["id"]) clan = community.objects.get(id=clan_id) clan_id = clan['id'] is_head = False if user['id'] in clan['Heads']: is_head = True clan_challenges_today = [] clan_challenges = [] print(clan['group_challanges']) for ch in clan['group_challanges']: c = clanChallange.objects.get(id=ch) strt = c['created_date'].strftime('%Y-%m-%d') comp = c['complete_date'].strftime('%Y-%m-%d') tod = datetime.date.today().strftime('%Y-%m-%d') if int(tod[:4]) <= int(comp[:4]): if int(tod[5:7]) <= int(comp[5:7]): if int(tod[8:10]) <= int(comp[8:10]): print(c['created_date'].strftime('%Y-%m-%d')) print(datetime.date.today().strftime('%Y-%m-%d')) tempc = dict() tempc['name'] = c['name'] tempc['discription'] = c['discription'] if strt == tod and comp == tod: clan_challenges_today.append(tempc) print(clan_challenges_today) clan_members = [] for j in clan['participants']: temp1 = dict() p = Profile.objects.get(user_id=j) photo1 = p["photo"].read() my_string1 = base64.b64encode(photo1) temp1['photo'] = my_string1.decode('utf-8') print(p['name']) temp1['name'] = p['name'] temp1['discription'] = p['discription'] temp1['is_friends'] = False u = User.objects.get(id=p['user_id']) temp1['email'] = u['email'] if user['id'] in p['friends']: temp1['is_friends'] = True clan_members.append(temp1) clan_posts = [] photo = profile["photo"].read() my_string1 = base64.b64encode(photo) for c in clan['community_blog']: temp = dict() post = Post.objects.get(id=c) temp['id'] = post['id'] temp['owner'] = profile['name'] temp['createdAt'] = post['createdAt'] temp['likes'] = post['likes'] temp['likedBy'] = post['likedBy'] temp['is_liked_by_curr_user'] = False if user['id'] in post['likedBy']: temp['is_liked_by_curr_user'] = True temp['comments'] = post['comments'] temp['description'] = post['description'] photo = post["image"].read() my_string = base64.b64encode(photo) temp["photo"] = my_string.decode('utf-8') temp["owner_photo"] = my_string1.decode('utf-8') clan_posts.append(temp) #print(clan) return render( request, 'clans/clan_show.html', { 'clan_challenges_today': clan_challenges_today, 'clan_members': clan_members, 'clan_posts': clan_posts, "clan_id": clan_id, 'is_head': is_head, 'clan_challenges': clan_challenges })