def post(self, request, poll_id): try: selects = request.data.get('vote') name = request.data.get('name') user = request.user poll = Poll.objects.get(id=poll_id) except: return HttpResponse404Error("This poll doesn\'t exist.") if check_poll_close(poll): return HttpResponse999Error("این نظرسنجی بسته شده است.") poll_selects = poll.selects.all() for index, val in enumerate(selects): try: selectUser = SelectUser.objects.filter( select=poll_selects[index], user=user, name=name) if selectUser: selectUser = selectUser[0] selectUser.agreement = val + 1 selectUser.save() else: selectUser = SelectUser(select=poll_selects[index], user=user, agreement=val + 1, name=name) selectUser.save() except Exception as e: return HttpResponse404Error("One of the options not found.") send_email_new_vote(user, poll, user.email) return HttpResponse("Submit successfully")
def get(self, request, room, select_id): try: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.owner != request.user: return HttpResponse401Error({ 'You don\'t have permission to set room for this meeting.' }) if meeting.room: return HttpResponse405Error({"This meeting already set room"}) while True: try: res = requests.post( url=settings.API_ADDRESS + 'rooms/' + str(room) + '/reserve', json={ "username": '******', "start": str(meeting.date) + 'T' + str(meeting.startTime), "end": str(meeting.date) + 'T' + str(meeting.endTime), }) select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.isCancel: meeting_json = serializers.serialize('json', [meeting]) return HttpResponse(meeting_json, content_type='application/json') if res.status_code == 200: meeting.room = room meeting.status = 2 SetReservationTimes.endTime(meeting.id) meeting.save() to = [] meetingParticipants = MeetingParticipant.objects.filter( meeting_id=meeting.id) for mp in meetingParticipants: to.append(mp.participant.email) self.sendMail(request.user, [meeting.owner.email] + to, meeting, select.poll.id, select_id, room) meeting_json = serializers.serialize('json', [meeting]) return HttpResponse(meeting_json, content_type='application/json') elif res.status_code == 400: return HttpResponse404Error({"Room Not found"}) except: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.isCancel: meeting_json = serializers.serialize('json', [meeting]) return HttpResponse(meeting_json, content_type='application/json') except: return HttpResponse404Error({"poll not found"})
def post(self, request, comment_id): try: comment = Comment.objects.get(id=comment_id) except: return HttpResponse404Error("No comment doesn\'t exist.") if request.user != comment.owner: return HttpResponse404Error( "You don\'t have permission to comment on this poll") text = request.data.get('text') comment.text = text comment.save() comments_json = CommentSerializer.makeSerial([comment]) return HttpResponse(comments_json, content_type='application/json')
def post(self, request): title = request.data.get('title') text = request.data.get('text') link = request.data.get('link', 'No link') user = request.user close_date = request.data.get('closeDate', '2424-2-2') if not close_date: close_date = '2424-2-2' participants = request.data.get('participants', []) selects = request.data.get('selects') meeting = Meeting(title=title, text=text, owner=user) meeting.save() poll = Poll(title=title, text=text, meeting=meeting) if close_date: poll.date_close = datetime.datetime.strptime( close_date, '%Y-%m-%d') poll.save() meetingParticipant = MeetingParticipant(meeting=meeting, participant=user) meetingParticipant.save() link += str(poll.id) self.create_participants(meeting, participants, user) self.createOptions(poll, selects) check_poll_close(poll) poll_json = serializers.serialize('json', [poll]) send_email_create_poll(user, title, link, participants) try: return HttpResponse(poll_json, content_type='application/json') except Exception as e: print(e) return HttpResponse404Error("This poll doesn\'t exist.")
def get(self, request, poll_id): try: selects = Select.objects.filter(poll_id=poll_id) selects_json = SelectSerializer.makeSerial(selects) return HttpResponse(selects_json, content_type='application/json') except: return HttpResponse404Error("This poll doesn\'t exist.")
def get(self, request, comment_id): try: comment = Comment.objects.get(id=comment_id) except: return HttpResponse404Error("This comment not found.") comment.delete() return HttpResponse('Comment deleted successfully')
def get(self, request): try: poll = Poll.objects.last() poll = serializers.serialize('json', [poll]) return HttpResponse(poll, content_type='application/json') except: return HttpResponse404Error("No poll doesn\'t exist.")
def get(self, request, poll_id): try: names = {} poll = Poll.objects.get(id=poll_id) poll_selects = poll.selects.all() for index, select in enumerate(poll_selects): selectUsers = SelectUser.objects.filter(select=select) for select_user in selectUsers: if select_user.name in names.keys(): names = self.fill_vote_of_participants( select_user, names, index) else: names[select_user.name] = [ -1 for i in range(len(poll_selects)) ] names = self.fill_vote_of_participants( select_user, names, index) res = [] for name in names.keys(): ss = {} ss['name'] = name ss['votes'] = names[name] res.append(ss) return HttpResponse(json.dumps(res), content_type='application/json') except: return HttpResponse404Error("This poll doesn\'t exist.")
def get(self, request, select_id): try: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.owner != request.user: return HttpResponse401Error({ 'You don\'t have permission to see room for this meeting.' }) if request.user.username != meeting.owner.username: return HttpResponse999Error( {'You don\'t have access to this point.'}) try: req = requests.get(settings.API_ADDRESS + 'available_rooms' + '?start=' + str(select.date) + 'T' + str(select.startTime) + '&end=' + str(select.date) + 'T' + str(select.endTime)) if req.status_code == 200: return JsonResponse(req.json()) else: return HttpResponse500Error( {'Reservation system crash, please try again.'}) except: return HttpResponse500Error( {'Reservation system crash, please try again.'}) except: return HttpResponse404Error({"This select id is not found."})
def post(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) meeting = poll.meeting text = request.data.get('text') owner = request.user if not MeetingParticipant.objects.filter(participant=owner, meeting=meeting): return HttpResponse404Error( "You don\'t have permission to comment on this poll") comment = Comment(owner=owner, poll=poll, text=text) comment.save() comment_json = serializers.serialize('json', [comment]) return HttpResponse(comment_json, content_type='application/json') except: return HttpResponse404Error("No poll doesn\'t exist.")
def get(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) except: return HttpResponse404Error({'this poll doesn\'t exist.'}) if check_poll_close(poll): return HttpResponse999Error("این نظرسنجی بسته شده است.") return HttpResponse('{"title": "' + poll.title + '" }', content_type='application/json')
def get(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) if check_poll_close(poll): return HttpResponse999Error("این نظرسنجی بسته شده است.") selects = Select.objects.filter(poll_id=poll_id) selects_json = SelectSerializer.makeSerial(selects) return HttpResponse(selects_json, content_type='application/json') except: return HttpResponse404Error("This poll doesn\'t exist.")
def get(self, request, poll_id): selectUsers = SelectUser.objects.filter(select__poll_id=poll_id) name = '' for su in selectUsers: if su.user == request.user: name = su.name break if name: return HttpResponse('{"name": "' + name + '"}', content_type='application/json') return HttpResponse404Error("این کاربر تا به حال رای نداده است.")
def get(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) except: return HttpResponse404Error("No poll doesn\'t exist.") poll.status = True poll.save() meetPars = MeetingParticipant.objects.filter(meeting=poll.meeting) send_email_close_poll(request.user, poll, meetPars) poll_json = serializers.serialize('json', [poll]) return HttpResponse("نظرسنجی با موفقیت بسته شد.")
def get(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) if poll.meeting.owner.username != request.user.username: return HttpResponse999Error( {'You don\'t have access to this point.'}) poll_json = ShowPollSerializer.makeSerial(poll) return HttpResponse(poll_json, content_type='application/json') except: return HttpResponse404Error({'this poll doesn\'t exist.'})
def get(self, request, select_id): try: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if request.user.username != meeting.owner.username: return HttpResponse999Error( {'You don\'t have access to this point.'}) meeting_json = serializers.serialize('json', [meeting]) return HttpResponse(meeting_json, content_type='application/json') except: return HttpResponse404Error("This select_id doesn\'t exist.")
def get(self, request, username, password): res = requests.post(SITE_URL + 'api/auth/get_token/', json={ "username": username, "password": password } ) if res.status_code != 200: return HttpResponse404Error( "User not found" ) return HttpResponse(res, content_type='application/json')
def get(self, request): try: # polls = Poll.objects.filter(meeting__owner=request.user) meetingParticipants = MeetingParticipant.objects.filter( participant=request.user) polls = [] for mp in meetingParticipants: polls.append(mp.meeting.polls.all()[0]) for poll in polls: check_poll_close(poll) polls_json = ShowPollsSerializer.makeSerial(polls, request.user) return HttpResponse(polls_json, content_type='application/json') except: return HttpResponse404Error("This poll doesn\'t exist.")
def post(self, request, comment_id): try: comment = Comment.objects.get(id=comment_id) except: return HttpResponse404Error( "You don\'t have permission to comment on this poll") poll = comment.get_poll meeting = poll.meeting text = request.data.get('text') owner = request.user if not MeetingParticipant.objects.filter(participant=owner, meeting=meeting): return HttpResponse404Error( "You don\'t have permission to comment on this poll") reply = Comment(owner=owner, poll=None, text=text, parent=comment, level=comment.level + 1) reply.save() reply_json = serializers.serialize('json', [reply]) return HttpResponse(reply_json, content_type='application/json')
def get(self, request, select_id): try: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.owner != request.user: return HttpResponse401Error({ 'You don\'t have permission to set date for this meeting.' }) meeting.startTime = select.startTime meeting.endTime = select.endTime meeting.date = select.date meeting.save() SetReservationTimes.startTime(meeting.id) return HttpResponse("Set date and time successfully") except: return HttpResponse404Error({"select or poll not found"})
def get(self, request, poll_id): try: poll = Poll.objects.get(id=poll_id) meeting = poll.meeting meetingParticipants = MeetingParticipant.objects.filter( meeting=meeting) participants = [] for mp in meetingParticipants: if mp.participant.email != request.user.email: participants.append(mp.participant.email) participants = {'participants': participants} participants_json = json.dumps(participants) return HttpResponse(participants_json, content_type='application/json') except: return HttpResponse404Error({'this poll doesn\'t exist.'})
def update_poll(self, user, poll_id, text, title, close_date): try: poll = Poll.objects.get(id=poll_id, meeting__owner=user) except: return HttpResponse404Error( "No poll doesn\'t exist or you can\'t access to modified this poll." ) poll.text = text poll.title = title if close_date: poll.date_close = datetime.datetime.strptime( close_date, '%Y-%m-%d') poll.status = False poll.save() meeting = poll.meeting meeting.text = text meeting.title = title meeting.save() return poll
def get(self, request, select_id): try: select = Select.objects.get(id=select_id) meeting = select.poll.meeting if meeting.owner != request.user: return HttpResponse401Error( {'You don\'t have permission to cancel this meeting.'}) meeting.isCancel = True meeting.status = 4 meeting.room = None poll = meeting.polls.all()[0] poll.status = True poll.save() meeting.save() meetPars = MeetingParticipant.objects.filter(meeting=meeting) send_email_cancel_meeting(request.user, meeting, meetPars) SetReservationTimes.delete(meeting) meeting_json = serializers.serialize('json', [meeting]) return HttpResponse(meeting_json, content_type='application/json') except: return HttpResponse404Error({'this select doesn\'t exist.'})