Beispiel #1
0
 def put(self, request, access_token, *args, **kwargs):
     try:
         user = UserProfileView.get_profile(access_token).user
         serializer = UserSerializer(user, data=request.data, partial=True)
         if serializer.is_valid():
             serializer.save()
             return response.Response(serializer.data)
     except Exception as e:
         UserView.handle_error(e)
Beispiel #2
0
 def get(self, request, access_token, *args, **kwargs):
     try:
         user = BdayAppUser.objects.select_related(
             'user_id').select_related('user_wallet').get(
                 id=UserProfileView.get_profile(access_token).user.id)
         serializer = UserSerializer(user)
         return response.Response(serializer.data)
     except Exception as e:
         UserView.handle_error(e)
 def get_queryset(self):
     access_token = self.kwargs['access_token']
     try:
         user = UserProfileView.get_profile(access_token).user
         reminder_list = Reminder.objects.filter(user=user)
         return reminder_list
     except Reminder.DoesNotExist:
         raise exceptions.NotFound("reminder list not found")
     except Exception as e:
         ReminderListView.handle_error(e)
Beispiel #4
0
 def put(self, request, *args, **kwargs):
     try:
         logger.info("Starting PUT method")
         access_token = kwargs['access_token']
         id = kwargs['id']
         logger.info("access token is " + access_token +
                     " event id for adding member is " + id)
         user = UserProfileView.get_profile(access_token).user
         event = Event.objects.get(pk=id)
         logger.info("event found with id " + id)
         if user in event.admin.all():
             user_ = BdayAppUser.objects.get(pk=request.data['user_id'])
             friends = UserProfileView.get_profile(
                 access_token).app_friends.all()
             friends_ = UserProfile.objects.get(
                 user=user_).app_friends.all()
             if user in friends_ and user_ in friends:
                 logger.info("Both the users are friend of each other")
                 event.admin.add(user_)
                 event.save()
                 chat_buffer = UnreadChatBuffer.objects.create(
                     event=event, user=user, last_read_chat=None)
                 chat_buffer.save()
                 notification_message = "Your friend " + UserProfile.objects.get(
                     user=user
                 ).first_name + " made you admin of the event " + event.name
                 notification = Notification.objects.create_notification(
                     notification_message,
                     user_,
                     None,
                     type="ADMIN_ADDITION")
                 notification.save()
                 logger.info("Completing PUT method")
                 return response.Response(status=status.HTTP_202_ACCEPTED)
             else:
                 raise exceptions.PermissionDenied()
         else:
             raise exceptions.PermissionDenied()
     except Event.DoesNotExist:
         raise exceptions.NotFound("event does not exist")
     except Exception as e:
         EventAdminView.handle_error(e)
Beispiel #5
0
 def get_queryset(self):
     try:
         logger.info("Starting function get_queryset")
         access_token = self.kwargs['access_token']
         friend_list = UserProfileView.get_profile(access_token).app_friends.all()
         logger.info("Completing function get_queryset")
         return friend_list
     except BdayAppUser.DoesNotExist:
         raise exceptions.NotFound("friend list not found")
     except Exception as e:
         FriendListView.handle_error(e)
 def get_queryset(self):
     access_token = self.kwargs['access_token']
     try:
         user = UserProfileView.get_profile(access_token).user
         notification_list = Notification.objects.filter(
             associated_user=user)
         return notification_list
     except Notification.DoesNotExist:
         raise exceptions.NotFound("notification list not found")
     except Exception as e:
         NotificationListView.handle_error(e)
Beispiel #7
0
    def get_queryset(self):
        access_token = self.kwargs['access_token']
        try:

            user = UserProfileView.get_profile(access_token).user
            message_list = Message.objects.filter(
                Q(from_field=user) | Q(to_field=user))
            return message_list
        except Message.DoesNotExist:
            raise exceptions.NotFound("message list not found")
        except Exception as e:
            MessageListView.handle_error(e)
 def get_notification(access_token, id):
     try:
         user = UserProfileView.get_profile(access_token).user
         logger.info("notification id is "+id)
         notification = Notification.objects.get(pk=id)
         if user == notification.associated_user:
             return notification
         else:
             raise exceptions.PermissionDenied()
     except Notification.DoesNotExist:
         raise exceptions.NotFound("notification does not exist")
     except Exception as e:
         NotificationView.handle_error(e)
Beispiel #9
0
 def post(self, request, *args, **kwargs):
     access_token = kwargs['access_token']
     try:
         message = request.data['message']
         to_field = BdayAppUser.objects.get(id=request.data['to_field'])
         from_field = UserProfileView.get_profile(access_token).user
         friends = UserProfileView.get_profile(
             access_token).app_friends.all()
         friends_ = UserProfile.objects.get(user=to_field).app_friends.all()
         if to_field in friends:
             logger.info("Dere")
         if from_field in friends_:
             logger.info("not der")
         if to_field in friends and from_field in friends_:
             message = Message.objects.create_message(
                 request.data['message'], to_field, from_field)
             serializer = MessageSerializer(message)
             return response.Response(serializer.data)
         else:
             raise exceptions.PermissionDenied()
     except Exception as e:
         MessageView.handle_error(e)
Beispiel #10
0
 def get_reminder(access_token, id):
     try:
         user = UserProfileView.get_profile(access_token).user
         logger.info(id)
         reminder = Reminder.objects.get(pk=id)
         logger.info(reminder.name)
         if user == reminder.user:
             return reminder
         else:
             raise exceptions.PermissionDenied()
     except Reminder.DoesNotExist:
         raise exceptions.NotFound("Reminder does not exist")
     except Exception as e:
         ReminderView.handle_error(e)
 def get_queryset(self):
     access_token = self.kwargs['access_token']
     id = self.kwargs['id']
     try:
         user = UserProfileView.get_profile(access_token).user
         chat_list = EventChat.objects.filter(event=Event.objects.get(
             pk=id))
         return chat_list
     except Event.DoesNotExist:
         raise exceptions.NotFound("event not found")
     except EventChat.DoesNotExist:
         raise exceptions.NotFound("chat list not found")
     except Exception as e:
         ChatListView.handle_error(e)
Beispiel #12
0
 def get_message(access_token, id):
     try:
         user = UserProfileView.get_profile(access_token).user
         logger.info(id)
         message = Message.objects.get(pk=id)
         logger.info(message.message)
         if user == message.to_field or user == message.from_field:
             return message
         else:
             return response.Response(status=status.HTTP_401_UNAUTHORIZED)
     except Message.DoesNotExist:
         raise exceptions.NotFound("message does not exist")
     except Exception as e:
         MessageView.handle_error(e)
Beispiel #13
0
 def get_wish(access_token, id):
     try:
         user = UserProfileView.get_profile(access_token).user
         logger.info(id)
         wish = Wish.objects.get(pk=id)
         logger.info(wish.url)
         if user == wish.user:
             return wish
         else:
             raise exceptions.PermissionDenied()
     except Wish.DoesNotExist:
         raise exceptions.NotFound("Wish does not exist")
     except Exception as e:
         WishView.handle_error(e)
 def get(self, request, *args, **kwargs):
     try:
         access_token = kwargs['access_token']
         event_id = kwargs['event_id']
         user = UserProfileView.get_profile(access_token=access_token).user
         data = UnreadChatBuffer.objects.get(
             event=Event.objects.get(pk=event_id), user=user)
         serializer = UnreadChatBufferSerializer(data)
         return response.Response(serializer.data)
     except Event.DoesNotExist:
         raise exceptions.NotFound("event not found")
     except BdayAppUser.DoesNotExist:
         raise exceptions.NotFound("user not found")
     except Exception as e:
         UnreadChatBufferView.handle_error(e)
Beispiel #15
0
 def delete(self, request, *args, **kwargs):
     access_token = kwargs['access_token']
     id = kwargs['id']
     try:
         user = UserProfileView.get_profile(access_token).user
         reminder = Reminder.objects.get(pk=id)
         if user == reminder.user:
             reminder.delete()
             return response.Response(status=status.HTTP_202_ACCEPTED)
         else:
             raise exceptions.PermissionDenied()
     except Reminder.DoesNotExist:
         raise exceptions.NotFound("Reminder does not exist")
     except Exception as e:
         ReminderView.handle_error(e)
Beispiel #16
0
 def put(self, request, *args, **kwargs):
     access_token = kwargs['access_token']
     id = kwargs['id']
     try:
         user = UserProfileView.get_profile(access_token).user
         event = Event.objects.get(pk=id)
         if user in event.admin.all():
             user_ = BdayAppUser.objects.get(pk=request.data['user_id'])
             friends = UserProfileView.get_profile(
                 access_token).app_friends.all()
             friends_ = UserProfile.objects.get(
                 user=user_).app_friends.all()
             if user in friends_ and user_ in friends:
                 event.admin.add(user_)
                 event.save()
                 return response.Response(status=status.HTTP_202_ACCEPTED)
             else:
                 raise exceptions.PermissionDenied()
         else:
             raise exceptions.PermissionDenied()
     except Event.DoesNotExist:
         raise exceptions.NotFound("event does not exist")
     except Exception as e:
         EventAdminView.handle_error(e)
Beispiel #17
0
 def post(self, request, access_token, id, *args, **kwargs):
     try:
         user = UserProfileView.get_profile(access_token).user
         if request.FILES:
             file = request.FILES['file_field']
         else:
             file = None
         event = Event.objects.create_event(admin=user,name=request.data['name'],start_date=request.data['start_date'],end_date=request.data['end_date'],picture=file)
         minimum_balance = moneyed.Money(0, 'INR')
         maximum_balance = moneyed.Money(100000, 'INR')
         wallet = Wallet.objects.create_wallet(minimum_balance, maximum_balance, 'E', 'INR', None, event)
         serializer = EventSerializer(event)
         return response.Response(serializer.data)
     except Exception as e:
         EventView.handle_error(e)
Beispiel #18
0
 def get_members_list(access_token, id):
     try:
         user = UserProfileView.get_profile(access_token).user
         if user in Event.objects.get(
                 pk=id).admin.all() or user in Event.objects.get(
                     pk=id).members.all():
             members_list = Event.objects.get(pk=id).members.all()
             logger.info(members_list)
             return members_list
         else:
             raise exceptions.PermissionDenied()
     except BdayAppUser.DoesNotExist:
         raise exceptions.NotFound("admin list does not exist")
     except Event.DoesNotExist:
         raise exceptions.NotFound("event list does not exist")
     except Exception as e:
         EventMembersView.handle_error(e)
Beispiel #19
0
 def post(self, request, *args, **kwargs):
     try:
         logger.info("Starting POST method")
         access_token = kwargs['access_token']
         logger.info("access token is " + access_token)
         user = UserProfileView.get_profile(access_token).user
         logger.info("user found from access token")
         file = None
         if request.FILES:
             file = request.FILES['file_field']
             logger.info("file found in request")
         else:
             file = None
         end_date = None
         type = 'DEFAULT'
         if 'end_date' in request.data:
             end_date = request.data['end_date']
             logger.info("end date is " + end_date)
         if 'type' in request.data:
             type = request.data['type']
             logger.info("type is " + type)
         event = Event.objects.create_event(
             admin=user,
             name=request.data['name'],
             start_date=request.data['start_date'],
             end_date=end_date,
             picture=file,
             type=type)
         logger.info("event created with name " + request.data['name'] +
                     " start date " + request.data['start_date'] +
                     " type " + type)
         minimum_balance = moneyed.Money(0, 'INR')
         maximum_balance = moneyed.Money(100000, 'INR')
         wallet = Wallet.objects.create_wallet(minimum_balance,
                                               maximum_balance, 'E', 'INR',
                                               None, event)
         wallet.save()
         logger.info("wallet created")
         # chat_buffer = UnreadChatBuffer.objects.create(event=event, user=user, last_read_chat=None)
         # chat_buffer.save()
         serializer = EventSerializer(event)
         logger.info("Completing POST method")
         return response.Response(serializer.data)
     except Exception as e:
         EventView.handle_error(e)
Beispiel #20
0
 def post(self, request, *args, **kwargs):
     access_token = kwargs['access_token']
     try:
         url = request.data['url']
         user = UserProfileView.get_profile(access_token).user
         url_parser = URLParser(url)
         dict_ = url_parser.parse()
         if dict_['status_code'] is not 200:
             raise exceptions.APIException(e)
         wish = Wish.objects.create_wish(url, dict_['name'],
                                         dict_['website_name'],
                                         dict_['website_url'],
                                         dict_['price'], dict_['picture'],
                                         user)
         serializer = WishSerializer(wish)
         return response.Response(serializer.data)
     except Exception as e:
         WishView.handle_error(e)
Beispiel #21
0
 def post(self, request, *args, **kwargs):
     access_token = kwargs['access_token']
     try:
         if request.FILES:
             file = request.FILES['file_field']
         else:
             file = None
         user = UserProfileView.get_profile(access_token).user
         reminder = Reminder.objects.create_reminder(request.data['name'],
                                                     request.data['date'],
                                                     user,
                                                     picture=file,
                                                     type='CUSTOM')
         reminder.save()
         serializer = ReminderSerializer(reminder)
         return response.Response(serializer.data)
     except Exception as e:
         ReminderView.handle_error(e)
 def get_queryset(self):
     access_token = self.kwargs['access_token']
     filter_by = self.request.query_params.get('user', None)
     events = Event.objects.none()
     try:
         user = UserProfileView.get_profile(access_token).user
         if filter_by == "admin":
             events = Event.objects.filter(admin=user)
         elif filter_by == "member":
             logger.info("members")
             events = Event.objects.filter(members=user)
         else:
             events = Event.objects.filter(Q(admin=user)
                                           | Q(members=user)).distinct()
         return events
     except Event.DoesNotExist:
         raise exceptions.NotFound("No event found")
     except Exception as e:
         EventListView.handle_error(e)
 def get_members_list(access_token, id):
     try:
         logger.info("Starting function get_members_list")
         user = UserProfileView.get_profile(access_token).user
         logger.info("user obtained from access token")
         if user in Event.objects.get(
                 pk=id).admin.all() or user in Event.objects.get(
                     pk=id).members.all():
             members_list = Event.objects.get(pk=id).members.all()
             logger.info("member list found for event " + id)
             logger.info("Completing function get_members_list")
             return members_list
         else:
             raise exceptions.PermissionDenied()
     except BdayAppUser.DoesNotExist:
         raise exceptions.NotFound("user does not exist")
     except Event.DoesNotExist:
         raise exceptions.NotFound("event does not exist")
     except Exception as e:
         EventMembersView.handle_error(e)
Beispiel #24
0
    def get_admin_list(access_token, id):
        try:
            logger.info("Starting function get_admin_list")
            user = UserProfileView.get_profile(access_token).user
            if user in Event.objects.get(
                    pk=id).admin.all() or user in Event.objects.get(
                        pk=id).members.all():
                admin_list = Event.objects.get(pk=id).admin.all()
                logger.info("admin list retrieved")
                logger.info("Completing function get_admin_list")
                return admin_list
            else:
                raise exceptions.PermissionDenied()

        except BdayAppUser.DoesNotExist:
            raise exceptions.NotFound("admin list does not exist")
        except Event.DoesNotExist:
            raise exceptions.NotFound("event list does not exist")
        except Exception as e:
            EventAdminView.handle_error(e)
 def delete(self, request, *args, **kwargs):
     try:
         logger.info("Starting function for DELETE method in ReminderView")
         access_token = kwargs['access_token']
         id = kwargs['id']
         logger.info("access token is "+access_token+" reminder id is "+id)
         user = UserProfileView.get_profile(access_token).user
         logger.info("user found from access token")
         reminder = Reminder.objects.get(pk=id)
         logger.info("reminder found from id")
         if user == reminder.user:
             logger.info("user permitted to delete reminder")
             reminder.delete()
             logger.info("reminder deleted")
             logger.info("Ending function for DELETE method in ReminderView")
             return response.Response(status=status.HTTP_202_ACCEPTED)
         else:
             logger.info("user not permitted to access reminder")
             raise exceptions.PermissionDenied()
     except Reminder.DoesNotExist:
         raise exceptions.NotFound("Reminder does not exist")
     except Exception as e:
         ReminderView.handle_error(e)
 def post(self, request, *args, **kwargs):
     try:
         access_token = kwargs['access_token']
         message = request.data['message']
         url = None
         user = UserProfileView.get_profile(access_token).user
         # if request.data.__contains__('url'):
         #     url = request.data['url']
         notification = Notification.objects.create_notification(message,user,url)
         notification.save()
         serializer = NotificationSerializer(notification)
         conn = stomp.Connection([(STOMP_SERVER_URL, STOMP_PORT)])
         conn.start()
         conn.connect(STOMP_ID, STOMP_PASSWORD, wait=True)
         conn.send(body=json.dumps(serializer.data), destination='/topic/notifications_' + str(user.id))
         logger.info("Sending a message through apollo")
         time.sleep(2)
         logger.info("Message Sent.........Disconnecting")
         conn.disconnect()
         logger.info("Disconnected !!!")
         return response.Response(serializer.data)
     except Exception as e:
         NotificationView.handle_error(e)
Beispiel #27
0
 def get_event(access_token, id, user_in=None):
     try:
         user = UserProfileView.get_profile(access_token).user
         event = Event.objects.select_related('event_wallet').get(pk=id)
         if user_in is None:
             if user in event.admin.all() or user in event.members.all():
                 return event
             else:
                 raise exceptions.PermissionDenied()
         elif user_in is "admin":
             if user in event.admin.all():
                 return event
             else:
                 raise exceptions.PermissionDenied()
         elif user_in is "member":
             if user in event.members.all():
                 return event
             else:
                 raise exceptions.PermissionDenied()
         logger.info("Completing get event method")
     except Event.DoesNotExist:
         raise exceptions.NotFound("event does not exist")
     except Exception as e:
         EventView.handle_error(e)
    def delete(self, request, *args, **kwargs):
        try:
            logger.info("Starting DELETE method")
            access_token = kwargs['access_token']
            global exitFlag
            exitFlag = 0
            id = kwargs['id']
            logger.info("access token is " + access_token + " event id is" +
                        id)
            user = UserProfileView.get_profile(access_token).user
            event = Event.objects.get(pk=id)
            logger.info("event obtained from id")
            if "user_ids" in request.data:
                if user in event.admin.all():
                    user_ids = eval(request.data['user_ids'])
                    for user_id in user_ids:
                        user_ = BdayAppUser.objects.get(pk=user_id)
                        friends = UserProfileView.get_profile(
                            access_token).app_friends.all()
                        friends_ = UserProfile.objects.get(
                            user=user_).app_friends.all()
                        if user in friends_ and user_ in friends:
                            event.members.remove(user_)
                            event.save()
                            members = event.members.all()
                            admins = event.admin.all()
                            workQueue = Queue.Queue(10)
                            queueLock.acquire()
                            for member in members:
                                chat = EventChat.objects.create_event_chat(
                                    Event.objects.get(pk=id),
                                    user,
                                    wish=None,
                                    message_field=str(user.id) + " deleted " +
                                    str(user_.id) + " from the event",
                                    url_field=None,
                                    file_field=None,
                                    type='CENTER')
                                chat.save()
                                logger.info("added center chat")
                                notification_message = "Admin " + UserProfile.objects.get(
                                    user=user
                                ).first_name + " has deleted " + UserProfile.objects.get(
                                    user=user_
                                ).first_name + " from event " + event.name
                                notification = Notification.objects.create_notification(
                                    notification_message,
                                    member,
                                    None,
                                    type='MEMBER_DELETION_BY_ADMIN',
                                    event=event)
                                notification.save()
                                serializer = NotificationSerializer(
                                    notification)
                                workQueue.put({
                                    "data":
                                    serializer.data,
                                    "destination":
                                    '/topic/notifications_' + str(member.id)
                                })
                            for admin in admins:
                                chat = EventChat.objects.create_event_chat(
                                    Event.objects.get(pk=id),
                                    user,
                                    wish=None,
                                    message_field=str(user.id) + " deleted " +
                                    str(user_.id) + " from the event",
                                    url_field=None,
                                    file_field=None,
                                    type='CENTER')
                                chat.save()
                                logger.info("added center chat")
                                notification_message = "Admin " + UserProfile.objects.get(
                                    user=user
                                ).first_name + " has deleted " + UserProfile.objects.get(
                                    user=user_
                                ).first_name + " from event " + event.name
                                notification = Notification.objects.create_notification(
                                    notification_message,
                                    admin,
                                    None,
                                    type='MEMBER_DELETION_BY_ADMIN',
                                    event=event)
                                notification.save()
                                serializer = NotificationSerializer(
                                    notification)
                                workQueue.put({
                                    "data":
                                    serializer.data,
                                    "destination":
                                    '/topic/notifications_' + str(admin.id)
                                })

                            queueLock.release()
                            threads = []
                            for i in range(0, MAX_THREADS):
                                thread = MultiThreading(
                                    "WORKER " + str(i), workQueue)
                                thread.start()
                                threads.append(thread)
                            while not workQueue.empty():
                                pass

                            # Notify threads it's time to exit
                            exitFlag = 1
                            for t in threads:
                                t.join()
                            logger.info("Sent all Notification")
                        else:
                            raise exceptions.PermissionDenied()

                    #event.members.remove(BdayAppUser.objects.get(pk=request.data['user_id']))
                    #event.save()
                    logger.info("Completing DELETE method")
                    return response.Response(status=status.HTTP_202_ACCEPTED)
                else:
                    raise exceptions.PermissionDenied()
            else:
                if user in event.members.all() and user not in event.admin.all(
                ):
                    event.members.remove(user)
                    members = event.members.all()
                    admins = event.admin.all()
                    workQueue = Queue.Queue(10)
                    queueLock.acquire()
                    for member in members:
                        event.save()
                        chat = EventChat.objects.create_event_chat(
                            Event.objects.get(pk=event.id),
                            user,
                            wish=None,
                            message_field=str(user.id) + "left the event",
                            url_field=None,
                            file_field=None,
                            type='CENTER')
                        chat.save()
                        notification_message = "Your friend " + UserProfile.objects.get(
                            user=user
                        ).first_name + " left the event " + event.name
                        notification = Notification.objects.create_notification(
                            notification_message,
                            member,
                            None,
                            type='MEMBER_LEAVING_EVENT',
                            event=event)
                        notification.save()
                        serializer = NotificationSerializer(notification)
                        workQueue.put({
                            "data":
                            serializer.data,
                            "destination":
                            '/topic/notifications_' + str(member.id)
                        })
                    for admin in admins:
                        event.save()
                        chat = EventChat.objects.create_event_chat(
                            Event.objects.get(pk=event.id),
                            user,
                            wish=None,
                            message_field=str(user.id) + "left the event",
                            url_field=None,
                            file_field=None,
                            type='CENTER')
                        chat.save()
                        notification_message = "Your friend " + UserProfile.objects.get(
                            user=user
                        ).first_name + " left the event " + event.name
                        notification = Notification.objects.create_notification(
                            notification_message,
                            admin,
                            None,
                            type='MEMBER_LEAVING_EVENT',
                            event=event)
                        notification.save()
                        serializer = NotificationSerializer(notification)
                        workQueue.put({
                            "data":
                            serializer.data,
                            "destination":
                            '/topic/notifications_' + str(admin.id)
                        })

                    queueLock.release()
                    threads = []
                    for i in range(0, MAX_THREADS):
                        thread = MultiThreading("WORKER " + str(i), workQueue)
                        thread.start()
                        threads.append(thread)
                    while not workQueue.empty():
                        pass
                    # Notify threads it's time to exit
                    exitFlag = 1
                    for t in threads:
                        t.join()
                    logger.info("Sent all Notification")
                    logger.info("Completing DELETE method")
                else:
                    raise exceptions.PermissionDenied()
        except Event.DoesNotExist:
            raise exceptions.NotFound("event does not exist")
        except Exception as e:
            EventMembersView.handle_error(e)
    def put(self, request, *args, **kwargs):
        try:
            logger.info("Starting PUT method")
            access_token = kwargs['access_token']
            id = kwargs['id']
            global exitFlag
            exitFlag = 0
            logger.info("access token is " + access_token + " event id is" +
                        id)
            user_profile = UserProfileView.get_profile(access_token)
            user = user_profile.user
            event = Event.objects.get(pk=id)
            logger.info("event obtained from id")
            if user in event.admin.all():
                user_ids = eval(request.data['user_ids'])
                for user_id in user_ids:
                    user_ = BdayAppUser.objects.get(pk=user_id)
                    friends = UserProfileView.get_profile(
                        access_token).app_friends.all()
                    friends_ = UserProfile.objects.get(
                        user=user_).app_friends.all()
                    if user in friends_ and user_ in friends:
                        event.members.add(user_)
                        event.save()
                        chat_buffer = UnreadChatBuffer.objects.create(
                            event=event, user=user_, last_read_chat=None)
                        chat_buffer.save()
                        chat = EventChat.objects.create_event_chat(
                            Event.objects.get(pk=event.id),
                            user_,
                            wish=None,
                            message_field=str(user_.id) + " joined the event",
                            url_field=None,
                            file_field=None,
                            type='CENTER')
                        chat.save()
                        logger.info("added center chat")
                        notification_message = "Your friend " + user_profile.first_name + " added you to event " + event.name
                        notification = Notification.objects.create_notification(
                            notification_message,
                            user_,
                            None,
                            type="NEW_MEMBER_ADDITION",
                            event=event)
                        notification.save()
                        logger.info("added notification message " +
                                    notification_message)
                        serializer = NotificationSerializer(notification)
                        conn = stomp.Connection([(STOMP_SERVER_URL, STOMP_PORT)
                                                 ])
                        conn.start()
                        conn.connect(STOMP_ID, STOMP_PASSWORD, wait=True)
                        conn.send(body=json.dumps(serializer.data),
                                  destination='/topic/notifications_' +
                                  str(user_.id))
                        logger.info("Sending a message through apollo")
                        time.sleep(2)
                        logger.info("Message Sent.........Disconnecting")
                        conn.disconnect()
                        logger.info("Disconnected !!!")
                    else:
                        raise exceptions.PermissionDenied()
                    event_members = event.members.all()
                    event_admins = event.admin.all()
                    workQueue = Queue.Queue(10)
                    queueLock.acquire()
                    for member in event_members:
                        if member.id is user_.id:
                            continue
                        notification_message = "Your friend " + UserProfile.objects.get(
                            user=user_
                        ).first_name + " joined the event " + event.name
                        notification = Notification.objects.create_notification(
                            notification_message,
                            member,
                            None,
                            type="NEW_MEMBER_JOINED",
                            event=event)
                        notification.save()
                        serializer = NotificationSerializer(notification)
                        workQueue.put({
                            "data":
                            serializer.data,
                            "destination":
                            '/topic/notifications_' + str(member.id)
                        })
                    for admin in event_admins:
                        notification_message = "Your friend " + UserProfile.objects.get(
                            user=user_
                        ).first_name + " joined the event " + event.name
                        notification = Notification.objects.create_notification(
                            notification_message,
                            admin,
                            None,
                            type="NEW_MEMBER_JOINED",
                            event=event)
                        notification.save()
                        serializer = NotificationSerializer(notification)
                        workQueue.put({
                            "data":
                            serializer.data,
                            "destination":
                            '/topic/notifications_' + str(admin.id)
                        })

                    queueLock.release()
                    threads = []
                    for i in range(0, MAX_THREADS):
                        thread = MultiThreading("WORKER " + str(i), workQueue)
                        thread.start()
                        threads.append(thread)
                    while not workQueue.empty():
                        pass

                    # Notify threads it's time to exit
                    exitFlag = 1
                    for t in threads:
                        t.join()
                    logger.info("Sent all notifications")
                logger.info("Completing PUT method")
                return response.Response(status=status.HTTP_202_ACCEPTED)
            else:
                raise exceptions.PermissionDenied()
        except Event.DoesNotExist:
            raise exceptions.NotFound("event does not exist")
        except Exception as e:
            EventMembersView.handle_error(e)
Beispiel #30
0
def process_payment(request):
    checksum = None
    message = {}
    if request.method == "POST":
        respons_dict = {}

        for i in request.POST.keys():
            print i
            respons_dict[i] = request.POST[i]
            if i == 'CHECKSUMHASH':
                checksum = request.POST[i]

        if 'GATEWAYNAME' in respons_dict:
            if respons_dict['GATEWAYNAME'] == 'WALLET':
                respons_dict['BANKNAME'] = 'null'

        verify = Checksum.verify_checksum(respons_dict, MERCHANT_KEY, checksum)
        print verify

        if verify:
            if respons_dict['RESPCODE'] == '01':
                print "order successful"
                for element in [
                        "SUBS_ID", "PROMO_CAMP_ID", "PROMO_STATUS",
                        "PROMO_RESPCODE"
                ]:
                    if element not in respons_dict:
                        respons_dict[element] = None
                user_profile = UserProfileView.get_profile(ACCESS_TOKEN)
                if request.session["transaction_type"] == "USER":
                    wallet = Wallet.objects.get(
                        associated_user=user_profile.user)
                else:
                    event = Event.objects.get(pk=EVENT_ID)
                    wallet = Wallet.objects.get(associated_event=event)
                with transaction.atomic():
                    amount = moneyed.Money(float(respons_dict['TXNAMOUNT']),
                                           respons_dict['CURRENCY'])
                    wallet.balance = wallet.balance + amount
                    wallet.save()
                    if request.session["transaction_type"] == "USER":
                        transaction_object = Transaction.objects.create(
                            from_wallet=None,
                            to_wallet=Wallet.objects.get(
                                associated_user=user_profile.user),
                            type='CREDIT',
                            amount=amount,
                            default_currency=respons_dict['CURRENCY'],
                            status=respons_dict["STATUS"],
                            order_id=respons_dict["ORDERID"],
                            external_transaction_flag=True,
                            external_subscription_id=respons_dict["SUBS_ID"],
                            external_transaction_id=respons_dict["TXNID"],
                            bank_transaction_id=respons_dict["BANKTXNID"],
                            transaction_date=respons_dict["TXNDATE"],
                            gateway_name=respons_dict["GATEWAYNAME"],
                            bank_name=respons_dict["BANKNAME"],
                            payment_mode=respons_dict["PAYMENTMODE"],
                            promo_camp_id=respons_dict["PROMO_CAMP_ID"],
                            promo_status=respons_dict["PROMO_STATUS"],
                            promo_response_code=respons_dict["PROMO_RESPCODE"])
                    else:
                        transaction_object = Transaction.objects.create(
                            from_wallet=None,
                            to_wallet=Wallet.objects.get(
                                associated_event=event),
                            type='CREDIT',
                            amount=amount,
                            default_currency=respons_dict['CURRENCY'],
                            status=respons_dict["STATUS"],
                            order_id=respons_dict["ORDERID"],
                            external_transaction_flag=True,
                            external_subscription_id=respons_dict["SUBS_ID"],
                            external_transaction_id=respons_dict["TXNID"],
                            bank_transaction_id=respons_dict["BANKTXNID"],
                            transaction_date=respons_dict["TXNDATE"],
                            gateway_name=respons_dict["GATEWAYNAME"],
                            bank_name=respons_dict["BANKNAME"],
                            payment_mode=respons_dict["PAYMENTMODE"],
                            promo_camp_id=respons_dict["PROMO_CAMP_ID"],
                            promo_status=respons_dict["PROMO_STATUS"],
                            promo_response_code=respons_dict["PROMO_RESPCODE"])
                    transaction_object.save()
                    if request.session["transaction_type"] == "EVENT":
                        chat = EventChat.objects.create_event_chat(
                            Event.objects.get(pk=event.id),
                            user_profile.user,
                            wish=None,
                            message_field=user_profile.user.id +
                            " contributed " + str(amount.amount),
                            url_field=None,
                            file_field=None,
                            type='CENTER')
                        chat.save()
                        members = event.members
                        for member in members:
                            notification_message = "Your friend " + user_profile.first_name + " contributed " + str(
                                amount.amount) + " to event " + event.name
                            notification = Notification.objects.create_notification(
                                notification_message, member, None)
                            notification.save()
            else:
                print "order unsuccessful because" + respons_dict['RESPMSG']
        else:
            print "order unsuccessful because" + respons_dict['RESPMSG']
        message["details"] = respons_dict['RESPMSG']
        return HttpResponse(status=200,
                            content_type="application/json",
                            content=json.dumps(message))