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 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 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 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 operation(request): failed_email = [] data = request.POST np.random.seed = 347 create_password = lambda: ''.join( [random.choice(string.ascii_letters) for i in range(5)] + [random.choice(string.digits) for i in range(5)] + [random.choice(string.punctuation) for i in range(5)]) stud_path = os.path.join(os.path.dirname('__file__'), 'show_admin', 'user_list', 'students.csv') if data['next'] == 'Create User': students = pd.read_csv(stud_path) for studemail in students: user = User.objects.create_user(username=studemail, password=create_password()) user.save() profile = Profile() profile.user = request.user profile.is_voter = True profile.save() try: election = ElectionStatus.objects.all()[0] except: election = ElectionStatus() if data['next'] == 'Start Election': students = pd.read_csv(stud_path) for studemail in students: user = User.objects.get(username=studemail) pasw = create_password() user.set_password(pasw) user.save() try: send_mail( 'Credentials For Upcoming Election', f'Username: {studemail}\nPassword:{pasw}\nDo not share this credentials with anyone in any case.', '*****@*****.**', ['*****@*****.**'], fail_silently=False, ) except: failed_email.append(studemail) failed = np.array(failed_email) np.savetxt("failed.csv", failed, delimiter=",") if (len(failed_email) / len(students)) == 0.0: election.is_active = True election.save() else: return HttpResponse( "<h4>Election is not started as all the students have not recieved the email</h4>" ) if data['next'] == 'Start Polling': election.poll_started = True election.save() if data['next'] == 'Stop Polling': election.poll_started = False election.result_declared = True return HttpResponseRedirect(reverse('show_admin:result')) if data['next'] == 'Stop Election': election.is_active = False return HttpResponseRedirect(reverse('show_admin:admin'))
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 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 update_profile(self, nickname=None, img_url=None): if not nickname and not img_url: return try: row = Profile.objects.get(username=self.username) if nickname: row.nickname = nickname if img_url: row.img_url = img_url row.save() except Profile.DoesNotExist: Profile(username=self.username, nickname=nickname, img_url=img_url).save() error_code = 0 return error_code
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')