def addOrderForwarderFeedbackToBuyer(request): try: user = request.user profile = Profile.objects.get(user=user) order = Order.objects.get(id=int(request.data.get('orderId')), service__profile=profile) orderFeedback = getOrderFeedback(order, profile) orderFeedback.score = request.data.get('score') orderFeedback.text = request.data.get('text') orderFeedback.save() buyerProfile = order.profile buyerProfile.feedback = FeedbackManager.updateBuyerProfileFeedback(buyerProfile) buyerProfile.save() feedbackNotification = Notification() feedbackNotification.alert = True feedbackNotification.user = buyerProfile.user orderLink = "%sbuyer" % ( feedbackNotification.getEmailLinkBaseUrl() ) alertText = "User %s left a feedback %.2f/5" % (profile.user.username, orderFeedback.score) feedbackNotification.alertData = "%s|%s" % (alertText, "/buyer") feedbackNotification.save() orderSerializer = OrderWithAddressesSerializer(order, context={'request': request}) return Response({'success': True, 'order': orderSerializer.data}) except Exception as e: return Response({'success': False, 'error': e.message})
def send2FASMS(request): email = request.data.get('email') if email: email = email.lower() success = False try: user = get_object_or_404(User, email__iexact=email) profile = get_object_or_404(Profile, user=user) wallet = Wallet.objects.get(profile=profile) TransactionManager.createVerify2FASMSTransaction( settings.TWO_FACTOR_SMS_COST, wallet.id) profile.setVerificationCode() profile.save() messageSend = sendVerificationCodeSMS(request, profile.verificationCode, profile.mobile) success = messageSend except ValueError: profile.enable2FASMS = False notification = Notification() notification.alert = True notification.user = profile.user alertText = "Two factor authentication has been disabled due to low credit" notification.alertData = alertText notification.save() profile.save() except Exception as e: return Response({ 'success': False, 'error': { 'code': 'no.sms.sent', 'msg': e.message } }) return Response({'success': success})
def cancel(request): try: user = request.user profile = Profile.objects.get(user=user) order = Order.objects.get(id=request.data.get('orderId'), profile=profile) order.state = ORDER_STATUS['cancelled'] order.save() orderSerializer = OrderProtectedSerializer(order, context={'request': request}) # Notification to forwarder orderNotification = Notification() orderNotification.email = True orderNotification.alert = True orderNotification.user = order.service.profile.user orderLink = "%sorders/forwarder/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id ) orderNotification.setEmailData( "LWF Order %s" % (order.state), "notifications/email/forwarder_order_change_status.html", { 'order': order, 'orderLink': orderLink } ) alertText = "Order #%d changed status to %s" % (order.id, order.state) orderNotification.alertData = "%s|%s|%d" % (alertText, "/orders/forwarder/", order.id) orderNotification.save() return Response({'success': True, 'order': orderSerializer.data}) except Exception as e: return Response({'success': False, 'error': e.message})
def updateForwarderTrackingInfo(request): try: user = request.user profile = Profile.objects.get(user=user) order = Order.objects.get(id=request.data.get('orderId'), service__profile=profile) try: orderTrackingInfo = OrderTrackingInfo.objects.get( order=order, profile=profile, fromForwarder=True, trackingStatus=settings.ORDER_TRACKING_INFO_STATUS_CHOICES[1][0] ) orderTrackingInfo.courier = request.data.get('courier') orderTrackingInfo.courierOther = request.data.get('courierOther') orderTrackingInfo.trn = request.data.get('trn') orderTrackingInfo.link = request.data.get('link') except OrderTrackingInfo.DoesNotExist: orderTrackingInfo = OrderTrackingInfo( order=order, profile=profile, fromForwarder=True, courier=request.data.get('courier'), courierOther=request.data.get('courierOther'), trn=request.data.get('trn'), link=request.data.get('link'), trackingStatus=settings.ORDER_TRACKING_INFO_STATUS_CHOICES[1][0] ) orderTrackingInfo.save() order.save() orderSerializer = OrderWithAddressesSerializer(order, context={'request': request}) # Notification to buyer orderNotification = Notification() orderNotification.email = True orderNotification.alert = True orderNotification.user = order.profile.user orderLink = "%sorders/buyer/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id ) orderNotification.setEmailData( "LWF Order Tracking Info Updated", "notifications/email/buyer_order_tracking_changed.html", { 'order': order, 'orderTrackingInfo': orderTrackingInfo, 'orderLink': orderLink } ) alertText = "Order #%d tracking info updated" % (order.id) orderNotification.alertData = "%s|%s|%d" % (alertText, "/orders/buyer/", order.id) orderNotification.save() return Response({'success': True, 'order': orderSerializer.data}) except Exception as e: return Response({'success': False, 'error': e.message})
def adminAjaxPostChatMessage(request): r = {'success': False, 'chat': None} adminUser = User.objects.get(username="******") #EMULATE ADMIN USER try: chatId = int(request.POST['chatId']) chat = Chat.objects.get(id=chatId) message = request.POST['message'] chatMessage = ChatMessage(sender=adminUser, text=message, chat=chat) chatMessage.save() issue = Issue.objects.get(chat=chat) issueNotificationBuyer = Notification() issueNotificationBuyer.alert = True issueNotificationBuyer.user = issue.order.profile.user alertText = "Admin sent a new message" issueLink = "%sissue/%d" % ( issueNotificationBuyer.getEmailLinkBaseUrl(), issue.id) issueNotificationBuyer.alertData = "%s|%s" % (alertText, issueLink) issueNotificationBuyer.save() issueNotificationForwarder = Notification() issueNotificationForwarder.alert = True issueNotificationForwarder.user = issue.order.service.profile.user alertText = "Admin sent a new message" issueLink = "%sissue/%d" % ( issueNotificationForwarder.getEmailLinkBaseUrl(), issue.id) issueNotificationForwarder.alertData = "%s|%s" % (alertText, issueLink) issueNotificationForwarder.save() chatSerializer = ChatSerializer(chat, context={'request': request}) chatMessageSerializer = ChatMessageSerializer( chatMessage, context={'request': request}) r['chat'] = chatSerializer.data r['message'] = chatMessageSerializer.data r['success'] = True except Exception, ex: r['error'] = "%s" % (traceback.format_exc())
def createIssue(request): try: order = Order.objects.get(id=request.data.get('order')) if checkExistingIssue(order): raise ValueError("Issue Already exists") user = request.user profile = Profile.objects.get(user=user) chat = createChat(order) issueSerializer = IssueProtectedSerializer(data=request.data) if issueSerializer.is_valid(): request.data['profile'] = profile request.data['order'] = order request.data['chat'] = chat issue = issueSerializer.create(request.data) issue.save() issueNotification = Notification() issueNotification.alert = True if profile == order.profile: issueNotification.user = issue.order.service.profile.user alertText = "User %s opened an issue" % issue.order.profile.user.username else: issueNotification.user = issue.order.profile.user alertText = "User %s opened an issue" % issue.order.service.profile.user.username issueLink = "%sissue/%d" % ( issueNotification.getEmailLinkBaseUrl(), issue.id ) issueNotification.alertData = "%s|%s|%d" % (alertText, "/issue", issue.id) issueNotification.save() return Response({'success': True, 'issueId': issue.id}, status=status.HTTP_201_CREATED) else: return Response({'success': False, 'errors': issueSerializer.errors}) except Profile.DoesNotExist: return Response({'success': False, 'error': 'profile.notfound'}) except Exception as e: return Response({'success': False, 'errors': e.message})
def forwardedDelivered(request): try: user = request.user profile = Profile.objects.get(user=user) order = Order.objects.get(id=request.data.get('orderId'), service__profile=profile) if order.service.type == settings.SERVICE_TYPES[0][0]: order.state = ORDER_STATUS['forwarded'] order.forwardedDate = django.utils.timezone.now() if order.service.type == settings.SERVICE_TYPES[1][0]: order.state = ORDER_STATUS['delivered'] order.save() orderSerializer = OrderWithAddressesSerializer(order, context={'request': request}) # Notification to buyer orderNotification = Notification() orderNotification.email = True orderNotification.alert = True orderNotification.user = order.profile.user orderLink = "%sorders/buyer/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id ) orderNotification.setEmailData( "LWF Order %s" % (order.state), "notifications/email/buyer_order_change_status.html", { 'order': order, 'orderLink': orderLink } ) alertText = "Order #%d changed status to %s" % (order.id, order.state) orderNotification.alertData = "%s|%s|%d" % (alertText, "/orders/buyer/", order.id) orderNotification.save() return Response({'success': True, 'order': orderSerializer.data}) except Exception as e: return Response({'success': False, 'error': e.message})
def orderPayment(request): try: order = Order.objects.get(id=request.data.get("orderId")) inWalletId = order.profile.wallet.id outWalletId = order.service.profile.wallet.id feePercentage = Configuration().getConfiguration( "forwarding_fee_percentage_level_1") TransactionManager.createPaymentTransaction( amount=order.totalPrice, order=order, inWalletId=inWalletId, outWalletId=outWalletId, feePercentage=feePercentage) # Notification to forwarder orderNotification = Notification() orderNotification.email = True orderNotification.alert = True orderNotification.user = order.service.profile.user orderLink = "%sorders/forwarder/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id) orderNotification.setEmailData( "New LWF Order", "notifications/email/forwarder_order_new_status.html", { 'order': order, 'orderLink': orderLink }) alertText = "Order #%d has been paid" % order.id orderNotification.alertData = "%s|%s|%d" % ( alertText, "/orders/forwarder", order.id) orderNotification.save() return Response({'success': True}) except Exception as e: return Response({'success': False, 'errors': e.message})
def createIssueChatMessage(request): try: user = request.user profile = Profile.objects.get(user=user) file = request.data.get('messageImage') text = request.data.get('messageText') chatId = int(request.data.get('chatId')) query = Q(profile=profile) | Q(order__buyer=profile) | Q( order__courier=profile) issue = InstantDeliveryOrderIssue.objects.get( query, state=issueStatus['OPEN'], chat__id=chatId) chatMessage = ChatMessage() if file: if file.find("http://") > -1 or file.find("https://") > -1: imgstr = base64.b64encode(requests.get(file).content) ext = file.split('/')[-1].split(".")[-1] noteImageName = "%d.%s" % (user.id, ext) data = ContentFile(base64.b64decode(imgstr), name=noteImageName) if data.size > settings.MAX_IMAGE_SIZE_UPLOAD: return Response({ 'success': False, 'error': 'file.toobig' }, 500) else: format, imgstr = request.data.get('messageImage').split( ';base64,') ext = format.split('/')[-1] noteImageName = "%d.%s" % (user.id, ext) data = ContentFile(base64.b64decode(imgstr), name=noteImageName) if data.size > settings.MAX_IMAGE_SIZE_UPLOAD: return Response({ 'success': False, 'error': 'file.toobig' }, 500) chatMessage.image = data chatMessage.chat = issue.chat chatMessage.sender = profile.user chatMessage.text = text chatMessage.save() issue = InstantDeliveryOrderIssue.objects.get(query, chat__id=chatId) issueSerializer = IssueSerializer(issue, context={'request': request}) issueNotification = Notification() issueNotification.alert = True if profile == issue.order.courier: issueNotification.user = issue.order.courier.user alertText = "User %s sent a new message" % issue.order.courier.user.username else: issueNotification.user = issue.order.buyer.user alertText = "User %s sent a new message" % issue.order.buyer.user.username issueLink = "%sissue/%d" % (issueNotification.getEmailLinkBaseUrl(), issue.id) issueNotification.alertData = "%s|%s|%d" % (alertText, "/issue", issue.id) issueNotification.save() return Response({'success': True, 'issue': issueSerializer.data}) except Profile.DoesNotExist: return Response({'success': False, 'error': 'profile.notfound'}) except Exception as e: return Response({'success': False, 'errors': e.message})
def createOrderNote(request): try: user = request.user profile = Profile.objects.get(user=user) file = request.data.get('noteImage') description = request.data.get('noteText') orderId = int(request.data.get('orderId')) query = Q(profile=profile) | Q(service__profile=profile) order = Order.objects.get(query, id=orderId) maxOrderNotes = int(Configuration.objects.get( key__iexact='max_order_notes' ).value) if len(order.notes) >= maxOrderNotes: return Response({'success': False, 'error': 'maxOrderNotesReached'}) if file: if file.find("http://") > -1 or file.find("https://") > -1: imgstr = base64.b64encode(requests.get(file).content) ext = file.split('/')[-1].split(".")[-1] noteImageName = "%d.%s" % (user.id, ext) data = ContentFile(base64.b64decode(imgstr), name=noteImageName) if data.size > settings.MAX_IMAGE_SIZE_UPLOAD: return Response({'success': False, 'error':'file.toobig'}, 500) elif request.data.get('noteImage').find(';base64,') > -1: format, imgstr = request.data.get('noteImage').split(';base64,') ext = format.split('/')[-1] noteImageName = "%d.%s" % (user.id, ext) data = ContentFile(base64.b64decode(imgstr), name=noteImageName) if data.size > settings.MAX_IMAGE_SIZE_UPLOAD: return Response({'success': False, 'error':'file.toobig'}, 500) else: data = None newOrderNote = OrderNote() newOrderNote.profile = profile newOrderNote.order = order newOrderNote.orderStatus = order.state if data: if data.size < settings.MAX_IMAGE_SIZE_UPLOAD: newOrderNote.document = data newOrderNote.description = description newOrderNote.save() order = Order.objects.get(id=orderId) if order.state == ORDER_STATUS['paid'] \ or order.state == ORDER_STATUS['new'] \ or order.state == ORDER_STATUS['refused']: orderSerializer = OrderProtectedSerializer( order, context={'request': request} ) else: orderSerializer = OrderWithAddressesSerializer( order, context={'request': request}, ) # Notification to buyer or Forwarder orderNotification = Notification() orderNotification.email = True orderNotification.alert = True alertText = "Order #%d has new note" % (order.id) if user.id == order.profile.user.id: orderNotification.user = order.service.profile.user notificationProfile = order.service.profile orderLink = "%sorders/forwarder/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id ) orderNotification.alertData = "%s|%s|%d" % (alertText, "/orders/forwarder/", order.id) else: orderNotification.user = order.profile.user notificationProfile = order.profile orderLink = "%sorders/buyer/%s" % ( orderNotification.getEmailLinkBaseUrl(), order.id ) orderNotification.alertData = "%s|%s|%d" % (alertText, "/orders/buyer/", order.id) orderNotification.setEmailData( "LWF Order Has New Note", "notifications/email/order_note_new.html", { 'order': order, 'profile': notificationProfile, 'orderLink': orderLink } ) orderNotification.save() return Response({'success': True, 'order': orderSerializer.data}) else: return Response({'success': False, 'error': 'nofile'}) except Exception as e: return Response({'success': False, 'error': e.message})