Advertisement
Guest User

views.py

a guest
Jan 17th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 34.45 KB | None | 0 0
  1. from django.shortcuts import render,HttpResponse
  2. import json
  3. from django.contrib.auth import authenticate, login
  4. from django.views.decorators.csrf import csrf_exempt
  5. from django.contrib.auth.models import User
  6. from django.db.models import Q  # add this line to the beginning section
  7. from .models import *
  8. from django.template import RequestContext
  9.  
  10.  
  11. @csrf_exempt
  12. # Create your views here.
  13. def index(request):
  14.     return HttpResponse("Appinion starts...")
  15.  
  16.  
  17. absoluteURL = 'http://triplemzim.pythonanywhere.com'
  18.  
  19. @csrf_exempt
  20.  
  21. def login1(request):
  22.     if request.method == 'POST':
  23.         data_byte=request.body.decode('utf-8')
  24.         data=json.loads(data_byte)
  25.         # username = request.POST.get('username')
  26.         # password = request.POST.get('password')
  27.         username=data['username']
  28.         password=data['password']
  29.         user = authenticate(username=username,password=password)
  30.  
  31.         if user is not None:
  32.             login(request,user)
  33.             temp_dict ={}
  34.             temp_dict['idUser']=user.pk
  35.             temp_dict['message']='success'
  36.  
  37.             # sending_list = []
  38.             # sending_list.append(temp_dict)
  39.  
  40.             data = json.dumps(temp_dict)
  41.             return HttpResponse(data)
  42.            
  43.         else:
  44.             return HttpResponse(json.dumps({'message': 'failure'}))
  45.  
  46.  
  47. @csrf_exempt
  48.  
  49. def signup(request):
  50.     if request.method == 'POST':
  51.         data_byte=request.body.decode('utf-8')
  52.         data=json.loads(data_byte)
  53.         # username = request.POST.get('username','')
  54.         # password = request.POST.get('password','')
  55.         # institution = request.POST.get('institution','')
  56.         # email = request.POST.get('email','')
  57.         # birthdate = request.POST.get('birthdate','')
  58.         # firstname = request.POST.get('firstname','')
  59.         # lastname = request.POST.get('lastname','')
  60.         username=data['username']
  61.         password=data['password']
  62.         institution=data['institution']
  63.         email=data['email']
  64.         firstname=data['firstname']
  65.         lastname=data['lastname']
  66.         birthdate=data['birthdate']
  67.         print(username+' '+password+' '+birthdate+' '+email)
  68.         dummy = DummyPhoto.objects.get(pk = 1)
  69.  
  70.  
  71.         try:
  72.             user = User.objects.create_user(username=username,password=password,first_name=firstname,last_name=lastname,email=email)
  73.             profile = Profile.objects.create(user=user,institution=institution,dateOfBirth=birthdate,secretKey = password);
  74.             profile.profilePhoto.save(str(user.pk)+'profile.jpg',dummy.dummyProfile)
  75.             profile.save()
  76.             user.save();
  77.             # profile.save();
  78.         except Exception as e:
  79.             return HttpResponse("failure "+ str(e))
  80.  
  81.         return HttpResponse('success')
  82.  
  83. @csrf_exempt
  84. # @login_required(login_url='/login')
  85.  
  86. def trending(request):
  87.     if request.method == 'POST':
  88.         data_byte=request.body.decode('utf-8')
  89.         data=json.loads(data_byte)
  90.  
  91.         post_count = data['post_count']
  92.  
  93.  
  94.         latest_surveys = Survey.objects.filter(inGroup = False).order_by('-creationTime')[post_count:post_count+10]
  95.        
  96.         print("I am here ",latest_surveys)
  97.  
  98.         sending_list = []
  99.  
  100.         for S in latest_surveys:
  101.             P = Poll.objects.filter(idSurvey = S)
  102.             O = OptionValues.objects.get(idPoll = P[0])
  103.             U = User.objects.get(pk = S.idUser.pk)
  104.             profile = Profile.objects.get(user = U.pk)
  105.             total_recommends = len(Recommend.objects.filter(idSurvey = S))
  106.             total_comments = len(Comment.objects.filter(idSurvey = S))
  107.             option_list = [O.name0,O.name1,O.name2,O.name3]
  108.             vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  109.             total_votes=O.value0+O.value1+O.value2+O.value3
  110.  
  111.             temp_dict = {}
  112.             temp_dict['idUser']=U.pk
  113.             temp_dict['name']=U.first_name+" "+U.last_name
  114.             temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
  115.             temp_dict['idSurvey']=S.pk
  116.             temp_dict['title']=S.title
  117.             temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  118.             temp_dict['idPoll']=P[0].pk
  119.             temp_dict['pollQuestion']=P[0].description
  120.             temp_dict['pollImage']='dummy.jpg'
  121.             temp_dict['recommend']=total_recommends
  122.             temp_dict['comment']=total_comments
  123.             temp_dict['option_list']=option_list
  124.             temp_dict['votes']=total_votes                       #dummy.jpg value for now
  125.             temp_dict['vote_count_list']=vote_count_list
  126.  
  127.             sending_list.append(temp_dict)
  128.  
  129.  
  130.         data = json.dumps(sending_list)
  131.         return HttpResponse(data)
  132.  
  133. @csrf_exempt
  134. # @login_required(login_url='/login')
  135.  
  136. def home(request):
  137.     if request.method == 'POST':
  138.         data_byte=request.body.decode('utf-8')
  139.         data=json.loads(data_byte)
  140.  
  141.         post_count = data['post_count']
  142.         idUser = User.objects.get(pk = data['idUser'])
  143.  
  144.  
  145.         # latest_surveys = []
  146.  
  147.         # subscriptions = Subscription.objects.filter(idSubscriber = idUser)
  148.  
  149.         # for ss in subscriptions:
  150.         #   latest_surveys.extend(Survey.objects.filter(idUser = ss.idSubscribee))
  151.  
  152.  
  153.         # latest_surveys_new=latest_surveys[post_count:post_count+10];
  154.         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
  155.  
  156.         sending_list = []
  157.        
  158.        
  159.         for S in latest_surveys:
  160.             P = Poll.objects.filter(idSurvey = S)[:1]
  161.             O = OptionValues.objects.get(idPoll = P[0])
  162.             U = User.objects.get(pk = S.idUser.pk)
  163.             profile = Profile.objects.get(user = U.pk)
  164.             total_recommends = len(Recommend.objects.filter(idSurvey = S))
  165.             total_comments = len(Comment.objects.filter(idSurvey = S))
  166.             option_list = [O.name0,O.name1,O.name2,O.name3]
  167.             vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  168.             total_votes=O.value0+O.value1+O.value2+O.value3
  169.  
  170.             temp_dict = {}
  171.             temp_dict['idUser']=U.pk
  172.             temp_dict['name']=U.first_name+" "+U.last_name
  173.             temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
  174.             temp_dict['idSurvey']=S.pk
  175.             temp_dict['title']=S.title
  176.             temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  177.             temp_dict['idPoll']=P[0].pk
  178.             temp_dict['pollQuestion']=P[0].description
  179.             temp_dict['pollImage']='dummy.jpg'
  180.             temp_dict['recommend']=total_recommends
  181.             temp_dict['comment']=total_comments
  182.             temp_dict['option_list']=option_list
  183.             temp_dict['votes']=total_votes                        #dummy.jpg value for now
  184.             temp_dict['vote_count_list']=vote_count_list
  185.  
  186.             sending_list.append(temp_dict)
  187.        
  188.  
  189.  
  190.         data = json.dumps(sending_list)
  191.         return HttpResponse(data)
  192.  
  193.  
  194.  
  195.  
  196. @csrf_exempt
  197. # @login_required(login_url='/login')
  198.  
  199.  
  200. def load_profile(request):
  201.     if request.method == 'POST':
  202.         data_byte=request.body.decode('utf-8')
  203.         data=json.loads(data_byte)
  204.  
  205.         post_count = data['post_count']
  206.         current = data['current']
  207.         target = data['target']
  208.        
  209.         targetUser = User.objects.get(pk = target)
  210.         print(targetUser)
  211.  
  212.         subscriber_count = len(Subscription.objects.filter(idSubscribee = target))
  213.         subscription_count = len(Subscription.objects.filter(idSubscriber = target))
  214.         # group_count = len(Group.objects.filter(idAdmin = target))
  215.         group_count = len(GroupMember.objects.filter(idUser = target))
  216.  
  217.         flag = ''
  218.  
  219.         if current == target:
  220.             flag = 'own_profile'
  221.         elif len(Subscription.objects.filter(idSubscriber = current).filter(idSubscribee = target)) > 0 :
  222.             flag = 'subscribed_profile'
  223.         else:
  224.             flag = 'unsubscribed_profile'
  225.  
  226.         head = {'name': (targetUser.first_name+' '+targetUser.last_name),'subscriber_count':subscriber_count,'subscription_count':subscription_count,'group_count':group_count,'flag':flag}
  227.  
  228.         profile = Profile.objects.get(user = target)
  229.         head['image_url']=absoluteURL+str(profile.profilePhoto.url)
  230.        
  231.         latest_surveys = Survey.objects.filter(inGroup = False).filter(idUser = target).order_by('-creationTime')[post_count:post_count+10]
  232.        
  233.  
  234.         sending_list = []
  235.         sending_list.append(head)
  236.         for S in latest_surveys:
  237.             P = Poll.objects.filter(idSurvey = S)
  238.             # print(S)
  239.             # print("length of poll: ",len(P))
  240.             # return HttpResponse('failure')
  241.             O = OptionValues.objects.get(idPoll = P[0])
  242.             U = User.objects.get(pk = S.idUser.pk)
  243.             profile = Profile.objects.get(user = U.pk)
  244.             total_recommends = len(Recommend.objects.filter(idSurvey = S))
  245.             total_comments = len(Comment.objects.filter(idSurvey = S))
  246.             option_list = [O.name0,O.name1,O.name2,O.name3]
  247.             vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  248.             total_votes=O.value0+O.value1+O.value2+O.value3
  249.  
  250.             temp_dict = {}
  251.             temp_dict['idUser']=U.pk
  252.             temp_dict['name']=U.first_name+" "+U.last_name
  253.             temp_dict['photo']= absoluteURL+str(profile.profilePhoto.url)
  254.             temp_dict['idSurvey']=S.pk
  255.             temp_dict['title']=S.title
  256.  
  257.             temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  258.             temp_dict['idPoll']=P[0].pk
  259.             temp_dict['pollQuestion']=P[0].description
  260.             temp_dict['pollImage']='dummy.jpg'
  261.             temp_dict['recommend']=total_recommends
  262.             temp_dict['comment']=total_comments
  263.             temp_dict['option_list']=option_list
  264.             temp_dict['votes']=total_votes                      #dummy.jpg value for now
  265.             temp_dict['vote_count_list']=vote_count_list
  266.  
  267.             sending_list.append(temp_dict)
  268.        
  269.  
  270.  
  271.         data = json.dumps(sending_list)
  272.         return HttpResponse(data)
  273.  
  274.  
  275.  
  276. @csrf_exempt
  277. # @login_required(login_url='/login')
  278.  
  279.  
  280. def load_survey(request):
  281.     if request.method == 'POST':
  282.         data_byte=request.body.decode('utf-8')
  283.         data=json.loads(data_byte)
  284.  
  285.         idSurvey = data['idSurvey']
  286.         idUser = data['idUser']
  287.  
  288.         sending_list = {}
  289.  
  290.         S = Survey.objects.get(pk = idSurvey)
  291.         P = Poll.objects.filter(idSurvey = S)
  292.         U = User.objects.get(pk = S.idUser.pk)
  293.         profile = Profile.objects.get(user = U.pk)
  294.         total_recommends = len(Recommend.objects.filter(idSurvey = S))
  295.  
  296.  
  297.        
  298.         head = {}
  299.         head['idUser']=U.pk
  300.         head['title']=S.title
  301.         head['name']=U.first_name + ' ' + U.last_name
  302.         head['photo']=absoluteURL+str(profile.profilePhoto.url)
  303.         head['idSurvey']=S.pk
  304.        
  305.         head['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  306.         head['recommend']=total_recommends
  307.  
  308.         sending_list['head'] = head
  309.  
  310.         ps = []
  311.  
  312.         for polls in P:
  313.             temp_dict = {}
  314.             O = OptionValues.objects.get(idPoll = polls)
  315.             vote = Vote.objects.filter(idUser = idUser).filter(idPoll = polls)
  316.             if(len(vote)>0):
  317.                 vote_idx = vote[0].optionIndex
  318.             else:
  319.                 vote_idx = -1
  320.  
  321.  
  322.             option_list = [O.name0,O.name1,O.name2,O.name3]
  323.             vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  324.             total_votes=O.value0+O.value1+O.value2+O.value3
  325.  
  326.  
  327.             # temp_dict['idPoll']=P[0].pk
  328.             temp_dict['idPoll']=polls.pk
  329.  
  330.             # temp_dict['pollQuestion']=P[0].description
  331.             temp_dict['pollQuestion']=polls.description
  332.             temp_dict['pollImage']='dummy.jpg'
  333.             temp_dict['option_list']=option_list
  334.             temp_dict['votes']=total_votes                      #dummy.jpg value for now
  335.             temp_dict['vote_count_list']=vote_count_list
  336.             temp_dict['vote_idx']=vote_idx                  #vote index of the current user, -1 if none
  337.  
  338.             ps.append(temp_dict)
  339.         sending_list['polls']=ps
  340.  
  341.  
  342.         comment = Comment.objects.filter(idSurvey = S)
  343.  
  344.         comment_list = []
  345.         for cm in comment:
  346.             temp_dict = {}
  347.             temp_dict['idUser'] = cm.idUser.pk
  348.             temp_dict['name'] = cm.idUser.first_name + ' '+ cm.idUser.last_name
  349.             temp_dict['text'] = cm.text
  350.  
  351.             profile = Profile.objects.get(user = cm.idUser.pk)
  352.             temp_dict['photo'] = absoluteURL+str(profile.profilePhoto.url)
  353.  
  354.             comment_list.append(temp_dict)
  355.  
  356.         sending_list['comments']=comment_list
  357.         # print(sending_list)
  358.        
  359.         data = json.dumps(sending_list)
  360.         return HttpResponse(data)
  361.  
  362. @csrf_exempt
  363. def load_survey_vote(request):
  364.     if request.method == 'POST':
  365.         data_byte=request.body.decode('utf-8')
  366.         data=json.loads(data_byte)
  367.  
  368.         idSurvey = data['idSurvey']
  369.         idUser = data['idUser']
  370.  
  371.         sending_list = {}
  372.  
  373.         S = Survey.objects.get(pk = idSurvey)
  374.         P = Poll.objects.filter(idSurvey = S)
  375.         U = User.objects.get(pk = S.idUser.pk)
  376.         profile = Profile.objects.get(user = U.pk)
  377.         total_recommends = len(Recommend.objects.filter(idSurvey = S))
  378.        
  379.         head = {}
  380.        
  381.         head['title']=S.title
  382.         head['name']=U.first_name + ' ' + U.last_name
  383.         head['photo']= absoluteURL+str(profile.profilePhoto.url)
  384.         head['idSurvey']=S.pk
  385.        
  386.         head['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  387.         head['recommend']=total_recommends
  388.  
  389.         sending_list['head'] = head
  390.  
  391.         ps = []
  392.  
  393.         for polls in P:
  394.             temp_dict = {}
  395.             O = OptionValues.objects.get(idPoll = polls)
  396.             vote = Vote.objects.filter(idUser = idUser).filter(idPoll = polls)
  397.             if(len(vote)>0):
  398.                 vote_idx = vote[0].optionIndex
  399.             else:
  400.                 vote_idx = -1
  401.  
  402.  
  403.             option_list = [O.name0,O.name1,O.name2,O.name3]
  404.             vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  405.             option_list_new=[]
  406.             total_votes=O.value0+O.value1+O.value2+O.value3
  407.  
  408.             for i in range(len(option_list)):
  409.                 if(option_list[i]!=""):
  410.                     option_list_new.append({'index':i,'text':option_list[i]})
  411.  
  412.  
  413.  
  414.  
  415.             temp_dict['idPoll']=polls.pk
  416.  
  417.             temp_dict['pollQuestion']=polls.description
  418.             temp_dict['selectedAnswer']="poll"+str(polls.pk)
  419.             temp_dict['pollImage']='nafis.jpg'
  420.             temp_dict['option_list']=option_list_new
  421.             temp_dict['votes']=total_votes                      #dummy.jpg value for now
  422.             temp_dict['vote_count_list']=vote_count_list
  423.             temp_dict['vote_idx']=vote_idx                  #vote index of the current user, -1 if none
  424.  
  425.             ps.append(temp_dict)
  426.         sending_list['polls']=ps
  427.  
  428.  
  429.         # comment = Comment.objects.filter(idSurvey = S)
  430.  
  431.         # comment_list = []
  432.         # for cm in comment:
  433.         #   temp_dict = {}
  434.         #   temp_dict['idUser'] = cm.idUser.pk
  435.         #   temp_dict['name'] = cm.idUser.first_name + ' '+ cm.idUser.last_name
  436.         #   temp_dict['text'] = cm.text
  437.  
  438.         #   profile = Profile.objects.get(pk = idUser)
  439.         #   temp_dict['photo'] = 'nafis.jpg'
  440.  
  441.         #   comment_list.append(temp_dict)
  442.  
  443.         # sending_list['comments']=comment_list
  444.         # print(sending_list)
  445.        
  446.         data = json.dumps(sending_list)
  447.         return HttpResponse(data)
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461. @csrf_exempt
  462. # @login_required(login_url='/login')
  463.  
  464.  
  465. def alter_subscription(request):
  466.     if request.method == 'POST':
  467.         data_byte=request.body.decode('utf-8')
  468.         data=json.loads(data_byte)
  469.  
  470.         current = User.objects.get(pk = data['current'])
  471.         target = User.objects.get(pk = data['target'])
  472.  
  473.         existing_data = Subscription.objects.filter(idSubscriber = current).filter(idSubscribee = target)
  474.         if len(existing_data)>0 :
  475.             existing_data.delete()
  476.         else:
  477.             try:
  478.                 Subscription(idSubscriber = current, idSubscribee = target).save()
  479.             except Exception as e:
  480.                 return HttpResponse("failure")
  481.  
  482.         return HttpResponse('success')
  483.  
  484.  
  485.  
  486.  
  487.  
  488. @csrf_exempt
  489. # @login_required(login_url='/login')
  490.  
  491.  
  492. def update_vote(request):
  493.     if request.method == 'POST':
  494.         data_byte=request.body.decode('utf-8')
  495.         data=json.loads(data_byte)
  496.  
  497.         idUser = data['idUser']
  498.  
  499.         voteList = data['voteList']
  500.  
  501.         for V in voteList:
  502.             idPoll = V['idPoll']
  503.             optionIndex = V['optionIndex']
  504.  
  505.             option_values = OptionValues.objects.get(idPoll = idPoll)
  506.  
  507.             vote = Vote.objects.filter(idUser = idUser).filter(idPoll = idPoll)
  508.  
  509.             if(len(vote)>0):
  510.                 if(vote[0].optionIndex != optionIndex):
  511.                    
  512.                     if optionIndex == 0:
  513.                         option_values.value0+=1
  514.                     elif optionIndex == 1:
  515.                         option_values.value1+=1
  516.                     elif optionIndex ==2:
  517.                         option_values.value2+=1
  518.                     else:
  519.                         option_values.value3+=1
  520.  
  521.  
  522.                     if vote[0].optionIndex == 0:
  523.                         option_values.value0-=1
  524.                     elif vote[0].optionIndex == 1:
  525.                         option_values.value1-=1
  526.                     elif vote[0].optionIndex ==2:
  527.                         option_values.value2-=1
  528.                     else:
  529.                         option_values.value3-=1
  530.  
  531.                     vote[0].optionIndex = optionIndex
  532.                 vote[0].save()
  533.  
  534.  
  535.             else:
  536.                 if optionIndex == 0:
  537.                     option_values.value0+=1
  538.                 elif optionIndex == 1:
  539.                     option_values.value1+=1
  540.                 elif optionIndex ==2:
  541.                     option_values.value2+=1
  542.                 else:
  543.                     option_values.value3+=1
  544.  
  545.                 U = User.objects.get(pk = idUser)
  546.                 P = Poll.objects.get(pk = idPoll)
  547.  
  548.                 try:
  549.                     Vote(idUser = U, idPoll = P, optionIndex = optionIndex).save()
  550.                 except Exception as e:
  551.                     return HttpResponse('failure')
  552.            
  553.             option_values.save()
  554.  
  555.         return HttpResponse('success')
  556.  
  557.  
  558.  
  559.  
  560.  
  561. @csrf_exempt
  562. # @login_required(login_url='/login')
  563.  
  564.  
  565. def comment_recommend_report(request):
  566.     if request.method == 'POST':
  567.         data_byte=request.body.decode('utf-8')
  568.         data=json.loads(data_byte)
  569.  
  570.         idUser = data['idUser']
  571.         idSurvey = data['idSurvey']
  572.         text = data['text']
  573.         flag = data['flag']
  574.  
  575.         # print("i got this",text)
  576.  
  577.         U = User.objects.get(pk = idUser)
  578.         S = Survey.objects.get(pk=idSurvey)
  579.  
  580.         if flag == 'comment':
  581.             try:
  582.                 Comment(idUser = U, idSurvey = S,text = text).save()
  583.             except Exception as e:
  584.                 return HttpResponse('failure')
  585.         elif flag == 'recommend':
  586.             try:
  587.                 Recommend(idUser = U, idSurvey = S,text = text).save()
  588.             except Exception as e:
  589.                 return HttpResponse('failure')
  590.            
  591.         elif flag == 'report':
  592.             try:
  593.                 Report(idUser = U, idSurvey = S,text = text).save()
  594.             except Exception as e:
  595.                 return HttpResponse('failure')
  596.        
  597.  
  598.         return HttpResponse('success')
  599.  
  600.  
  601.  
  602. @csrf_exempt
  603.  
  604. def fetch_profile(request):
  605.     if request.method == 'POST':
  606.         data_byte=request.body.decode('utf-8')
  607.         data=json.loads(data_byte)
  608.  
  609.  
  610.         idUser = data['idUser']
  611.  
  612.         user = User.objects.get(pk = idUser)
  613.  
  614.         profile = Profile.objects.get(user = user)
  615.  
  616.         temp_dict = {}
  617.         temp_dict['username'] = user.username
  618.         temp_dict['first_name']=user.first_name
  619.         temp_dict['last_name']=user.last_name
  620.         temp_dict['email']=user.email
  621.  
  622.         temp_dict['institution'] = profile.institution
  623.         temp_dict['profilePhoto']= absoluteURL+str(profile.profilePhoto.url)
  624.         temp_dict['dateOfBirth']=str(profile.dateOfBirth)
  625.  
  626.         sending_list= []
  627.         sending_list.append(temp_dict)
  628.  
  629.  
  630.  
  631.         data = json.dumps(sending_list)
  632.         return HttpResponse(data)
  633.  
  634.  
  635. @csrf_exempt
  636.  
  637.  
  638. def save_profile(request):
  639.     if request.method == 'POST':
  640.         data_byte=request.body.decode('utf-8')
  641.         data=json.loads(data_byte)
  642.  
  643.         idUser = data['idUser']
  644.         old_password = data['old_password']
  645.         user = User.objects.get(pk = idUser)
  646.  
  647.  
  648.  
  649.         loggedUser = authenticate(username = user.username , password = old_password)
  650.  
  651.         if loggedUser is not None:
  652.             profile = Profile.objects.get(user = loggedUser)
  653.  
  654.             loggedUser.first_name = data['first_name']
  655.             loggedUser.last_name = data['last_name']
  656.             profile.institution = data['institution']
  657.             # profile photo needs to be updated
  658.             profile.dateOfBirth = data['dateOfBirth']
  659.             new_password = data['new_password']
  660.             if len(new_password) >=3 :
  661.                 loggedUser.set_password(new_password)
  662.                 profile.secretKey = new_password
  663.  
  664.  
  665.             try:
  666.                 loggedUser.save()
  667.                 profile.save()
  668.             except Exception as e:
  669.                 return HttpResponse('failure')
  670.  
  671.  
  672.  
  673.             return HttpResponse('success')
  674.  
  675.     return HttpResponse('failure')
  676.  
  677.  
  678. @csrf_exempt
  679.  
  680.  
  681. def add_survey(request):
  682.     if request.method == 'POST':
  683.         data_byte=request.body.decode('utf-8')
  684.         data=json.loads(data_byte)
  685.  
  686.  
  687.         idUser = User.objects.get(pk = data['idUser'])
  688.        
  689.         S = data['survey']
  690.         user = User.objects.get(pk = idUser.pk)
  691.         survey = Survey()
  692.         try:
  693.             if S['inGroup'] == False :
  694.                 survey = Survey(idUser = user,title = S['title'], category = S['category'],isPaid = S['isPaid'], endTime = S['endTime'],inGroup = False)
  695.                
  696.             else:
  697.                 idGroup = Group.objects.get(pk = S['idGroup'])
  698.                 survey = Survey(idUser = user,title = S['title'], category = S['category'],isPaid = S['isPaid'], endTime = S['endTime'],inGroup = True, idGroup = idGroup)
  699.             survey.save()
  700.         except Exception as e:
  701.             return HttpResponse('1stfailure')
  702.  
  703.  
  704.         poll_list = data['poll_list']
  705.  
  706.         for P in poll_list:
  707.  
  708.             temp_p = Poll(idSurvey = survey, tags = P['tags'],description = P['description'],pollImage='dummy.jpg')
  709.             temp_option_list = P['option_list']
  710.  
  711.             try:
  712.                 temp_p.save()
  713.             except Exception as e:
  714.                 survey.delete()
  715.                 return HttpResponse('3rdfailure')
  716.             if(len(temp_option_list)==0):
  717.                 return HttpResponse('2ndfailure')
  718.  
  719.             option_values = OptionValues(idPoll = temp_p, name0 = temp_option_list[0])
  720.  
  721.             # print("option 1 : ",temp_option_list[0])
  722.  
  723.             if len(temp_option_list)>=2:
  724.                 option_values.name1= temp_option_list[1]
  725.             if len(temp_option_list)>=3:
  726.                 option_values.name2= temp_option_list[2]
  727.             if len(temp_option_list)>=4:
  728.                 option_values.name3= temp_option_list[3]
  729.  
  730.             print(len(temp_option_list),' ',temp_option_list)
  731.            
  732.             try:
  733.  
  734.                 option_values.save()
  735.             except Exception as e:
  736.                 survey.delete()
  737.                 temp_p.delete()
  738.                 return HttpResponse('4thfailure')
  739.  
  740.  
  741.  
  742.  
  743.         return HttpResponse('success')
  744.  
  745.  
  746. @csrf_exempt
  747. def create_group(request):
  748.     if request.method == 'POST':
  749.         data_byte=request.body.decode('utf-8')
  750.         data=json.loads(data_byte)
  751.  
  752.  
  753.         idUser = User.objects.get(pk = data['idUser'])
  754.         groupName = data['groupName']
  755.         description = data['description']
  756.         group = Group(idAdmin = idUser, groupName = groupName, description = description)
  757.  
  758.         dummy = DummyPhoto.objects.get(pk = 1)
  759.         group.groupImage.save(str(idUser.pk)+'group.jpg', dummy.dummyGroup)
  760.         # group.groupImage = dummy.dummyGroup
  761.         try:
  762.             group.save()
  763.             GroupMember(idUser = idUser,idGroup = group,idType = "Service").save()
  764.         except Exception as e:
  765.             return HttpResponse('failure')
  766.         return HttpResponse('success')
  767.  
  768.  
  769. @csrf_exempt
  770. def member_list(request):
  771.     if request.method == 'POST':
  772.         data_byte=request.body.decode('utf-8')
  773.         data=json.loads(data_byte)
  774.  
  775.         idGroup = data['idGroup']
  776.         item_count = data['item_count']
  777.         admin = Group.objects.get(pk = idGroup)
  778.         profile = Profile.objects.get(user = admin.idAdmin.pk)
  779.         memberList = GroupMember.objects.filter(idGroup = idGroup).exclude(idUser = admin.idAdmin)[item_count:item_count+10]
  780.         # memberList = GroupMember.objects.filter(idGroup = idGroup).exclude(idUser = admin.idAdmin)
  781.         temp_dict = {}
  782.         temp_dict['idUser'] = admin.idAdmin.pk
  783.         temp_dict['name'] = admin.idAdmin.first_name + ' ' + admin.idAdmin.last_name
  784.         temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
  785.         profile = Profile.objects.get(user = admin.idAdmin)
  786.         temp_dict['institution'] = profile.institution
  787.  
  788.         sending_list = []
  789.         sending_list.append(temp_dict)
  790.         for ml in memberList:
  791.             temp_dict = {}
  792.             temp_dict['idUser'] = ml.idUser.pk
  793.             profile = Profile.objects.get(user = ml.idUser.pk)
  794.             temp_dict['name'] = ml.idUser.first_name + ' ' + ml.idUser.last_name
  795.             temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
  796.            
  797.            
  798.             temp_dict['institution'] = profile.institution
  799.             sending_list.append(temp_dict)
  800.  
  801.         data = json.dumps(sending_list)
  802.         return HttpResponse(data)
  803.  
  804.  
  805. @csrf_exempt
  806. def ar_member(request):
  807.     if request.method == 'POST':
  808.         data_byte = request.body.decode('utf-8')
  809.         data = json.loads(data_byte)
  810.  
  811.         idUser = User.objects.get(pk = data['idUser'])
  812.         target = User.objects.get(pk = data['target_idUser'])
  813.         idGroup = Group.objects.get(pk = data['idGroup'])
  814.         operation_type = data['operationType']
  815.         # if(idUser == target):
  816.         #   return HttpResponse('failure')
  817.  
  818.         if Group.objects.filter(pk = idGroup.pk).filter(idAdmin = idUser).count()==1 :
  819.             info = GroupMember.objects.filter(idGroup = idGroup).filter(idUser = target)
  820.             if(idUser == target):
  821.                 return HttpResponse('failure')
  822.             if len(info) > 0 :
  823.                 info[0].delete()
  824.                
  825.             else:
  826.                 if operation_type =='add':
  827.                     member = GroupMember(idUser = target, idGroup = idGroup)
  828.                     try:
  829.                         member.save()
  830.                     except Exception as e:
  831.                         return HttpResponse('failure')
  832.                
  833.                 info = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup)
  834.                 if len(info) > 0:
  835.                     info[0].delete()
  836.  
  837.             return HttpResponse('success')
  838.         else:
  839.             info = GroupMember.objects.filter(idGroup = idGroup).filter(idUser = target)
  840.             if len(info) > 0:
  841.                 if idUser == target:
  842.                     info[0].delete()
  843.                 else:
  844.                     return HttpResponse('1failure')
  845.             else:
  846.                
  847.                 mr = MemberRequest(idRequest = target, idRequester = idUser,idGroup = idGroup)
  848.                 req = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup)
  849.                 if len(req) > 0:
  850.                     if idUser == target:
  851.                         # req = MemberRequest.objects.filter(idRequest = target).filter(idGroup = idGroup).get()
  852.                         req[0].delete()
  853.                     return HttpResponse('success')
  854.                 try:
  855.                     mr.save()
  856.                 except Exception as e:
  857.                     return HttpResponse('3failure')
  858.  
  859.  
  860.  
  861.  
  862.         return HttpResponse('success')
  863.  
  864.  
  865. @csrf_exempt
  866. def watch_list(request):
  867.     context = RequestContext(request)
  868.     if request.method == 'POST':
  869.         data_byte = request.body.decode('utf-8')
  870.         data = json.loads(data_byte)
  871.  
  872.         idUser = data['idUser']
  873.         item_count = data['item_count']
  874.         item_type = data['item_type']
  875.  
  876.         sending_list = []
  877.         if item_type == 'subscription':
  878.             subscription = Subscription.objects.filter(idSubscriber = idUser)[item_count:item_count+10]
  879.  
  880.             for sbs in subscription:
  881.                 user = User.objects.get(pk = sbs.idSubscribee.pk)
  882.                 profile = Profile.objects.get(user = user.pk)
  883.  
  884.                 temp_dict = {}
  885.                 temp_dict['idUser'] = user.pk
  886.                 temp_dict['name'] = user.first_name+' '+user.last_name
  887.                 temp_dict['image']= absoluteURL+str(profile.profilePhoto.url)
  888.                 sending_list.append(temp_dict)
  889.  
  890.  
  891.         elif item_type == 'subscriber':
  892.             subscription = Subscription.objects.filter(idSubscribee = idUser)[item_count:item_count+10]
  893.  
  894.             for sbs in subscription:
  895.                 user = User.objects.get(pk = sbs.idSubscriber.pk)
  896.                 profile = Profile.objects.get(user = user.pk)
  897.  
  898.                 temp_dict = {}
  899.                 temp_dict['idUser'] = user.pk
  900.                 temp_dict['name'] = user.first_name + ' ' + user.last_name
  901.                 temp_dict['image']= absoluteURL+str(profile.profilePhoto.url)
  902.                 sending_list.append(temp_dict)
  903.         elif item_type == 'group':
  904.             groups = GroupMember.objects.filter(idUser = idUser)[item_count:item_count+10]
  905.  
  906.             for gps in groups:
  907.                 temp_dict = {}
  908.  
  909.                 group = Group.objects.get(pk = gps.idGroup.pk)
  910.  
  911.                 if group.idAdmin.pk == idUser:
  912.                     temp_dict['isAdmin'] = True
  913.                     temp_user = User.objects.get(pk = idUser)
  914.                     temp_dict['adminName'] = temp_user.first_name+' '+temp_user.last_name
  915.  
  916.                 else:
  917.                     temp_dict['isAdmin'] = False
  918.                 temp_dict['name'] = group.groupName
  919.                 temp_dict['idGroup'] = group.pk
  920.                 temp_dict['image'] = absoluteURL+str(group.groupImage.url)
  921.  
  922.                 sending_list.append(temp_dict)
  923.  
  924.  
  925.  
  926.         else:
  927.             return HttpResponse('failure')
  928.  
  929.         data = json.dumps(sending_list)
  930.         return HttpResponse(data)
  931.  
  932.  
  933. @csrf_exempt
  934. def visit_group(request):
  935.     if request.method == 'POST':
  936.         data_byte = request.body.decode('utf-8')
  937.         data = json.loads(data_byte)
  938.  
  939.         idUser = User.objects.get(pk = data['idUser'])
  940.         post_count = data['post_count']
  941.         #print(data['idGroup'])
  942.         idGroup = Group.objects.get(pk = data['idGroup'])
  943.  
  944.         sending_list = []
  945.         head = {}
  946.         # poll_list = []
  947.         head['name'] = idGroup.groupName
  948.         head['idGroup'] = idGroup.pk
  949.         head['image_url'] = absoluteURL+str(idGroup.groupImage.url)
  950.         head['member_count'] = GroupMember.objects.filter(idGroup = idGroup).count()
  951.  
  952.         if Group.objects.filter(id = idGroup.pk).filter(idAdmin = idUser).count() > 0:
  953.             head['user_type'] = 'admin'
  954.             head['request_count'] = MemberRequest.objects.filter(idGroup = idGroup).count()
  955.         elif GroupMember.objects.filter(idGroup = idGroup).filter(idUser = idUser).count() > 0:
  956.             head['user_type'] = 'member'
  957.         elif MemberRequest.objects.filter(idGroup = idGroup,idRequest = idUser).count() > 0:
  958.             head['user_type'] = 'infiltrator'
  959.         else:
  960.             head['user_type'] = 'spy'
  961.        
  962.         sending_list.append(head)
  963.  
  964.         if head['user_type'] != 'spy':
  965.             latest_surveys = Survey.objects.filter(inGroup = True).filter(idGroup = idGroup).order_by('-creationTime')[post_count:post_count+10]
  966.  
  967.             for S in latest_surveys:
  968.                 P = Poll.objects.filter(idSurvey = S)[:1]
  969.                 O = OptionValues.objects.get(idPoll = P)
  970.                 U = User.objects.get(pk = S.idUser.pk)
  971.                 profile = Profile.objects.get(user = U.pk)
  972.                 total_recommends = len(Recommend.objects.filter(idSurvey = S))
  973.                 total_comments = len(Comment.objects.filter(idSurvey = S))
  974.                 option_list = [O.name0,O.name1,O.name2,O.name3]
  975.                 vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  976.                 total_votes=O.value0+O.value1+O.value2+O.value3
  977.  
  978.                 temp_dict = {}
  979.                 temp_dict['idUser']=U.pk
  980.                 temp_dict['name']=U.first_name+" "+U.last_name
  981.                 temp_dict['image_url']=absoluteURL+str(profile.profilePhoto.url)
  982.                 temp_dict['idSurvey']=S.pk
  983.                 temp_dict['title']=S.title
  984.  
  985.                 temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  986.                 temp_dict['idPoll']=P[0].pk
  987.                 temp_dict['pollQuestion']=P[0].description
  988.                 temp_dict['pollImage']='dummy.jpg'
  989.                 temp_dict['recommend']=total_recommends
  990.                 temp_dict['comment']=total_comments
  991.                 temp_dict['option_list']=option_list
  992.                 temp_dict['votes']=total_votes                      #dummy.jpg value for now
  993.                 temp_dict['vote_count_list']=vote_count_list
  994.  
  995.                 sending_list.append(temp_dict)
  996.  
  997.        
  998.        
  999.  
  1000.  
  1001.         data = json.dumps(sending_list)
  1002.         return HttpResponse(data)
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.     '''Rahat'''
  1009.  
  1010. @csrf_exempt
  1011.  
  1012. def request_list(request):
  1013.     if request.method == 'POST':
  1014.         data_byte=request.body.decode('utf-8')
  1015.         data=json.loads(data_byte)
  1016.        
  1017.         idUser=data['idUser']
  1018.         idGroup=data['idGroup']
  1019.         request_count = data['request_count']
  1020.  
  1021.         # items = MemberRequest.objects.filter(idGroup = idGroup)[request_count:request_count+10]
  1022.         items = MemberRequest.objects.filter(idGroup = idGroup)
  1023.         sending_list = []
  1024.  
  1025.         for item in items:
  1026.             temp_dict = {}
  1027.             temp_dict['name'] = item.idRequest.first_name + ' ' + item.idRequest.last_name
  1028.             temp_dict['idUser'] = item.idRequest.pk
  1029.             profile = Profile.objects.get(user = item.idRequest.pk)
  1030.             temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
  1031.             temp_dict['institution'] = profile.institution
  1032.  
  1033.             sending_list.append(temp_dict)
  1034.  
  1035.  
  1036.         # item={'name':'Dr. cat','image_url':'cat.png','idUser':1, 'institution':'BUET'}
  1037.  
  1038.         # for i in range(5):
  1039.         #   sending_list.append(item)
  1040.  
  1041.  
  1042.         data = json.dumps(sending_list)
  1043.         return HttpResponse(data)
  1044.  
  1045.  
  1046.  
  1047. @csrf_exempt
  1048. # @login_required(login_url='/login')
  1049.  
  1050.  
  1051. def non_members(request):
  1052.     if request.method == 'POST':
  1053.         data_byte=request.body.decode('utf-8')
  1054.         data=json.loads(data_byte)
  1055.  
  1056.         idUser = User.objects.get(pk = data['idUser'])
  1057.         idGroup = Group.objects.get(pk = data['idGroup'])
  1058.         item_count = data['item_count']
  1059.  
  1060.         subscribers = Subscription.objects.filter(idSubscribee = idUser.pk).exclude(idSubscriber__groupmember__idGroup = idGroup.pk)[item_count:item_count+10]
  1061.         print(subscribers)
  1062.         sending_list = []
  1063.  
  1064.         for subs in subscribers:
  1065.             temp_dict = {}
  1066.             temp_dict['idUser'] = subs.idSubscriber.pk
  1067.             temp_dict['name'] = subs.idSubscriber.first_name+ ' ' + subs.idSubscriber.last_name
  1068.             profile = Profile.objects.get(user = subs.idSubscriber)
  1069.             temp_dict['institution'] = profile.institution
  1070.             temp_dict['image_url'] = absoluteURL+str(profile.profilePhoto.url)
  1071.  
  1072.             sending_list.append(temp_dict)
  1073.  
  1074.  
  1075.         data = json.dumps(sending_list)
  1076.         return HttpResponse(data)
  1077.  
  1078.  
  1079. @csrf_exempt
  1080. def search_db(request):
  1081.     if request.method == 'POST':
  1082.         data_byte=request.body.decode('utf-8')
  1083.         data=json.loads(data_byte)
  1084.  
  1085.         idUser = User.objects.get(pk = data['idUser'])
  1086.         item_count=data['item_count']
  1087.         search_type = data['type']
  1088.         string = data['string']
  1089.         sending_list = []
  1090.  
  1091.         if search_type == 'person':
  1092.             users = User.objects.filter(Q(first_name__icontains = string) | Q(last_name__icontains = string) | Q(profile__institution__icontains = string) )[item_count:item_count+10]
  1093.             for U in users:
  1094.                 temp_dict = {}
  1095.                 # profile = Profile.objects.get(idUser = U)
  1096.                 profile = Profile.objects.get(user = U)
  1097.                 temp_dict['idUser'] = U.pk
  1098.                 # temp_dict['firstName']=U.first_name
  1099.                 # temp_dict['lastName']=U.last_name
  1100.                 temp_dict['name']=U.first_name+" "+U.last_name
  1101.                 temp_dict['photo']=absoluteURL+str(profile.profilePhoto.url)
  1102.                 temp_dict['subscription'] = Subscription.objects.filter(idSubscriber = idUser).count()
  1103.  
  1104.                 sending_list.append(temp_dict)
  1105.  
  1106.                
  1107.         elif search_type == 'group':
  1108.             groups = Group.objects.filter(groupName__icontains = string)[item_count:item_count+10]
  1109.             for G in groups:
  1110.                 temp_dict = {}
  1111.                 temp_dict['idGroup']=G.pk
  1112.                 temp_dict['name'] = G.groupName
  1113.                 temp_dict['type'] = G.groupType
  1114.                 temp_dict['photo'] = absoluteURL+str(G.groupImage.url)
  1115.                 temp_dict['memberNo'] = GroupMember.objects.filter(idGroup = G).count()
  1116.  
  1117.                 sending_list.append(temp_dict)
  1118.  
  1119.  
  1120.         else:
  1121.             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]
  1122.  
  1123.             sending_list = []
  1124.  
  1125.             for S in surveys:
  1126.                 P = Poll.objects.filter(idSurvey = S)[:1]
  1127.                 O = OptionValues.objects.get(idPoll = P)
  1128.                 U = User.objects.get(pk = S.idUser.pk)
  1129.                 profile = Profile.objects.get(user = U.pk)
  1130.                 total_recommends = len(Recommend.objects.filter(idSurvey = S))
  1131.                 total_comments = len(Comment.objects.filter(idSurvey = S))
  1132.                 option_list = [O.name0,O.name1,O.name2,O.name3]
  1133.                 vote_count_list = [O.value0,O.value1,O.value2,O.value3]
  1134.                 total_votes=O.value0+O.value1+O.value2+O.value3
  1135.  
  1136.                 temp_dict = {}
  1137.  
  1138.                 temp_dict['name']=U.first_name+" "+U.last_name
  1139.                 temp_dict['photo']=absoluteURL+str(profile.profilePhoto.url)
  1140.                 temp_dict['idSurvey']=S.pk
  1141.                 temp_dict['title']=S.title
  1142.                 temp_dict['idUser']=U.pk
  1143.                
  1144.                 temp_dict['time']=str( S.creationTime.date()) + ' , '+str(S.creationTime.time())[0:5]
  1145.                 temp_dict['idPoll']=P[0].pk
  1146.                 temp_dict['pollQuestion']=P[0].description
  1147.                 temp_dict['pollImage']='dummy.jpg'
  1148.                 temp_dict['recommend']=total_recommends
  1149.                 temp_dict['comment']=total_comments
  1150.                 temp_dict['option_list']=option_list
  1151.                 temp_dict['votes']=total_votes                        #dummy value for now
  1152.                 temp_dict['vote_count_list']=vote_count_list
  1153.  
  1154.                 sending_list.append(temp_dict)
  1155.  
  1156.         data = json.dumps(sending_list)
  1157.         return HttpResponse(data)
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163. @csrf_exempt
  1164. def save_image(request):
  1165. #   if request.method == 'POST':
  1166.  
  1167.     print('I am in save_image function')
  1168.  
  1169.     Type = request.POST.get('type','')
  1170.  
  1171.     if Type == 'group':
  1172.         idGroup = int(request.POST.get('dest_id'))
  1173.         obj = Group.objects.get(pk = idGroup)
  1174.         obj.groupImage.delete(True)
  1175.         obj.groupImage.save(str(idGroup)+(str(datetime.datetime.now().time())[0:5])+'group.jpg',request.FILES['file'])
  1176.         try:
  1177.             obj.save()
  1178.             return HttpResponse('success')
  1179.         except Exception as e:
  1180.             return HttpResponse('failure')
  1181.     elif Type == 'profile':
  1182.         idUser = int(request.POST.get('dest_id'))
  1183.         obj = Profile.objects.get(user = idUser)
  1184.         obj.profilePhoto.delete(True)
  1185.         obj.profilePhoto.save(str(idUser)+(str(datetime.datetime.now().time())[0:5])+'profile.jpg', request.FILES['file'])
  1186.         try:
  1187.             obj.save()
  1188.             return HttpResponse('success')
  1189.         except Exception as e:
  1190.             return HttpResponse('failure')
  1191.  
  1192.     return HttpResponse('failure')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement