Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.shortcuts import render,HttpResponse
- import json
- from django.contrib.auth import authenticate, login
- from django.views.decorators.csrf import csrf_exempt
- from django.contrib.auth.models import User
- from django.db.models import Q # add this line to the beginning section
- from .models import *
- from django.template import RequestContext
- @csrf_exempt
- # Create your views here.
- def index(request):
- return HttpResponse("Appinion starts...")
- absoluteURL = 'http://triplemzim.pythonanywhere.com'
- @csrf_exempt
- def login1(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- # username = request.POST.get('username')
- # password = request.POST.get('password')
- username=data['username']
- password=data['password']
- user = authenticate(username=username,password=password)
- if user is not None:
- login(request,user)
- temp_dict ={}
- temp_dict['idUser']=user.pk
- temp_dict['message']='success'
- # sending_list = []
- # sending_list.append(temp_dict)
- data = json.dumps(temp_dict)
- return HttpResponse(data)
- else:
- return HttpResponse(json.dumps({'message': 'failure'}))
- @csrf_exempt
- def signup(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- # username = request.POST.get('username','')
- # password = request.POST.get('password','')
- # institution = request.POST.get('institution','')
- # email = request.POST.get('email','')
- # birthdate = request.POST.get('birthdate','')
- # firstname = request.POST.get('firstname','')
- # lastname = request.POST.get('lastname','')
- username=data['username']
- password=data['password']
- institution=data['institution']
- email=data['email']
- firstname=data['firstname']
- lastname=data['lastname']
- birthdate=data['birthdate']
- print(username+' '+password+' '+birthdate+' '+email)
- dummy = DummyPhoto.objects.get(pk = 1)
- try:
- user = User.objects.create_user(username=username,password=password,first_name=firstname,last_name=lastname,email=email)
- profile = Profile.objects.create(user=user,institution=institution,dateOfBirth=birthdate,secretKey = password);
- profile.profilePhoto.save(str(user.pk)+'profile.jpg',dummy.dummyProfile)
- profile.save()
- user.save();
- # profile.save();
- except Exception as e:
- return HttpResponse("failure "+ str(e))
- return HttpResponse('success')
- @csrf_exempt
- # @login_required(login_url='/login')
- def trending(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- post_count = data['post_count']
- latest_surveys = Survey.objects.filter(inGroup = False).order_by('-creationTime')[post_count:post_count+10]
- print("I am here ",latest_surveys)
- sending_list = []
- for S in latest_surveys:
- P = Poll.objects.filter(idSurvey = S)
- O = OptionValues.objects.get(idPoll = P[0])
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- total_comments = len(Comment.objects.filter(idSurvey = S))
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- temp_dict = {}
- temp_dict['idUser']=U.pk
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
- temp_dict['idSurvey']=S.pk
- temp_dict['title']=S.title
- temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- temp_dict['idPoll']=P[0].pk
- temp_dict['pollQuestion']=P[0].description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['recommend']=total_recommends
- temp_dict['comment']=total_comments
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- # @login_required(login_url='/login')
- def home(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- post_count = data['post_count']
- idUser = User.objects.get(pk = data['idUser'])
- # latest_surveys = []
- # subscriptions = Subscription.objects.filter(idSubscriber = idUser)
- # for ss in subscriptions:
- # latest_surveys.extend(Survey.objects.filter(idUser = ss.idSubscribee))
- # latest_surveys_new=latest_surveys[post_count:post_count+10];
- latest_surveys = Survey.objects.filter(inGroup = False).filter(idUser__userSubscribee__idSubscriber = idUser.pk).order_by('-creationTime')[post_count:post_count+10] # using related name for subscription
- sending_list = []
- for S in latest_surveys:
- P = Poll.objects.filter(idSurvey = S)[:1]
- O = OptionValues.objects.get(idPoll = P[0])
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- total_comments = len(Comment.objects.filter(idSurvey = S))
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- temp_dict = {}
- temp_dict['idUser']=U.pk
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
- temp_dict['idSurvey']=S.pk
- temp_dict['title']=S.title
- temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- temp_dict['idPoll']=P[0].pk
- temp_dict['pollQuestion']=P[0].description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['recommend']=total_recommends
- temp_dict['comment']=total_comments
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- # @login_required(login_url='/login')
- def load_profile(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- post_count = data['post_count']
- current = data['current']
- target = data['target']
- targetUser = User.objects.get(pk = target)
- print(targetUser)
- subscriber_count = len(Subscription.objects.filter(idSubscribee = target))
- subscription_count = len(Subscription.objects.filter(idSubscriber = target))
- # group_count = len(Group.objects.filter(idAdmin = target))
- group_count = len(GroupMember.objects.filter(idUser = target))
- flag = ''
- if current == target:
- flag = 'own_profile'
- elif len(Subscription.objects.filter(idSubscriber = current).filter(idSubscribee = target)) > 0 :
- flag = 'subscribed_profile'
- else:
- flag = 'unsubscribed_profile'
- head = {'name': (targetUser.first_name+' '+targetUser.last_name),'subscriber_count':subscriber_count,'subscription_count':subscription_count,'group_count':group_count,'flag':flag}
- profile = Profile.objects.get(user = target)
- head['image_url']=absoluteURL+str(profile.profilePhoto.url)
- latest_surveys = Survey.objects.filter(inGroup = False).filter(idUser = target).order_by('-creationTime')[post_count:post_count+10]
- sending_list = []
- sending_list.append(head)
- for S in latest_surveys:
- P = Poll.objects.filter(idSurvey = S)
- # print(S)
- # print("length of poll: ",len(P))
- # return HttpResponse('failure')
- O = OptionValues.objects.get(idPoll = P[0])
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- total_comments = len(Comment.objects.filter(idSurvey = S))
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- temp_dict = {}
- temp_dict['idUser']=U.pk
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
- temp_dict['idSurvey']=S.pk
- temp_dict['title']=S.title
- temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- temp_dict['idPoll']=P[0].pk
- temp_dict['pollQuestion']=P[0].description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['recommend']=total_recommends
- temp_dict['comment']=total_comments
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- # @login_required(login_url='/login')
- def load_survey(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idSurvey = data['idSurvey']
- idUser = data['idUser']
- sending_list = {}
- S = Survey.objects.get(pk = idSurvey)
- P = Poll.objects.filter(idSurvey = S)
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- head = {}
- head['idUser']=U.pk
- head['title']=S.title
- head['name']=U.first_name + ' ' + U.last_name
- head['photo']=absoluteURL+str(profile.profilePhoto.url)
- head['idSurvey']=S.pk
- head['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- head['recommend']=total_recommends
- sending_list['head'] = head
- ps = []
- for polls in P:
- temp_dict = {}
- O = OptionValues.objects.get(idPoll = polls)
- vote = Vote.objects.filter(idUser = idUser).filter(idPoll = polls)
- if(len(vote)>0):
- vote_idx = vote[0].optionIndex
- else:
- vote_idx = -1
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- # temp_dict['idPoll']=P[0].pk
- temp_dict['idPoll']=polls.pk
- # temp_dict['pollQuestion']=P[0].description
- temp_dict['pollQuestion']=polls.description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- temp_dict['vote_idx']=vote_idx #vote index of the current user, -1 if none
- ps.append(temp_dict)
- sending_list['polls']=ps
- comment = Comment.objects.filter(idSurvey = S)
- comment_list = []
- for cm in comment:
- temp_dict = {}
- temp_dict['idUser'] = cm.idUser.pk
- temp_dict['name'] = cm.idUser.first_name + ' '+ cm.idUser.last_name
- temp_dict['text'] = cm.text
- profile = Profile.objects.get(user = cm.idUser.pk)
- temp_dict['photo'] = absoluteURL+str(profile.profilePhoto.url)
- comment_list.append(temp_dict)
- sending_list['comments']=comment_list
- # print(sending_list)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def load_survey_vote(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idSurvey = data['idSurvey']
- idUser = data['idUser']
- sending_list = {}
- S = Survey.objects.get(pk = idSurvey)
- P = Poll.objects.filter(idSurvey = S)
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- head = {}
- head['title']=S.title
- head['name']=U.first_name + ' ' + U.last_name
- head['photo']= absoluteURL+str(profile.profilePhoto.url)
- head['idSurvey']=S.pk
- head['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- head['recommend']=total_recommends
- sending_list['head'] = head
- ps = []
- for polls in P:
- temp_dict = {}
- O = OptionValues.objects.get(idPoll = polls)
- vote = Vote.objects.filter(idUser = idUser).filter(idPoll = polls)
- if(len(vote)>0):
- vote_idx = vote[0].optionIndex
- else:
- vote_idx = -1
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- option_list_new=[]
- total_votes=O.value0+O.value1+O.value2+O.value3
- for i in range(len(option_list)):
- if(option_list[i]!=""):
- option_list_new.append({'index':i,'text':option_list[i]})
- temp_dict['idPoll']=polls.pk
- temp_dict['pollQuestion']=polls.description
- temp_dict['selectedAnswer']="poll"+str(polls.pk)
- temp_dict['pollImage']='nafis.jpg'
- temp_dict['option_list']=option_list_new
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- temp_dict['vote_idx']=vote_idx #vote index of the current user, -1 if none
- ps.append(temp_dict)
- sending_list['polls']=ps
- # comment = Comment.objects.filter(idSurvey = S)
- # comment_list = []
- # for cm in comment:
- # temp_dict = {}
- # temp_dict['idUser'] = cm.idUser.pk
- # temp_dict['name'] = cm.idUser.first_name + ' '+ cm.idUser.last_name
- # temp_dict['text'] = cm.text
- # profile = Profile.objects.get(pk = idUser)
- # temp_dict['photo'] = 'nafis.jpg'
- # comment_list.append(temp_dict)
- # sending_list['comments']=comment_list
- # print(sending_list)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- # @login_required(login_url='/login')
- def alter_subscription(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- current = User.objects.get(pk = data['current'])
- target = User.objects.get(pk = data['target'])
- existing_data = Subscription.objects.filter(idSubscriber = current).filter(idSubscribee = target)
- if len(existing_data)>0 :
- existing_data.delete()
- else:
- try:
- Subscription(idSubscriber = current, idSubscribee = target).save()
- except Exception as e:
- return HttpResponse("failure")
- return HttpResponse('success')
- @csrf_exempt
- # @login_required(login_url='/login')
- def update_vote(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = data['idUser']
- voteList = data['voteList']
- for V in voteList:
- idPoll = V['idPoll']
- optionIndex = V['optionIndex']
- option_values = OptionValues.objects.get(idPoll = idPoll)
- vote = Vote.objects.filter(idUser = idUser).filter(idPoll = idPoll)
- if(len(vote)>0):
- if(vote[0].optionIndex != optionIndex):
- if optionIndex == 0:
- option_values.value0+=1
- elif optionIndex == 1:
- option_values.value1+=1
- elif optionIndex ==2:
- option_values.value2+=1
- else:
- option_values.value3+=1
- if vote[0].optionIndex == 0:
- option_values.value0-=1
- elif vote[0].optionIndex == 1:
- option_values.value1-=1
- elif vote[0].optionIndex ==2:
- option_values.value2-=1
- else:
- option_values.value3-=1
- vote[0].optionIndex = optionIndex
- vote[0].save()
- else:
- if optionIndex == 0:
- option_values.value0+=1
- elif optionIndex == 1:
- option_values.value1+=1
- elif optionIndex ==2:
- option_values.value2+=1
- else:
- option_values.value3+=1
- U = User.objects.get(pk = idUser)
- P = Poll.objects.get(pk = idPoll)
- try:
- Vote(idUser = U, idPoll = P, optionIndex = optionIndex).save()
- except Exception as e:
- return HttpResponse('failure')
- option_values.save()
- return HttpResponse('success')
- @csrf_exempt
- # @login_required(login_url='/login')
- def comment_recommend_report(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = data['idUser']
- idSurvey = data['idSurvey']
- text = data['text']
- flag = data['flag']
- # print("i got this",text)
- U = User.objects.get(pk = idUser)
- S = Survey.objects.get(pk=idSurvey)
- if flag == 'comment':
- try:
- Comment(idUser = U, idSurvey = S,text = text).save()
- except Exception as e:
- return HttpResponse('failure')
- elif flag == 'recommend':
- try:
- Recommend(idUser = U, idSurvey = S,text = text).save()
- except Exception as e:
- return HttpResponse('failure')
- elif flag == 'report':
- try:
- Report(idUser = U, idSurvey = S,text = text).save()
- except Exception as e:
- return HttpResponse('failure')
- return HttpResponse('success')
- @csrf_exempt
- def fetch_profile(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = data['idUser']
- user = User.objects.get(pk = idUser)
- profile = Profile.objects.get(user = user)
- temp_dict = {}
- temp_dict['username'] = user.username
- temp_dict['first_name']=user.first_name
- temp_dict['last_name']=user.last_name
- temp_dict['email']=user.email
- temp_dict['institution'] = profile.institution
- temp_dict['profilePhoto']= absoluteURL+str(profile.profilePhoto.url)
- temp_dict['dateOfBirth']=str(profile.dateOfBirth)
- sending_list= []
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def save_profile(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = data['idUser']
- old_password = data['old_password']
- user = User.objects.get(pk = idUser)
- loggedUser = authenticate(username = user.username , password = old_password)
- if loggedUser is not None:
- profile = Profile.objects.get(user = loggedUser)
- loggedUser.first_name = data['first_name']
- loggedUser.last_name = data['last_name']
- profile.institution = data['institution']
- # profile photo needs to be updated
- profile.dateOfBirth = data['dateOfBirth']
- new_password = data['new_password']
- if len(new_password) >=3 :
- loggedUser.set_password(new_password)
- profile.secretKey = new_password
- try:
- loggedUser.save()
- profile.save()
- except Exception as e:
- return HttpResponse('failure')
- return HttpResponse('success')
- return HttpResponse('failure')
- @csrf_exempt
- def add_survey(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- S = data['survey']
- user = User.objects.get(pk = idUser.pk)
- survey = Survey()
- try:
- if S['inGroup'] == False :
- survey = Survey(idUser = user,title = S['title'], category = S['category'],isPaid = S['isPaid'], endTime = S['endTime'],inGroup = False)
- else:
- idGroup = Group.objects.get(pk = S['idGroup'])
- survey = Survey(idUser = user,title = S['title'], category = S['category'],isPaid = S['isPaid'], endTime = S['endTime'],inGroup = True, idGroup = idGroup)
- survey.save()
- except Exception as e:
- return HttpResponse('1stfailure')
- poll_list = data['poll_list']
- for P in poll_list:
- temp_p = Poll(idSurvey = survey, tags = P['tags'],description = P['description'],pollImage='dummy.jpg')
- temp_option_list = P['option_list']
- try:
- temp_p.save()
- except Exception as e:
- survey.delete()
- return HttpResponse('3rdfailure')
- if(len(temp_option_list)==0):
- return HttpResponse('2ndfailure')
- option_values = OptionValues(idPoll = temp_p, name0 = temp_option_list[0])
- # print("option 1 : ",temp_option_list[0])
- if len(temp_option_list)>=2:
- option_values.name1= temp_option_list[1]
- if len(temp_option_list)>=3:
- option_values.name2= temp_option_list[2]
- if len(temp_option_list)>=4:
- option_values.name3= temp_option_list[3]
- print(len(temp_option_list),' ',temp_option_list)
- try:
- option_values.save()
- except Exception as e:
- survey.delete()
- temp_p.delete()
- return HttpResponse('4thfailure')
- return HttpResponse('success')
- @csrf_exempt
- def create_group(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- groupName = data['groupName']
- description = data['description']
- group = Group(idAdmin = idUser, groupName = groupName, description = description)
- dummy = DummyPhoto.objects.get(pk = 1)
- group.groupImage.save(str(idUser.pk)+'group.jpg', dummy.dummyGroup)
- # group.groupImage = dummy.dummyGroup
- try:
- group.save()
- GroupMember(idUser = idUser,idGroup = group,idType = "Service").save()
- except Exception as e:
- return HttpResponse('failure')
- return HttpResponse('success')
- @csrf_exempt
- def member_list(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idGroup = data['idGroup']
- item_count = data['item_count']
- admin = Group.objects.get(pk = idGroup)
- profile = Profile.objects.get(user = admin.idAdmin.pk)
- memberList = GroupMember.objects.filter(idGroup = idGroup).exclude(idUser = admin.idAdmin)[item_count:item_count+10]
- # memberList = GroupMember.objects.filter(idGroup = idGroup).exclude(idUser = admin.idAdmin)
- temp_dict = {}
- temp_dict['idUser'] = admin.idAdmin.pk
- temp_dict['name'] = admin.idAdmin.first_name + ' ' + admin.idAdmin.last_name
- temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
- profile = Profile.objects.get(user = admin.idAdmin)
- temp_dict['institution'] = profile.institution
- sending_list = []
- sending_list.append(temp_dict)
- for ml in memberList:
- temp_dict = {}
- temp_dict['idUser'] = ml.idUser.pk
- profile = Profile.objects.get(user = ml.idUser.pk)
- temp_dict['name'] = ml.idUser.first_name + ' ' + ml.idUser.last_name
- temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
- temp_dict['institution'] = profile.institution
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def ar_member(request):
- if request.method == 'POST':
- data_byte = request.body.decode('utf-8')
- data = json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- target = User.objects.get(pk = data['target_idUser'])
- idGroup = Group.objects.get(pk = data['idGroup'])
- operation_type = data['operationType']
- # if(idUser == target):
- # return HttpResponse('failure')
- if Group.objects.filter(pk = idGroup.pk).filter(idAdmin = idUser).count()==1 :
- info = GroupMember.objects.filter(idGroup = idGroup).filter(idUser = target)
- if(idUser == target):
- return HttpResponse('failure')
- if len(info) > 0 :
- info[0].delete()
- else:
- if operation_type =='add':
- member = GroupMember(idUser = target, idGroup = idGroup)
- try:
- member.save()
- except Exception as e:
- return HttpResponse('failure')
- info = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup)
- if len(info) > 0:
- info[0].delete()
- return HttpResponse('success')
- else:
- info = GroupMember.objects.filter(idGroup = idGroup).filter(idUser = target)
- if len(info) > 0:
- if idUser == target:
- info[0].delete()
- else:
- return HttpResponse('1failure')
- else:
- mr = MemberRequest(idRequest = target, idRequester = idUser,idGroup = idGroup)
- req = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup)
- if len(req) > 0:
- if idUser == target:
- # req = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup).get()
- req[0].delete()
- return HttpResponse('success')
- try:
- mr.save()
- except Exception as e:
- return HttpResponse('3failure')
- return HttpResponse('success')
- @csrf_exempt
- def watch_list(request):
- context = RequestContext(request)
- if request.method == 'POST':
- data_byte = request.body.decode('utf-8')
- data = json.loads(data_byte)
- idUser = data['idUser']
- item_count = data['item_count']
- item_type = data['item_type']
- sending_list = []
- if item_type == 'subscription':
- subscription = Subscription.objects.filter(idSubscriber = idUser)[item_count:item_count+10]
- for sbs in subscription:
- user = User.objects.get(pk = sbs.idSubscribee.pk)
- profile = Profile.objects.get(user = user.pk)
- temp_dict = {}
- temp_dict['idUser'] = user.pk
- temp_dict['name'] = user.first_name+' '+user.last_name
- temp_dict['image']= absoluteURL+str(profile.profilePhoto.url)
- sending_list.append(temp_dict)
- elif item_type == 'subscriber':
- subscription = Subscription.objects.filter(idSubscribee = idUser)[item_count:item_count+10]
- for sbs in subscription:
- user = User.objects.get(pk = sbs.idSubscriber.pk)
- profile = Profile.objects.get(user = user.pk)
- temp_dict = {}
- temp_dict['idUser'] = user.pk
- temp_dict['name'] = user.first_name + ' ' + user.last_name
- temp_dict['image']= absoluteURL+str(profile.profilePhoto.url)
- sending_list.append(temp_dict)
- elif item_type == 'group':
- groups = GroupMember.objects.filter(idUser = idUser)[item_count:item_count+10]
- for gps in groups:
- temp_dict = {}
- group = Group.objects.get(pk = gps.idGroup.pk)
- if group.idAdmin.pk == idUser:
- temp_dict['isAdmin'] = True
- temp_user = User.objects.get(pk = idUser)
- temp_dict['adminName'] = temp_user.first_name+' '+temp_user.last_name
- else:
- temp_dict['isAdmin'] = False
- temp_dict['name'] = group.groupName
- temp_dict['idGroup'] = group.pk
- temp_dict['image'] = absoluteURL+str(group.groupImage.url)
- sending_list.append(temp_dict)
- else:
- return HttpResponse('failure')
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def visit_group(request):
- if request.method == 'POST':
- data_byte = request.body.decode('utf-8')
- data = json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- post_count = data['post_count']
- #print(data['idGroup'])
- idGroup = Group.objects.get(pk = data['idGroup'])
- sending_list = []
- head = {}
- # poll_list = []
- head['name'] = idGroup.groupName
- head['idGroup'] = idGroup.pk
- head['image_url'] = absoluteURL+str(idGroup.groupImage.url)
- head['member_count'] = GroupMember.objects.filter(idGroup = idGroup).count()
- if Group.objects.filter(id = idGroup.pk).filter(idAdmin = idUser).count() > 0:
- head['user_type'] = 'admin'
- head['request_count'] = MemberRequest.objects.filter(idGroup = idGroup).count()
- elif GroupMember.objects.filter(idGroup = idGroup).filter(idUser = idUser).count() > 0:
- head['user_type'] = 'member'
- elif MemberRequest.objects.filter(idGroup = idGroup,idRequest = idUser).count() > 0:
- head['user_type'] = 'infiltrator'
- else:
- head['user_type'] = 'spy'
- sending_list.append(head)
- if head['user_type'] != 'spy':
- latest_surveys = Survey.objects.filter(inGroup = True).filter(idGroup = idGroup).order_by('-creationTime')[post_count:post_count+10]
- for S in latest_surveys:
- P = Poll.objects.filter(idSurvey = S)[:1]
- O = OptionValues.objects.get(idPoll = P)
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- total_comments = len(Comment.objects.filter(idSurvey = S))
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- temp_dict = {}
- temp_dict['idUser']=U.pk
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['image_url']=absoluteURL+str(profile.profilePhoto.url)
- temp_dict['idSurvey']=S.pk
- temp_dict['title']=S.title
- temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- temp_dict['idPoll']=P[0].pk
- temp_dict['pollQuestion']=P[0].description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['recommend']=total_recommends
- temp_dict['comment']=total_comments
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy.jpg value for now
- temp_dict['vote_count_list']=vote_count_list
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- '''Rahat'''
- @csrf_exempt
- def request_list(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser=data['idUser']
- idGroup=data['idGroup']
- request_count = data['request_count']
- # items = MemberRequest.objects.filter(idGroup = idGroup)[request_count:request_count+10]
- items = MemberRequest.objects.filter(idGroup = idGroup)
- sending_list = []
- for item in items:
- temp_dict = {}
- temp_dict['name'] = item.idRequest.first_name + ' ' + item.idRequest.last_name
- temp_dict['idUser'] = item.idRequest.pk
- profile = Profile.objects.get(user = item.idRequest.pk)
- temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
- temp_dict['institution'] = profile.institution
- sending_list.append(temp_dict)
- # item={'name':'Dr. cat','image_url':'cat.png','idUser':1, 'institution':'BUET'}
- # for i in range(5):
- # sending_list.append(item)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- # @login_required(login_url='/login')
- def non_members(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- idGroup = Group.objects.get(pk = data['idGroup'])
- item_count = data['item_count']
- subscribers = Subscription.objects.filter(idSubscribee = idUser.pk).exclude(idSubscriber__groupmember__idGroup = idGroup.pk)[item_count:item_count+10]
- print(subscribers)
- sending_list = []
- for subs in subscribers:
- temp_dict = {}
- temp_dict['idUser'] = subs.idSubscriber.pk
- temp_dict['name'] = subs.idSubscriber.first_name+ ' ' + subs.idSubscriber.last_name
- profile = Profile.objects.get(user = subs.idSubscriber)
- temp_dict['institution'] = profile.institution
- temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def search_db(request):
- if request.method == 'POST':
- data_byte=request.body.decode('utf-8')
- data=json.loads(data_byte)
- idUser = User.objects.get(pk = data['idUser'])
- item_count=data['item_count']
- search_type = data['type']
- string = data['string']
- sending_list = []
- if search_type == 'person':
- users = User.objects.filter(Q(first_name__icontains = string) | Q(last_name__icontains = string) | Q(profile__institution__icontains = string) )[item_count:item_count+10]
- for U in users:
- temp_dict = {}
- # profile = Profile.objects.get(idUser = U)
- profile = Profile.objects.get(user = U)
- temp_dict['idUser'] = U.pk
- # temp_dict['firstName']=U.first_name
- # temp_dict['lastName']=U.last_name
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['photo']=absoluteURL+str(profile.profilePhoto.url)
- temp_dict['subscription'] = Subscription.objects.filter(idSubscriber = idUser).count()
- sending_list.append(temp_dict)
- elif search_type == 'group':
- groups = Group.objects.filter(groupName__icontains = string)[item_count:item_count+10]
- for G in groups:
- temp_dict = {}
- temp_dict['idGroup']=G.pk
- temp_dict['name'] = G.groupName
- temp_dict['type'] = G.groupType
- temp_dict['photo'] = absoluteURL+str(G.groupImage.url)
- temp_dict['memberNo'] = GroupMember.objects.filter(idGroup = G).count()
- sending_list.append(temp_dict)
- else:
- surveys = Survey.objects.filter(Q(title__icontains = string) | Q(category__icontains = string)).exclude( Q(inGroup=True), ~Q(idGroup__groupmember__idUser = idUser) )[item_count:item_count+10]
- sending_list = []
- for S in surveys:
- P = Poll.objects.filter(idSurvey = S)[:1]
- O = OptionValues.objects.get(idPoll = P)
- U = User.objects.get(pk = S.idUser.pk)
- profile = Profile.objects.get(user = U.pk)
- total_recommends = len(Recommend.objects.filter(idSurvey = S))
- total_comments = len(Comment.objects.filter(idSurvey = S))
- option_list = [O.name0,O.name1,O.name2,O.name3]
- vote_count_list = [O.value0,O.value1,O.value2,O.value3]
- total_votes=O.value0+O.value1+O.value2+O.value3
- temp_dict = {}
- temp_dict['name']=U.first_name+" "+U.last_name
- temp_dict['photo']=absoluteURL+str(profile.profilePhoto.url)
- temp_dict['idSurvey']=S.pk
- temp_dict['title']=S.title
- temp_dict['idUser']=U.pk
- temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
- temp_dict['idPoll']=P[0].pk
- temp_dict['pollQuestion']=P[0].description
- temp_dict['pollImage']='dummy.jpg'
- temp_dict['recommend']=total_recommends
- temp_dict['comment']=total_comments
- temp_dict['option_list']=option_list
- temp_dict['votes']=total_votes #dummy value for now
- temp_dict['vote_count_list']=vote_count_list
- sending_list.append(temp_dict)
- data = json.dumps(sending_list)
- return HttpResponse(data)
- @csrf_exempt
- def save_image(request):
- # if request.method == 'POST':
- print('I am in save_image function')
- Type = request.POST.get('type','')
- if Type == 'group':
- idGroup = int(request.POST.get('dest_id'))
- obj = Group.objects.get(pk = idGroup)
- obj.groupImage.delete(True)
- obj.groupImage.save(str(idGroup)+(str(datetime.datetime.now().time())[0:5])+'group.jpg',request.FILES['file'])
- try:
- obj.save()
- return HttpResponse('success')
- except Exception as e:
- return HttpResponse('failure')
- elif Type == 'profile':
- idUser = int(request.POST.get('dest_id'))
- obj = Profile.objects.get(user = idUser)
- obj.profilePhoto.delete(True)
- obj.profilePhoto.save(str(idUser)+(str(datetime.datetime.now().time())[0:5])+'profile.jpg', request.FILES['file'])
- try:
- obj.save()
- return HttpResponse('success')
- except Exception as e:
- return HttpResponse('failure')
- return HttpResponse('failure')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement