def discard(self, request, pk=None): from apps.order.serializers import OrderBaseSr from apps.order.models import Status order = get_object_or_404(Order, pk=pk) order = MoveStatusUtils.move(order, Status.DISCARD, staff=request.user.staff) return res(OrderBaseSr(order).data)
def delete_list(self, request): pk = self.request.query_params.get('ids', '') pk = [int(pk)] if pk.isdigit() else map(lambda x: int(x), pk.split(',')) result = Transaction.objects.filter(pk__in=pk) if result.count() == 0: raise Http404 result.delete() return res(status=status.HTTP_204_NO_CONTENT)
def add(self, request): if 'staff' not in request.data: request.data['staff'] = self.request.user.staff.pk data = request.data serializer = BagBaseSr(data=data) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def change(self, request, pk=None): obj = self.get_object(pk) data = Tools.clean_and_upper_key(request.data, 'uid') data = Tools.clean_key(request.data, 'purchase_code') serializer = BolBaseSr(obj, data=data, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def change_status(self, request, pk=None): obj = get_object_or_404(Order, pk=pk) status = request.data.get('value', obj.status) obj = MoveStatusUtils.move(obj, int(status), approver=request.user.staff) serializer = OrderBaseSr(obj) return res(serializer.data)
def delete_list(self, request): pk = self.request.query_params.get('ids', '') pks = [int(pk)] if pk.isdigit() else map(lambda x: int(x), pk.split(',')) for pk in pks: item = get_object_or_404(Customer, pk=pk) item.delete() return res(status=status.HTTP_204_NO_CONTENT)
def unmark_cn(self, request, pk=None): obj = self.get_object(pk) if obj.vn_date is not None: raise ValidationError( {"uid": "Vận đơn này đã ghi nhận về kho VN."}) obj.cn_date = None obj.save() serializer = BolBaseSr(obj) return res(serializer.data)
def change_address(self, request, pk=None): obj = get_object_or_404(Order, pk=pk) blank, address = Tools.obj_from_pk(Address, request.data.get('value', None)) if not blank and not address: # Address not exist -> do nothing serializer = OrderBaseSr(obj) else: serializer = OrderUtils.partial_update(obj, 'address', address.pk) return res(serializer.data)
def mark_cn(self, request, pk=None): obj = self.get_object(pk) if obj.cn_date is not None: raise ValidationError( {"uid": "Vận đơn này đã ghi nhận về kho TQ."}) data = Tools.clean_and_upper_key(request.data, 'uid') serializer = BolBaseSr(obj, data=data, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def batch_update(self, request, pk=None): order = get_object_or_404(Order, pk=pk) value = request.data.get('value', 0) extra = request.data.get('extra', []) ids = extra.get('ids', []) field = extra.get('field', '') params = {} params[field] = value OrderItemUtils.batch_update(order, ids, params) return res({})
def renew_discard(self, request, pk=None): from apps.order.serializers import OrderBaseSr from apps.order.models import Status order = get_object_or_404(Order, pk=pk) if hasattr(request.user, 'customer') and order.customer != request.user.customer: raise ValidationError( "Bạn không thể khởi tạo lại đơn hàng của người khác.") order = MoveStatusUtils.move(order, Status.NEW) return res(OrderBaseSr(order).data)
def change_cust_care(self, request, pk=None): obj = get_object_or_404(Order, pk=pk) blank, staff = Tools.obj_from_pk(Staff, request.data.get('value', None)) if not blank and not staff: # Staff not exist -> do nothing serializer = OrderBaseSr(obj) else: serializer = OrderUtils.partial_update(obj, 'cust_care', staff.pk) return res(serializer.data)
def post(self, request, format=None): params = self.request.data administrator = self.get_object().administrator serializer = AdministratorUpdateSerializer(administrator, data=params, partial=True) if serializer.is_valid() is True: serializer.save() return res(serializer.data) else: return err_res(serializer.errors)
def retrieve(self, request, pk=None): order = get_object_or_404(Order, pk=pk) serializer = OrderBaseSr(order) data = serializer.data data['options'] = { 'addresses': AddressBaseSr( Address.objects.filter(customer_id=order.customer_id), many=True).data } return res(data)
def early_discard(self, request, pk=None): from apps.order.serializers import OrderBaseSr from apps.order.models import Status order = get_object_or_404(Order, pk=pk) staff = None if hasattr(request.user, 'customer') and order.customer != request.user.customer: raise ValidationError("Bạn không thể huỷ đơn hàng của người khác.") if hasattr(request.user, 'staff'): staff = request.user.staff order = MoveStatusUtils.move(order, Status.DISCARD, staff=staff) return res(OrderBaseSr(order).data)
def match_vn(self, request): uid = request.data.get('bol_uid', '') bag_uid = request.data.get('bag_uid', '') uid = Tools.remove_special_chars(uid, True) bag_uid = Tools.remove_special_chars(bag_uid, True) obj = get_object_or_404(Bol, uid=uid, bag__uid=bag_uid) if not obj.vn_date: obj.vn_date = timezone.now() obj.save() return res(BolBaseSr(obj).data)
def export_check(self, request): ids = [ int(pk) for pk in self.request.query_params.get('ids', '').split(',') ] bols = Bol.objects.filter(pk__in=ids) status = BolUtils.export_check(bols, ids) if status: raise ValidationError(status) return res({'ok': True})
def complaint_resolve(self, request, pk=None): order = get_object_or_404(Order, pk=pk) decide = int(request.data.get('decide', ComplaintDecide.AGREE)) if decide == ComplaintDecide.AGREE: OrderUtils.complaint_agree(order) elif decide == ComplaintDecide.MONEY_BACK: OrderUtils.complaint_money_back(order) elif decide == ComplaintDecide.CHANGE: OrderUtils.complaint_change(order) else: raise ValidationError( "Lựa chọn giải quyết khiếu nại không hợp lệ.") return res({})
def get(self, request, format=None): token = self.request.GET.get("token", "") item = self.get_object(token, "reset_password_token") user = item.user if item.reset_password_token == "": raise Http404 user.password = item.reset_password_tmp item.reset_password_tmp = "" item.reset_password_token = "" item.reset_password_created = None user.save() item.save() return res({})
def bulk_approve(self, request): from .utils import OrderUtils pks = self.request.data.get('ids', []) sale = int(self.request.data.get('sale', 0)) if not sale: sale = None for pk in pks: order = get_object_or_404(Order, pk=pk) success, message = OrderUtils.approve(order, request.user.staff, sale) if not success: raise ValidationError(message) return res({'approved': pks})
def post(self, request, format=None): params = self.request.data user = self.get_object() # Check correct old password if check_password(params["oldPassword"], user.password) is False: return err_res({"detail": "Incorrect old password"}) user.password = make_password(params["password"]) user.save() return res({})
def change(self, request, pk=None): obj = get_object_or_404(Staff, pk=pk) data = Tools.parse_user_related_data(request.data) userSr = UserSr(obj.user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': obj.user_id}) serializer = StaffBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def post(self, request, format=None): params = self.request.data try: item = self.get_object(params["username"]) except Staff.DoesNotExist: return res({"url": ""}) user = item.user token = Tools.get_uuid() item.reset_password_token = token item.reset_password_tmp = make_password(params["password"]) item.reset_password_created = timezone.now() item.save() url = settings.BASE_URL + "admin/reset-password/" + str(token) subject = "Rest set password for %s %s" % (user.first_name, user.last_name) body = "Reset password confirm link: %s" % (url) to = user.email Tools.send_email_async(subject, body, to) return res({"url": url})
def add(self, request): data = Tools.parse_user_related_data(request.data) userSr = UserSr(data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': userSr.data['id']}) serializer = StaffBaseSr(data=remain) if serializer.is_valid(raise_exception=True): serializer.save() serializer.data.update({'user': userSr.data}) return res(serializer.data)
def retrieve_to_print(self, request, pk=None): transaction = get_object_or_404(Transaction, pk=pk) return res({ 'company_info': VariableUtils.get_company_info(), 'customer': { 'fullname': Tools.get_fullname(transaction.customer), 'address': transaction.customer.address, }, 'uid': transaction.uid, 'created_at': transaction.created_at, 'amount': transaction.amount, 'note': transaction.note })
def change_purchase_code(self, request, pk=None): from apps.bol.models import Bol from apps.order.models import Status obj = get_object_or_404(Order, pk=pk) value = request.data.get('value', obj.purchase_code) if value and obj.status == Status.DEBT: obj = MoveStatusUtils.move(obj, Status.PAID) if not value and obj.status == Status.PAID: obj = MoveStatusUtils.move(obj, Status.DEBT) value = Tools.remove_special_chars(value) serializer = OrderUtils.partial_update(obj, 'purchase_code', value) data = serializer.data Bol.objects.filter(purchase_code=data.get('purchase_code')).update( order=obj) return res(data)
def add(self, request): data = Tools.parse_user_related_data(request.data) userSr = UserSr(data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': userSr.data['id']}) if type(remain.get('avatar')) is not InMemoryUploadedFile: remain.pop('avatar', None) serializer = CustomerBaseSr(data=remain) if serializer.is_valid(raise_exception=True): serializer.save() serializer.data.update({'user': userSr.data}) return res(serializer.data)
def change(self, request, pk=None): obj = get_object_or_404(Customer, pk=pk) data = Tools.parse_user_related_data(request.data) userSr = UserSr(obj.user, data=data['user']) if userSr.is_valid(raise_exception=True): userSr.save() remain = data['remain'] remain.update({'user': obj.user_id}) if type(remain.get('avatar')) is not InMemoryUploadedFile: remain.pop('avatar', None) serializer = CustomerBaseSr(obj, data=remain) if serializer.is_valid(raise_exception=True): serializer.save() return res(serializer.data)
def check(self, request): from apps.order.models import Order from apps.order.utils import OrderUtils uid = request.data.get('uid', None) checked_items = request.data.get('checked_items', {}) potential_bols = request.data.get('potential_bols', '') order = get_object_or_404(Order, uid=uid) remain = OrderUtils.check(order, checked_items) if len(remain.keys()): order.pending = True order.checked_date = timezone.now() order.checker = request.user.staff order.potential_bols = potential_bols order.save() return res(remain)
def export(self, request): from apps.receipt.models import Receipt, Type from apps.transaction.utils import TransactionUtils ids = [ int(pk) for pk in self.request.query_params.get('ids', '').split(',') ] bols = Bol.objects.filter(pk__in=ids) status = BolUtils.export_check(bols, ids) if status: raise ValidationError(status) vnd_delivery_fee = int(self.request.query_params.get('vnd_sub_fee', 0)) note = self.request.query_params.get('note', '') customer = bols[0].customer staff = request.user.staff type = BolUtils.get_bols_type(bols) address = bols[0].address receipt = Receipt(vnd_delivery_fee=vnd_delivery_fee, note=note, customer=customer, staff=staff, type=type, address=address) receipt.save() if vnd_delivery_fee: TransactionUtils.charge_receipt_vnd_delivery_fee( vnd_delivery_fee, customer, staff, receipt) total = vnd_delivery_fee if type == Type.TRANSPORT: total = total + BolUtils.export_transport_bols( bols, receipt, customer, staff) else: total = total + BolUtils.export_order_bols(bols, receipt, customer, staff) receipt.vnd_total = total receipt.save() return res({'ok': True})