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)
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)
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 put(self, request, *args, **kwargs): access_token = kwargs['access_token'] try: access_token = kwargs['access_token'] event_id = kwargs['event_id'] last_read_chat = EventChat.objects.get( pk=request.data['last_read_chat_id']) user = UserProfileView.get_profile(access_token=access_token).user data = UnreadChatBuffer.objects.get( event=Event.objects.get(pk=event_id), user=user) data.last_read_chat = last_read_chat data.save() 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)
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)
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 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)
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))
def process_payment(request): global exitFlag exitFlag = 0 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( request.session["ACCESS_TOKEN"]) if request.session["transaction_type"] == "USER": wallet = Wallet.objects.get( associated_user=user_profile.user) else: event = Event.objects.get(pk=request.session["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": members = event.members.all() admins = event.admin.all() workQueue = Queue.Queue(10) queueLock.acquire() 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() serializer = NotificationSerializer(notification) workQueue.put({ "data": serializer.data, "destination": '/topic/notifications_' + str(member.id) }) for admin in admins: notification_message = "Your friend " + user_profile.first_name + " contributed " + str( amount.amount) + " to event " + event.name notification = Notification.objects.create_notification( notification_message, admin, None) 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() print "Sent all Notification" 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))