def create(self, request): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false role = getProfile(request).get_role_display() currentProfile = Profile.getProfile(request.user) # TODO: Need To Check if Current Profile allowed to perform this action try: if 'items' in request.POST: ext_posted_data = simplejson.loads(request.POST.get('items')) attrs = self.flatten_dict(ext_posted_data) try: del attrs['id'] except KeyError: pass try: del attrs['location'] except KeyError: pass try: del attrs['enabled'] except KeyError: pass try: del attrs['breakable'] except KeyError: pass try: del attrs['currentPrice'] except KeyError: pass try: del attrs['previousPrice'] except KeyError: pass try: del attrs['dateChanged'] except KeyError: pass try: del attrs['in_stock'] except KeyError: pass try: del attrs['on_sale'] except KeyError: pass try: del attrs['manufacturer'] except KeyError: pass if 'isPricePerUnit' in attrs: attrs['ispriceperunit'] = attrs['isPricePerUnit'] del attrs['isPricePerUnit'] else: pass if 'price' in attrs: attrs['price'] = Decimal(str(attrs['price']).replace(',','.').replace('$','').replace(' ', '')) else: pass for x in attrs: if attrs[x] == '': attrs[x] = None else: attrs[x] = attrs[x] req = check(request, id=None, attrs=attrs) if role == 'Vendor' or role == 'Vendor salesmen': if req == True: reason=[] vendor_id = Vendor.objects.get(user=currentProfile.parent_user).id try: del attrs['manufacturer'] except KeyError: pass attrs['product_num'] = str(attrs['product_num']).strip() # remove leading and trailing spaces attrs['product_num'] = attrs['product_num'].lstrip('0') # remove leading zeros product_num_vendor_id = attrs['product_num'] + '_' + str(vendor_id) try: # with transaction.commit_on_success(): table = self.model.objects.create(user=currentProfile.parent_user, vendor_id=vendor_id, date_modified = datetime.now(), product_num_vendor_id=product_num_vendor_id, **attrs) ready_data_true['items'] = table ready_data_true['total'] = 1 return ready_data_true except ValueError: logger.error('Some of fields do not correspond to their type: %s' % (attrs)) reason.append(u'Some of fields do not correspond to their type') ready_data_false['errors'] = reason return ready_data_false except IntegrityError: product = self.model.objects.get(product_num_vendor_id=product_num_vendor_id).delete() table = self.model.objects.create(user=currentProfile.parent_user, vendor_id=vendor_id, date_modified = datetime.now(), product_num_vendor_id=product_num_vendor_id, **attrs) ready_data_true['items'] = table ready_data_true['total'] = 1 return ready_data_true except Exception, err: logger.error(err.message + ', attrs: %s' % (attrs)) ready_data_false['errors'] = err.message ready_data_false['vendor_id'] = str(vendor_id) ready_data_false['attrs'] = str(attrs) return ready_data_false else: ready_data_false['errors'] = req return ready_data_false elif role == 'Customer': ready_data_false['errors'] = u'Customers can not create VendorProduct' return ready_data_false elif role == 'Admin': table = self.model.objects.create(date_modified = datetime.now(), **attrs) ready_data_true['items'] = table ready_data_true['total'] = 1 return ready_data_true elif 'products' in request.POST: ext_posted_data = simplejson.loads(request.POST.get('products')) data = [] error = [] vendor_id = None if role == 'Customer': if 'vendor_id' in request.POST: vendor_id = int(request.POST.get('vendor_id')) else: ready_data_false['errors'] = u'vendor_id is Required Field' return ready_data_false if role == 'Vendor' or role == 'Vendor salesmen': vendor_id = Vendor.objects.get(user=currentProfile.parent_user).id if vendor_id == None: ready_data_false['errors'] = u'vendor_id is Required Field' return ready_data_false for attrs in ext_posted_data: attrs['product_num'] = str(attrs['product_num']).strip() # remove leading and trailing spaces #attrs['product_num'] = attrs['product_num'].lstrip('0') # remove leading zeros product_num_vendor_id = attrs['product_num'] + '_' + str(vendor_id) try: if 'isPricePerUnit' in attrs: attrs['ispriceperunit'] = attrs['isPricePerUnit'] del attrs['isPricePerUnit'] else: pass if 'price' in attrs: if attrs['price'] == '': attrs['price'] = '0' attrs['price'] = Decimal(str(attrs['price']).replace(',','.').replace('$','').replace(' ', '')) else: pass for x in attrs: if attrs[x] == '': attrs[x] = None #else: # attrs[x] = attrs[x] req = check(request, id=None, attrs=attrs) if req == True: #with transaction.commit_on_success(): try: exist = self.model.objects.filter(product_num_vendor_id=product_num_vendor_id, user=currentProfile.parent_user).exists() if exist: self.model.objects.filter(product_num_vendor_id=product_num_vendor_id, user=currentProfile.parent_user).delete() product = self.model.objects.create(date_modified = datetime.now(), vendor_id = vendor_id, user=currentProfile.parent_user, product_num_vendor_id=product_num_vendor_id, **attrs) data.append(product) except IntegrityError: try: product = self.model.objects.create(user=currentProfile.parent_user, vendor_id=vendor_id, date_modified = datetime.now(), product_num_vendor_id=product_num_vendor_id, **attrs) data.append(product) except Exception, err: error.append(err) else: ready_data_false['errors'] = req return ready_data_false
def update(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false user = getParentuser(request) role = getProfile(request).get_role_display() currentProfile = Profile.getProfile(request.user) # TODO: Need To Check if Current Profile allowed to perform this action ext_posted_data = simplejson.loads(request.POST.get('items')) attrs = self.flatten_dict(ext_posted_data) try: del attrs['id'] except KeyError: pass try: del attrs['date_modified'] except KeyError: pass if 'isPricePerUnit' in attrs: attrs['ispriceperunit'] = attrs['isPricePerUnit'] del attrs['isPricePerUnit'] else: pass if 'price' in attrs: attrs['price'] = Decimal(str(attrs['price']).replace(',','.').replace('$','').replace(' ', '')) else: pass reason=[] if id: req = check(request, id, attrs=attrs) if role == 'Vendor' or role == 'Vendor salesmen': if req == True: product = self.model.objects.filter(pk=id).update(date_modified = datetime.now(), **attrs) product = self.model.objects.get(pk=id) ready_data_true['items'] = product ready_data_true['total'] = 1 return ready_data_true else: ready_data_false['errors'] = req return ready_data_false elif role == 'Customer': ready_data_false['errors'] = u'Customer can not update VendorProducts' return ready_data_false elif role == 'Admin': if req == True: product = self.model.objects.filter(pk=id).update(date_modified = datetime.now(), **attrs) product = self.model.objects.get(pk=id) ready_data_true['items'] = product ready_data_true['total'] = 1 return ready_data_true else: ready_data_false['errors'] = req return ready_data_false else: reason.append(u'Product ID is required') ready_data_false['errors'] = reason return ready_data_false
def read(self, request, id=None): ready_data_true = {'success': True } ready_data_false = {'success': False} if not request.user.is_authenticated(): ready_data_false["error"] = "Authorization is failed" return ready_data_false currentProfile = Profile.getProfile(request.user) parentProfile = Profile.getProfile(currentProfile.parent_user) # TODO: Need To Check if Current Profile allowed to perform this action total = 0 vendors = [] relationships = Relationship.objects.filter(customer=parentProfile) for relationship in relationships: vendors.append(relationship.vendor.parent_user.id) role = currentProfile.get_role_display() if id: req = check(request, id, attrs=None) if role == 'Customer': if req == True: product = self.model.objects.get(pk=id, user=currentProfile.parent_user) if product.user_id in vendors: ready_data_true['items'] = product ready_data_true['total'] = 1 return ready_data_true else: ready_data_false['errors'] = u'Not Found' return ready_data_false else: ready_data_false['errors'] = req return ready_data_false elif role == 'Vendor' or role == 'Vendor salesmen': if req == True: product = self.model.objects.get(pk=id) if product.user_id == currentProfile.user: ready_data_true['items'] = product ready_data_true['total'] = 1 return ready_data_true else: ready_data_false['errors'] = u'Not Found' return ready_data_false else: ready_data_false['errors'] = req return ready_data_false elif role == 'Admin': try: product_id = int(id) except ValueError: reason.append(u'id is not a number') return reason else: try: product = self.model.objects.get(pk=id) except ObjectDoesNotExist: ready_data_false['errors'] = u'Product not found by ID' return ready_data_false else: ready_data_true['items'] = product return ready_data_true else: sort = 'name1' if 'sort' in request.GET: sort = request.GET['sort'] if 'dir' in request.GET: if 'DESC' == request.GET['dir']: sort = '-' + sort if role == 'Customer' and 'vendor_id' in request.GET: vendor_id = request.GET['vendor_id'] if 'start' in request.GET and 'limit' in request.GET: start = int(request.GET['start']) end = start + int(request.GET['limit']) if 'search' in request.GET: search = request.GET['search'] words = search.split() sq = Q() if len(words) > 1: # search is multiple words sq_name = Q() sq_category = Q() sq_product_num = Q() sq_upc = Q() sq_brand = Q() sq_notes = Q() for word in words: sq_name = sq_name & Q(name1__icontains=word) sq_category = sq_category & Q(category__icontains=word) sq_product_num = sq_product_num & Q(product_num__icontains=word) sq_upc = sq_upc & Q(upc__icontains=word) sq_brand = sq_brand & Q(brand__icontains=word) sq_notes = sq_notes & Q(notes__icontains=word) sq = sq | sq_name | sq_category | sq_product_num | sq_brand | sq_notes | sq_upc else: # search is a single word sq = sq | Q(upc__icontains=search) | Q(name1__icontains=search) | Q(category__icontains=search) sq = sq | Q(product_num__icontains=search) | Q(brand__icontains=search) | Q(notes__icontains=search) total = self.model.objects.filter(sq,vendor=vendor_id,user=currentProfile.parent_user).count() products = self.model.objects.filter(sq,vendor=vendor_id,user=currentProfile.parent_user).order_by(sort)[start:end] ready_data_true['items'] = products ready_data_true['total'] = total ready_data_true['sq'] = str(sq) return ready_data_true else: ready_data_true['total'] = self.model.objects.filter(user=currentProfile.parent_user, vendor=vendor_id).count() ready_data_true['items'] = self.model.objects.filter(user=currentProfile.parent_user, vendor=vendor_id).order_by(sort)[start:end] return ready_data_true else: ready_data_true['total'] = self.model.objects.filter(vendor=vendor_id).count() ready_data_true['items'] = self.model.objects.filter(vendor=vendor_id).order_by(sort) return ready_data_true if role == 'Customer': products_keywords = Q() for vendor in vendors: products_keywords = products_keywords | Q(user=vendor) if 'start' in request.GET and 'limit' in request.GET: start = request.GET['start'] end = start + request.GET['limit'] ready_data_true['total'] = self.model.objects.filter(products_keywords).count() ready_data_true['items'] = self.model.objects.filter(products_keywords).order_by(sort)[start:end] return ready_data_true else: ready_data_true['total'] = self.model.objects.filter(products_keywords).count() ready_data_true['items'] = self.model.objects.filter(products_keywords).order_by(sort) return ready_data_true elif role == 'Vendor' or role == 'Vendor salesmen': if 'start' in request.GET and 'limit' in request.GET: start = int(request.GET['start']) end = start + int(request.GET['limit']) if 'filter' in request.GET: filter = request.GET['filter'] # TODO: add search upc code for filter total = self.model.objects.filter(user=currentProfile.user).filter(name1__icontains=filter).count() products = self.model.objects.filter(user=currentProfile.user).filter(name1__icontains=filter).order_by(sort)[start:end] if total == 0: #if search did not find a filter let's also look in field product_num total = self.model.objects.filter(user=currentProfile.user).filter(product_num__icontains=filter) products = self.model.objects.filter(user=currentProfile.user).filter(product_num__icontains=filter).order_by(sort)[start:end] ready_data_true['items'] = products ready_data_true['total'] = total return ready_data_true else: total = self.model.objects.filter(user=currentProfile.user).count() products = self.model.objects.filter(user=currentProfile.user).order_by(sort)[start:end] ready_data_true['items'] = products ready_data_true['total'] = total return ready_data_true else: ready_data_true['items'] = self.model.objects.filter(user=currentProfile.user).order_by(sort) ready_data_true['total'] = len(ready_data_true['items']) return ready_data_true elif role == 'Admin': if 'start' in request.GET and 'limit' in request.GET: start = request.GET['start'] end = start + request.GET['limit'] ready_data_true['items'] = self.model.objects.all().order_by(sort)[start:end] ready_data_true['total'] = self.model.objects.all().count() return ready_data_true else: ready_data_true['items'] = self.model.objects.all().order_by(sort) ready_data_true['total'] = len(ready_data_true['items']) return ready_data_true