def get(self, request, pk, format=None): user = self.get_object(pk) try: friendrequest = FriendRequest.objects.get(to_user=user, from_user=request.user) status = 0 except: friendrequest = FriendRequest(to_user=user, from_user=request.user, status='P') friendrequest.save() notification = Notification( user=user, notification_type='UFRS', object_id=friendrequest.id, object_type='Friend Request', message=request.user.first_name + ' Sent you a friend request', object_name=user.first_name, created_by=request.user, ) notification.save() status = 1 data = {'status': status} return HttpResponse(json.dumps(data), content_type='application/x-json')
def send_friend_request(request,user): if not user in request.user.friends.all(): try: friendrequest = FriendRequest.objects.get(to_user = user,from_user = request.user,status='P') except: friendrequest = FriendRequest( to_user = user, from_user = request.user, status = 'P' ) friendrequest.save() notification = Notification( user = user, notification_type = 'UFRS', created_by=request.user, object_id = friendrequest.id, object_type = 'Friend Request', object_name = request.user.first_name, message = request.user.first_name + " Sent you a friend request." ) notification.save() user.notifications_count += 1 user.save() return True
def add_notifications(): user = GolfUser.objects.get(id=13) for i in range(10): notification = Notification( user=user, created_by=user, notification_type='GURI', object_id=49, object_type='group', object_name='new test group', message="Kruthika wants to test notification") notification.save()
def get(self, request, pk, format=None): data = {} event = self.get_object(pk) eventRequest = EventRequestInvitation.objects.get(event=event, to=request.user, type='I', accept='M') accept = request.GET.get('accept') if accept == 'true': eventRequest.accept = 'Y' eventaccess = EventsAccess(event=event, user=request.user, access=True) eventaccess.save() event.attending += 1 data[ 'response_message'] = 'You have successfully accepted the invitation.' data['request_status'] = True message = request.user.first_name + " has accepted your invitation to the event '" + event.name + "'" else: eventRequest.accept = 'N' data[ 'response_message'] = 'You have successfully declined the invitation.' data['request_status'] = False message = request.user.first_name + " has declined your invitation to the event '" + event.name + "'" eventRequest.save() event.mayattend = EventRequestInvitation.objects.filter( event=event, type='I', accept='M').count() event.save() if event.created_by.notify_accept_invitation: usernotification = Notification(user=event.created_by, created_by=request.user, notification_type='EUAI', object_id=event.id, object_type='Event', object_name=event.name, message=message) usernotification.save() user = event.created_by user.notifications_count += 1 user.save() send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json, status=status.HTTP_200_OK)
def get(self, request, pk, format=None): user = self.get_object(pk) if user in request.user.friends.all(): response_message = 'You have already friends with this user.' request_status = False else: try: friendrequest = FriendRequest.objects.get(to_user = user,from_user = request.user,status='P') response_message = 'You have already sent friend request.' request_status = False except: friendrequest = FriendRequest( to_user = user, from_user = request.user, status = 'P' ) friendrequest.save() notification = Notification( user = user, notification_type = 'UFRS', created_by=request.user, object_id = friendrequest.id, object_type = 'Friend Request', object_name = request.user.first_name, message = request.user.first_name + " sent you a friend request." ) notification.save() user.notifications_count += 1 user.save() response_message = 'Friend request sent successfully.' request_status = True data = {'response_message':response_message,'request_status':request_status} send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json,status=status.HTTP_200_OK)
def get(self, request, *args, **kwargs): data = {} post = Post.objects.get(id=int(self.kwargs['pk'])) user = request.user if user not in post.liked_users.all(): post.liked_users.add(user) post.likes_count += 1 success = True if post.author.notify_like_post and not request.user == post.author: try: notification = Notification.objects.get( created_by=request.user, notification_type='UPL', object_id=post.id) except: notification = Notification( user=post.author, created_by=request.user, notification_type='UPL', object_id=post.id, object_type='Post', object_name=post.title, message=request.user.first_name + " liked your post " + post.title) notification.save() user = post.author user.notifications_count += 1 user.save() else: post.liked_users.remove(user) post.likes_count -= 1 success = False post.save() data = {'like': success, 'likes_count': post.likes_count} send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json, status=status.HTTP_200_OK)
def post(self, request, pk, format=None): post = self.get_object(pk) commentform = CommentForm(request.data) if commentform.is_valid(): comment = commentform.save(commit=False) comment.author = request.user comment.save() post.comments.add(comment) post.comment_count = post.comments.count() post.save() try: image_ids = request.data['images'] images = PostFiles.objects.filter(id__in=image_ids) for image in images: comment.images.add(image) except: pass if post.author.notify_comment_post and not request.user == post.author: notification = Notification(user=post.author, created_by=request.user, notification_type='UPC', object_id=post.id, object_type='Post', object_name=post.title, message=request.user.first_name + " commented on your post " + post.title) notification.save() user = post.author user.notifications_count += 1 user.save() serializer = CommentSerializer(comment) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, format=None): event = self.get_object(pk) data = {} try: eventRequest = EventRequestInvitation.objects.get( event=event, created_by=request.user, type='R', accept='M') message = 'You have already requested for access.' request_status = False except: eventRequest = EventRequestInvitation(event=event, created_by=request.user, to=event.created_by, type='R') eventRequest.save() ownernotification = Notification( user=event.created_by, created_by=request.user, notification_type='EURA', object_id=eventRequest.id, object_type='Event Invitation', object_name=event.name, message=request.user.first_name + ' Requested access to the event ' + event.name) ownernotification.save() user = event.created_by user.notifications_count += 1 user.save() request_status = True message = 'Successfully sent request to the event owner' data['response_message'] = message data['request_status'] = request_status send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json, status=status.HTTP_200_OK)
def put(self, request, pk, format=None): event = self.get_object(pk) if event.venue_course_id: old_course = event.venue_course_id else: old_course = False form = EventAddForm(request.data, instance=event) if form.is_valid(): obj = form.save(commit=False) user = request.user obj.modified_by = user try: coverImageId = request.data.get('cover_image') if coverImageId: cover_image = EventImages.objects.get(id=int(coverImageId)) obj.cover_image = cover_image obj.save() except: pass try: fileId = request.data.get('teetime_file') if fileId: tee_file = EventFiles.objects.get(id=int(fileId)) if not tee_file in obj.files.all(): obj.files.add(tee_file) obj.save() except: pass try: groupId = request.data.get('group_id') if groupId: group = Groups.objects.get(id=int(groupId)) group.events.add(obj) obj.event_group_id = group.id obj.save() except: pass is_course = request.data.get('is_course') if is_course: course_id = request.data.get('venue_course') course = Courses.objects.get(id=int(course_id)) course.events.add(obj) obj.venue = course.name obj.venue_course_id = course.id obj.save() else: obj.venue_course_id = None if old_course: course = Courses.objects.get(id=old_course) course.events.remove(obj) obj.save() searchindex = IndexObject('event', obj.id) event_access = EventsAccess.objects.filter( event=event, attending='Y').values_list('user', flat=True) users = GolfUser.objects.filter(id__in=event_access).distinct() subject = 'GolfConnectx Event - ' + str(obj.name) message = 'Event details has been updated by ' + str( request.user.first_name) for user in users: if user.notify_event_updates: notification = Notification( user=user, created_by=request.user, notification_type='EUN', object_id=event.id, object_type='Event Update', object_name=event.name, message=request.user.first_name + ' updated the event ' + event.name) notification.save() user.notifications_count += 1 user.save() serializer = EventSerializer(obj, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK) data = {'errors': dict(form.errors.items())} errors = json.dumps(data) errors = json.loads(errors) return Response(errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, pk, format=None): event = self.get_object(pk) #serializer = EventSerializer(data=request.data) is_friend = request.data.get('is_friend') try: user_ids = request.data.get('users') if user_ids: for userid in user_ids: user = GolfUser.objects.get(id=int(userid)) if is_friend: req_stat = send_friend_request(request, user) try: eventRequest = EventRequestInvitation.objects.get( event=event, created_by=event.created_by, to=user, type='I', accept='M') except: eventRequest = EventRequestInvitation( event=event, created_by=event.created_by, to=user, type='I') eventRequest.save() if user.notify_invite_event: usernotification = Notification( user=user, created_by=request.user, notification_type='EURI', object_id=eventRequest.id, object_type='Event Invitation', object_name=event.name, message=event.created_by.first_name + " Sent Invite to attend the event '" + event.name + "'") usernotification.save() user.notifications_count += 1 user.save() useremails = request.data.get('useremails') try: message = request.data['message'] except: message = '' mail_message = '' if useremails: for email in useremails: try: user = GolfUser.objects.get(email=email) if is_friend: req_stat = send_friend_request(request, user) try: eventRequest = EventRequestInvitation.objects.get( event=event, created_by=event.created_by, to=user, type='I', accept='M') except: eventRequest = EventRequestInvitation( event=event, created_by=event.created_by, to=user, type='I') eventRequest.save() if user.notify_invite_event: usernotification = Notification( user=user, created_by=request.user, notification_type='EURI', object_id=eventRequest.id, object_type='Event Invitation', object_name=event.name, message=event.created_by.first_name + " Sent Invite to attend the event '" + event.name + "'") usernotification.save() user.notifications_count += 1 user.save() mail_message = mail_message + str( email) + " -user is already part of the system.\n" except: if is_friend: req_stat = send_friend_request_via_mail( request, email) try: invite = Invite.objects.get(email=email, object_id=event.id, object_type='Event') mail_message = mail_message + str( email) + " -invite has already been sent.\n" except: invite = Invite( email=email, object_id=event.id, object_name=event.name, object_type='Event', ) token = ''.join([choice(rand) for i in range(6)]) invite.token = token invite.invited_by = request.user invite.save() try: to_email = [email] name = email.split('@')[0] sdata = { 'invite': invite, 'message': message, 'name': name } email_message = get_template( 'common/invite_mail.html').render( Context(sdata)) subject = 'You have been invited to GolfConnectX' email = EmailMessage(subject, email_message, mysettings.DEFAULT_FROM_EMAIL, to_email) email.content_subtype = "html" email.send() mail_message = mail_message + str( email) + " -invite sent successfully.\n" except: mail_message = mail_message + str( email) + " -failed to send invite.\n" event.mayattend = EventRequestInvitation.objects.filter( event=event, type='I', accept='M').count() event.save() return Response('Invitation send successfully', status=status.HTTP_200_OK) except: return Response("Error while sending invitation", status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, id, format=None): event = self.get_object(pk) data = {} try: eventRequest = EventRequestInvitation.objects.get( event=event, created_by__id=id, type='R', accept='M') if eventRequest.accept == 'N': message = 'User rejected the request to this event.' request_status = False else: try: eventaccess = EventsAccess.objects.get( event=event, user=eventRequest.created_by) message = 'User already has access to this event' request_status = False except: access = request.GET.get('accept') if access == 'true': eventaccess = EventsAccess( event=event, user=eventRequest.created_by, access=True) eventaccess.save() message = event.created_by.first_name + " granted access to the event '" + event.name + "'" eventRequest.accept = 'Y' event.attending += 1 else: message = event.created_by.first_name + " declined access to the event '" + event.name + "'" eventRequest.accept = 'N' eventRequest.save() event.mayattend = EventRequestInvitation.objects.filter( event=event, type='I', accept='M').count() event.save() usernotification = Notification( user=eventRequest.created_by, created_by=request.user, notification_type='EUGA', object_id=eventRequest.id, object_type='Event Invitation', object_name=event.name, message=message) usernotification.save() user = eventRequest.created_by user.notifications_count += 1 user.save() request_status = True except: message = 'This request has already been processed.' request_status = False data['response_message'] = message data['request_status'] = request_status send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json, status=status.HTTP_200_OK)
def post(self,request,*args,**kwargs): user_list = request.data.get('users') users = GolfUser.objects.filter(id__in=user_list) loggeduser = request.user try: for user in users: try: friendrequest = FriendRequest.objects.get(to_user = user,from_user = request.user,status='P') except: friendrequest = FriendRequest( to_user = user, from_user = request.user, status = 'P' ) friendrequest.save() if user.notify_invite_event: notification = Notification( user = user, notification_type = 'UFRS', created_by=request.user, object_id = friendrequest.id, object_type = 'Friend Request', object_name = request.user.first_name, message = request.user.first_name + ' Sent you a friend request' ) notification.save() user.notifications_count += 1 user.save() try: message = request.data['message'] except: message = '' mail_message = '' useremails = request.data['useremails'] for email in useremails: try: user = GolfUser.objects.get(email=email) try: friendrequest = FriendRequest.objects.get(to_user = user,from_user = request.user,status='P') except: friendrequest = FriendRequest( to_user = user, from_user = request.user, status = 'P' ) friendrequest.save() if user.notify_invite_event: notification = Notification( user = user, notification_type = 'UFRS', created_by=request.user, object_id = friendrequest.id, object_type = 'Friend Request', object_name = request.user.first_name, message = request.user.first_name + ' Sent you a friend request' ) notification.save() user.notifications_count += 1 user.save() mail_message = mail_message + str(email) +" -user is already part of the system.\n" except: try: invite = Invite.objects.get(email=email,object_id=request.user.id,object_type='Friend') mail_message = mail_message + str(email) +" -invite has already been sent.\n" except: invite = Invite( email = email, object_id = request.user.id, object_name = request.user.first_name, object_type = 'Friend', ) token = ''.join([choice(rand) for i in range(6)]) invite.token = token invite.invited_by=request.user invite.save() try: to_email=[email] name = email.split('@')[0] sdata = {'invite': invite,'message':message,'name':name} email_message = get_template('common/invite_mail.html').render(Context(sdata)) subject = 'You have been invited to Golf Connectx' email= EmailMessage(subject,email_message,my_settings.DEFAULT_FROM_EMAIL,to_email) email.content_subtype = "html" email.send() mail_message = mail_message + str(email) +" -invite sent successfully.\n" except: mail_message = mail_message + str(email) +" -failed to send invite.\n" response_message = mail_message request_status = True except: response_message = 'Request failed with error.' request_status = False data = {'response_message':response_message,'request_status':request_status} send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json,status=status.HTTP_200_OK)
def get(self, request, pk, format=None): data = {} user = self.get_object(pk) try: friendrequest = FriendRequest.objects.get(from_user = user,to_user = request.user,status='P') accept=request.GET.get('accept') if accept=='true': friendrequest.status = 'A' loged_user = request.user loged_user.friends.add(user) if loged_user.friends_ids: loged_user.friends_ids = loged_user.friends_ids + ","+str(user.id) else: loged_user.friends_ids = str(user.id) loged_user.save() user.friends.add(loged_user) if user.friends_ids: user.friends_ids = user.friends_ids + ","+str(loged_user.id) else: user.friends_ids = str(loged_user.id) user.save() message = 'Friend Request Accepted.' else: friendrequest.status = 'R' message = 'Friend Request Rejected.' friendrequest.save() notification = Notification( user = user, notification_type = 'UFRR', created_by=request.user, object_id = friendrequest.id, object_name = request.user.first_name, object_type = 'Friend Request', ) user.notifications_count += 1 user.save() if accept=='true': message = request.user.first_name + " accepted your friend request." else: message = request.user.first_name + " declined your friend request." notification.message = message notification.save() request_status = True data['friend_request_status'] = friendrequest.status except: from sys import exc_info print "+++++++++++++++",exc_info(),"+++++++++++++++++" request_status = False message = 'You have already responded to the request' data['response_message'] = message data['request_status'] = request_status send_data = json.dumps(data) send_json = json.loads(send_data) return Response(send_json,status=status.HTTP_200_OK)
def post(self, request, *args, **kwargs): serializer = UserCreateSerializer(data=request.data) if serializer.is_valid(raise_exception=True): first_name = request.data['first_name'] last_name = request.data['last_name'] email = request.data['email'] password = request.data['password'] is_private = request.data.get('is_private') zipcode = request.data.get('zipcode') user = GolfUser(first_name=first_name, last_name=last_name, email=email, zipcode=zipcode) try: if is_private: user.is_private = is_private except: pass user.set_password(password) user.save() try: code = request.data.get('invite_code') invite = Invite.objects.get(token=code, email=user.email) object_id = invite.object_id object_type = invite.object_type invites = Invite.objects.filter(email=user.email) for invite in invites: if invite.object_type == 'Group': group = Groups.objects.get(id=invite.object_id) groupRequest = GroupsRequestInvitation( group=group, created_by=invite.invited_by, to=user, type='I') groupRequest.save() usernotification = Notification( user=user, created_by=invite.invited_by, notification_type='GURI', object_id=group.id, object_type='Group Invitation', object_name=group.name, message=invite.invited_by.first_name + ' Sent Invite to be a member of the group ' + group.name) usernotification.save() elif invite.object_type == 'Event': event = Events.objects.get(id=invite.object_id) eventRequest = EventRequestInvitation( event=event, created_by=invite.invited_by, to=user, type='I') eventRequest.save() usernotification = Notification( user=user, created_by=invite.invited_by, notification_type='EURI', object_id=event.id, object_type='Event Invitation', object_name=event.name, message=invite.invited_by.first_name + ' Sent Invite to attend the event ' + event.name) usernotification.save() elif invite.object_type == 'Friend': friendrequest = FriendRequest( from_user=invite.invited_by, to_user=user, status='P') friendrequest.save() usernotification = Notification( user=user, notification_type='UFRS', object_id=friendrequest.id, object_name=invite.invited_by.first_name, object_type='Friend Request', message=invite.invited_by.first_name + ' Sent you a friend request') usernotification.save() user.notifications_count += 1 user.save() except: object_id = None object_type = None user = authenticate(username=email, password=password) initials = user.initials() color = ALPHA_COLORS[initials[0]] if initials[0] == "W": fontsize = 45 else: fontsize = 55 img = Image.open( str(my_settings.STATICFILES_DIRS[0]) + "themes/img/" + color + ".png") draw = ImageDraw.Draw(img) font = ImageFont.truetype( str(my_settings.STATICFILES_DIRS[0]) + "themes/fonts/RODUSsquare300.otf", fontsize) IH, IW = 136, 134 W, H = font.getsize(initials) xycords = (((IW - W) / 2), ((IH - H) / 2)) draw.text(xycords, initials, COLORS_RGB[color], font=font, align="center") tempfile = img tempfile_io = StringIO.StringIO() tempfile.save(tempfile_io, format='PNG') image_file = InMemoryUploadedFile(tempfile_io, None, 'pimage.png', 'image/png', tempfile_io.len, None) image_obj = UserImage() image_obj.name = "profile_image_" + user.first_name image_obj.image = image_file image_obj.save() user.profile_image = image_obj user.save() login(request, user) token, created = Token.objects.get_or_create(user=user) data = { 'id': user.id, 'name': user.first_name, 'email': user.email, 'token': token.key, 'profile_image_url': user.get_api_profile_image_url(), 'object_id': object_id, 'object_type': object_type } searchindex = IndexObject('profile', user.id) return Response(data) return Response(serializer.errors, status=HTTP_500_INTERNAL_SERVER_ERROR)