def __init__(self, *args, **kwargs): super(AdminFrontListForm, self).__init__(*args, **kwargs) self.fields['title_2'].required = False self.fields['description'].required = False self.fields['rule_name'].required = False self.fields['category'].required = False self.fields['top_limit'].required = False self.fields['max_limit'].required = False self.fields['front_products'].required = False if "instance" in kwargs: instance = kwargs["instance"] if instance and instance.products.exists(): product_model = instance.products.first().product_model if product_model == "Book": Book = load_model(app_label="book_rental", model_name="Book") fl_products = instance.products.values_list('product_id', flat=True) book_instances = Book.objects.filter(pk__in=fl_products) self.fields['front_products'].initial = book_instances if instance and instance.exclude_products.exists(): product_model = instance.exclude_products.first().product_model if product_model == "Book": Book = load_model(app_label="book_rental", model_name="Book") fl_products = instance.exclude_products.values_list('product_id', flat=True) book_instances = Book.objects.filter(pk__in=fl_products) self.fields['exclude_front_products'].initial = book_instances
def get_front_products(self): products = [] if self.products.exists(): product_model = self.products.first().product_model product_ids = self.products.values_list('product_id', flat=True) if product_model == "Book": Book = load_model(app_label="book_rental", model_name="Book") products = Book.objects.filter(pk__in=product_ids) product_list = [ '<a class="front_product_class" href="%s">%s</a>' % (p.get_detail_link(object_id=p.pk), str(p)) for p in products ] return ','.join(product_list)
def get_front_list(self): FrontList = load_model(app_label="ecommerce", model_name="FrontList") front_list_objects = FrontList.objects.filter(palette_id=self.pk) return front_list_objects
def get_product_instance(self): if self.product_model == "Book": Book = load_model(app_label="book_rental", model_name="Book") return Book.objects.get(pk=self.product_id)
def get_product(self): if self.product_model == "Book": Book = load_model(app_label="book_rental", model_name="Book") book_objects = Book.objects.filter(code=self.product_code) if book_objects.exists(): return book_objects.first()
def handle_upload(self): Book = load_model(app_label="book_rental", model_name="Book") Inventory = load_model(app_label="inventory", model_name="Inventory") self.data = self.data[1:] for row in self.data: try: with transaction.atomic(): index = 0 code = row[index].strip() if row[index] else None index += 1 warehouse_code = row[index] if row[index] else None index += 1 product_code = row[index] if row[index] else None index += 1 is_new = 1 if row[index] else 0 index += 1 printing_type = row[index] if row[index] else None index += 1 current_stock = row[index] if row[index] else 0 index += 1 new_stock = row[index] if row[index] else 0 index += 1 available_for_sale = row[index] if row[index] else 0 index += 1 available_for_rent = row[index] if row[index] else 0 index += 1 available_for_buy = row[index] if row[index] else 0 index += 1 supplier_name = row[index] index += 1 address1 = row[index] index += 1 address2 = row[index] index += 1 address3 = row[index] index += 1 address4 = row[index] index += 1 phone1 = row[index] index += 1 phone2 = row[index] index += 1 note = row[index] if any([ not item for item in [ warehouse_code, product_code, printing_type ] ]): ErrorLog.log(url='', stacktrace='Book Upload missing data: %s. Skipping...' % str(row), context=Inventory.__name__) continue if not int(new_stock) or int(new_stock) < 0: ErrorLog.log(url='', stacktrace='New stock must be greater than 0. Provided: %s. Skipping...' % str(new_stock), context=Inventory.__name__) warehouse_object = None warehouse_objects = Warehouse.objects.filter(code=str(warehouse_code)) if warehouse_objects.exists(): warehouse_object = warehouse_objects.first() else: ErrorLog.log(url='', stacktrace='Missing warehouse in inventory upload. data: %s. Skipping...' % str(warehouse_code), context=Inventory.__name__) continue book_object = None book_objects = Book.objects.filter(code=str(product_code)) if book_objects.exists(): book_object = book_objects.first() else: ErrorLog.log(url='', stacktrace='Missing book in inventory upload. data: %s. Skipping...' % str(product_code), context=Inventory.__name__) continue try: current_stock = int(current_stock) except: ErrorLog.log(url='', stacktrace='Invalid current_stock. Number expected. data: %s. Skipping...' % str(current_stock), context=Inventory.__name__) continue try: new_stock = int(new_stock) except: ErrorLog.log(url='', stacktrace='Invalid new_stock. Number expected. data: %s. Skipping...' % str( new_stock), context=Inventory.__name__) continue try: is_new = int(is_new) except: ErrorLog.log(url='', stacktrace='Invalid Is New. 1 or 0 expected. data: %s. Skipping...' % str(is_new), context=Inventory.__name__) continue if is_new == 1: is_new = True elif is_new == 0: is_new = False try: available_for_sale = int(available_for_sale) except: ErrorLog.log(url='', stacktrace='Invalid available_for_sale. 1 or 0 expected. data: %s. Skipping...' % str( available_for_sale), context=Inventory.__name__) continue try: available_for_rent = int(available_for_rent) except: ErrorLog.log(url='', stacktrace='Invalid available_for_rent. 1 or 0 expected. data: %s. Skipping...' % str( available_for_rent), context=Inventory.__name__) continue try: available_for_buy = int(available_for_buy) except: ErrorLog.log(url='', stacktrace='Invalid available_for_buy. 1 or 0 expected. data: %s. Skipping...' % str( available_for_buy), context=Inventory.__name__) continue if available_for_sale == 1: available_for_sale = True elif available_for_sale == 0: available_for_sale = False if available_for_rent == 1: available_for_rent = True elif available_for_rent == 0: available_for_rent = False if available_for_buy == 1: available_for_buy = True elif available_for_buy == 0: available_for_buy = False if printing_type not in [ 'COL', 'ORI', 'ECO' ]: ErrorLog.log(url='', stacktrace='Printing type must be in COL, ORI, ECO. data: %s. Skipping...' % str( printing_type), context=Inventory.__name__) continue if code: inventory_objects = Inventory.objects.filter(code=code) if inventory_objects.exists(): inventory_object = inventory_objects.first() else: ErrorLog.log(url='', stacktrace='Invalid inventory code. data: %s. Skipping...' % str(code), context=Inventory.__name__) continue else: inventory_objects = Inventory.objects.filter(warehouse_id=warehouse_object.pk, product_id=book_object.pk, product_model=book_object.__class__.__name__, is_new=is_new, print_type=printing_type, available_for_rent=available_for_rent) if inventory_objects.exists(): inventory_object = inventory_objects.first() else: inventory_object = Inventory() stock = inventory_object.stock + new_stock inventory_object.product_id = book_object.pk inventory_object.product_model = book_object.__class__.__name__ inventory_object.warehouse_id = warehouse_object.pk inventory_object.stock = stock inventory_object.available_for_sale = available_for_sale inventory_object.available_for_rent = available_for_rent inventory_object.available_for_buy = available_for_buy inventory_object.is_new = is_new inventory_object.print_type = printing_type inventory_object.comment = note inventory_object.save() supplier_object = None if supplier_name: supplier_objects = ProductSupplier.objects.filter(name=str(supplier_name)) if supplier_objects.exists(): supplier_object = supplier_objects.first() else: supplier_object = ProductSupplier() supplier_object.name = str(supplier_name) supplier_object.address_line1 = str(address1) supplier_object.address_line2 = str(address2) supplier_object.address_line3 = str(address3) supplier_object.address_line4 = str(address4) supplier_object.phone_number1 = str(phone1) supplier_object.phone_number2 = str(phone2) supplier_object.notes = str(note) supplier_object.save() inventory_transaction = InventoryTransaction() inventory_transaction.transaction_type = InventoryTXNType.STOCK_IN.value inventory_transaction.qty = new_stock if supplier_object: inventory_object.supplier_id = supplier_object.pk inventory_transaction.warehouse_id = warehouse_object.pk inventory_transaction.product_id = book_object.pk inventory_transaction.product_model = book_object.__class__.__name__ inventory_transaction.is_new = is_new inventory_transaction.print_type = printing_type inventory_transaction.save() except Exception as exp: ErrorLog.log(url='', stacktrace='Exception Occured. Message: %s. Skipping...' % str(exp), context=Inventory.__name__) return True
def handle_upload(self): BookPublisher = load_model(app_label="book_rental", model_name="BookPublisher") self.data = self.data[1:] for row in self.data: with transaction.atomic(): try: index = 0 code = row[index].strip() if row[index] else None index += 1 publisher_name = row[index] if row[index] else None index += 1 publisher_name_2 = row[index] if row[index] else None index += 1 publisher_description = row[index] if row[index] else None index += 1 publisher_description_2 = row[index] if row[index] else None index += 1 show_2 = row[index] if row[index] else None index += 1 publisher_image = row[index] if row[index] else None index += 1 emails = row[index] if row[index] else None index += 1 phones = row[index] if row[index] else None if not publisher_name: ErrorLog.log(url='', stacktrace='Publisher name must be given', context=BookPublisher.__name__) continue if not publisher_description: ErrorLog.log( url='', stacktrace='Publisher description must be given', context=BookPublisher.__name__) continue try: if not show_2: show_2 = 0 show_2 = int(show_2) if show_2 == 1: if not publisher_name_2 or not publisher_description_2: ErrorLog.log( url='', stacktrace= 'Publisher name bn and Publisher description bn missing. Data: %s skipping...' % row, context=BookPublisher.__name__) continue except Exception as exp: ErrorLog.log( url='', stacktrace= 'Show BN must be number. Given %s. skipping...' % show_2, context=BookPublisher.__name__) continue if code: publisher_objects = BookPublisher.objects.filter( code=code) if publisher_objects.exists(): publisher_object = publisher_objects.first() else: ErrorLog.log( url='', stacktrace= 'Publisher with code %s not found. skipping...' % code, context=BookPublisher.__name__) continue else: publisher_object = BookPublisher() publisher_object.name = str(publisher_name) publisher_object.description = str(publisher_description) if publisher_name_2: publisher_object.name_2 = publisher_name_2 if publisher_description_2: publisher_object.description_2 = publisher_description_2 publisher_object.show_2 = show_2 if publisher_image: image_full_path = os.path.join( settings.MEDIA_PUBLISHER_PATH, publisher_image) if os.path.exists(image_full_path): image_name_relative_media = get_relative_path_to_media( image_full_path) publisher_object.image.name = image_name_relative_media else: ErrorLog.log( url='', stacktrace= 'Publisher image %s not found. skipping...' % publisher_image, context=BookPublisher.__name__) continue publisher_object.save() publisher_object.emails.clear() if emails: for email in emails: email_objects = Email.objects.filter(email=email) if email_objects.exists(): email_object = email_objects.first() else: email_object = Email(email=email) email_object.save() if not publisher_object.emails.filter( pk=email_object.pk).exists(): publisher_object.emails.add(email_object) publisher_object.phones.clear() if phones: for phone in phones: phone_objects = Phone.objects.filter(number=phone) if phone_objects.exists(): phone_object = phone_objects.first() else: phone_object = Phone(number=phone) phone_object.save() if not publisher_object.phones.filter( pk=phone_object.pk).exists(): publisher_object.phones.add(phone_object) except Exception as exp: ErrorLog.log(url='', stacktrace='Exception Occured. Message: %s' % str(exp), context=BookPublisher.__name__)
def handle_upload(self): Author = load_model(app_label="book_rental", model_name="Author") self.data = self.data[1:] for row in self.data: try: with transaction.atomic(): index = 0 code = row[index].strip() if row[index] else None index += 1 author_name = row[index] if row[index] else None index += 1 author_name_2 = row[index] if row[index] else None index += 1 author_description = row[index] if row[index] else None index += 1 author_description_2 = row[index] if row[index] else None index += 1 show_2 = row[index] if row[index] else None index += 1 author_image = row[index] if row[index] else None index += 1 date_of_birth = row[index] if row[index] else None index += 1 emails = str(row[index]) if row[index] else None if not author_name: ErrorLog.log(url='', stacktrace='Author Name must be given', context='Author') continue if not author_description: ErrorLog.log( url='', stacktrace='Author description must be given', context='Author') continue try: if not show_2: show_2 = 0 show_2 = int(show_2) if show_2 == 1: if not author_name_2 or not author_description_2: ErrorLog.log( url='', stacktrace= 'Author name bn and author description bn missing. Data: %s skipping...' % row, context='Author') continue except Exception as exp: ErrorLog.log( url='', stacktrace= 'Show 2 must be number. Given %s. skipping...' % show_2, context='Author') continue if emails: emails = emails.split(',') else: emails = [] index += 1 phones = str(row[index]) if row[index] else None if phones: phones = phones.split(',') else: phones = [] index += 1 author_object = None if code: author_objects = Author.objects.filter(code=code) if author_objects.exists(): author_object = author_objects.first() else: ErrorLog.log( url='', stacktrace= 'Author with code %s not found. skipping...' % code, context='Author') continue if not author_object: author_object = Author() if author_name_2: author_object.name_2 = author_name_2 if author_description_2: author_object.description_2 = author_description_2 author_object.show_2 = show_2 if author_image: image_full_path = os.path.join( settings.MEDIA_AUTHOR_PATH, author_image) if os.path.exists(image_full_path): image_name_relative_media = get_relative_path_to_media( image_full_path) author_object.image.name = image_name_relative_media else: ErrorLog.log( url='', stacktrace= 'Author image %s not found. skipping...' % author_image, context='Author') continue if date_of_birth: try: date_of_birth = datetime.strptime( date_of_birth, "%d/%m/%Y") except Exception as exp: print(str(exp)) date_of_birth = None ErrorLog.log( url='', stacktrace= 'Author date of birth format incorrect. Correct format: dd/mm/yyyy. skipping...', context='Author') continue if date_of_birth: author_object.date_of_birth = date_of_birth author_object.name = str(author_name) author_object.description = str(author_description) author_object.save() author_object.emails.clear() if emails: for email in emails: email_objects = Email.objects.filter(email=email) if email_objects.exists(): email_object = email_objects.first() else: email_object = Email(email=email) email_object.save() if not author_object.emails.filter( pk=email_object.pk).exists(): author_object.emails.add(email_object) author_object.phones.clear() if phones: for phone in phones: phone = phone.replace("'", "") phone_objects = Phone.objects.filter(number=phone) if phone_objects.exists(): phone_object = phone_objects.first() else: phone_object = Phone(number=phone) phone_object.save() if not author_object.phones.filter( pk=phone_object.pk).exists(): author_object.phones.add(phone_object) except Exception as exp: ErrorLog.log( url='', stacktrace= 'Exception Occured. Exception message: %s. Skipping...Date: %s' % (str(exp), row), context='Author') return True
def handle_upload(self): ProductCategory = load_model(app_label="ecommerce", model_name="ProductCategory") try: for row in self.data: with transaction.atomic(): index = 0 code = row[index].strip() if row[index] else None index += 1 category_name = row[index] if row[index] else None index += 1 category_name_2 = row[index] if row[index] else None index += 1 show_name_2 = row[index] if row[index] else None index += 1 parent_name = row[index] if row[index] else None if not category_name: ErrorLog.log( url='', stacktrace='Category name is missing. Data %s' % str(row), context=ProductCategory.__name__) continue try: if not show_name_2: show_name_2 = 0 show_name_2 = int(show_name_2) if show_name_2 == 1: if not category_name_2: ErrorLog.log( url='', stacktrace= 'Category name 2 missing. Data: %s skipping...' % row, context=ProductCategory.__name__) continue except Exception as exp: ErrorLog.log( url='', stacktrace= 'Show name 2 must be number. Given %s. skipping...' % show_name_2, context=ProductCategory.__name__) continue if code: category_objects = ProductCategory.objects.filter( code=code) if category_objects.exists(): category_object = category_objects.first() if parent_name: parent_categories = ProductCategory.objects.filter( name=parent_name) if parent_categories.exists(): parent_category = parent_categories.first() else: ErrorLog.log( url='', stacktrace= "Parent category doesn't exist. Skipping... Data: %s" % str(row), context=ProductCategory.__name__) continue category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.parent_id = parent_category.pk category_object.save() else: category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.parent_id = None category_object.save() else: ErrorLog.log( url='', stacktrace= "Invalid Category Code. Skipping... Data: %s" % str(row), context=ProductCategory.__name__) continue else: book_category_objects = ProductCategory.objects.filter( name=category_name) if book_category_objects.exists(): category_object = book_category_objects.first() if parent_name: parent_categories = ProductCategory.objects.filter( name=parent_name) if parent_categories.exists(): parent_category = parent_categories.first() else: ErrorLog.log( url='', stacktrace= "Parent category doesn't exist. Skipping... Data: %s" % str(row), context=ProductCategory.__name__) continue category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.parent_id = parent_category.pk category_object.save() else: category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.save() else: category_object = ProductCategory() if parent_name: parent_categories = ProductCategory.objects.filter( name=parent_name) if parent_categories.exists(): parent_category = parent_categories.first() else: ErrorLog.log( url='', stacktrace= "Parent category doesn't exist. Skipping... Data: %s" % str(row), context=ProductCategory.__name__) continue category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.parent_id = parent_category.pk category_object.save() else: category_object.name = category_name if show_name_2 == 1: category_object.name_2 = category_name_2 category_object.show_name_2 = show_name_2 category_object.save() return True except Exception as exp: ErrorLog.log(url='', stacktrace="Exception Occured. Message: %s" % str(exp), context=ProductCategory.__name__) return False
def handle_upload(self): Warehouse = load_model(app_label="ecommerce", model_name="Warehouse") self.data = self.data[1:] for row in self.data: try: with transaction.atomic(): index = 0 code = row[index].strip() if row[index] else None index += 1 name = row[index] if row[index] else None index += 1 description = row[index] if row[index] else None index += 1 contact_name = row[index] if row[index] else None index += 1 contact_no = row[index] if row[index] else None if not name: ErrorLog.log( url='', stacktrace= 'Warehouse name must be given. skipping...', context=Warehouse.__name__) continue if not description: ErrorLog.log( url='', stacktrace= 'Warehouse description must be given. skipping...', context=Warehouse.__name__) continue if code: wh_objects = Warehouse.objects.filter(code=code) if wh_objects.exists(): wh_object = wh_objects.first() else: ErrorLog.log( url='', stacktrace='Invalid code given. Data %s' % str(row), context=Warehouse.__name__) continue else: wh_objects = Warehouse.objects.filter(name=str(name)) if wh_objects.exists(): wh_object = wh_objects.first() else: wh_object = Warehouse() wh_object.name = str(name) wh_object.description = str(description) if contact_no: phones = Phone.objects.filter(number=contact_no) if phones.exists(): phone_object = phones.first() else: phone_object = Phone(number=contact_no) phone_object.save() wh_object.contact_id = phone_object.pk if contact_name: wh_object.warehouse_manager = str(contact_name) wh_object.save() except Exception as exp: ErrorLog.log(url='', stacktrace='Exception Occured. Message: %s' % str(exp), context=Warehouse.__name__) return True
def handle_upload(self): Book = load_model(app_label="book_rental", model_name="Book") self.data = self.data[1:] for row in self.data: with transaction.atomic(): try: index = 0 code = row[index] index += 1 book_title = str(row[index]) if row[index] else None index += 1 book_title_2 = str(row[index]) if row[index] else None index += 1 sub_title = str(row[index]) if row[index] else None index += 1 sub_title_2 = str(row[index]) if row[index] else None index += 1 isbn = str(row[index]).replace("'", "") if row[index] else None index += 1 isbn13 = str(row[index]).replace( "'", "") if row[index] else None index += 1 description = str(row[index]) if row[index] else None index += 1 description_2 = str(row[index]) if row[index] else None index += 1 show_2 = str(row[index]) if row[index] else None index += 1 category_codes = str(row[index]) if row[index] else None index += 1 edition = str(row[index]) if row[index] else None index += 1 total_page = str(row[index]) if row[index] else None index += 1 publisher_code = str(row[index]) if row[index] else None index += 1 published_date = row[index] index += 1 cover_photo = str(row[index]) if row[index] else None index += 1 language = str(row[index]) if row[index] else None index += 1 keywords = str(row[index]) if row[index] else None if keywords: keywords = keywords.split(',') authors = [] index += 1 author_codes = row[index] authors = author_codes.split(',') authors = [str(acode) for acode in authors] index += 1 sale_available = row[index] index += 1 rent_available = row[index] # Validate data if any([ not item for item in [ book_title, sub_title, description, edition, total_page, publisher_code, published_date, language, keywords, authors ] ]): ErrorLog.log( url='', stacktrace= 'Book Upload missing data: %s. Skipping...' % str(row), context=Book.__name__) continue if isbn and len(isbn) != 10: ErrorLog.log( url='', stacktrace= 'ISBN number must be 10 digit long. Skipping... Data %s' % str(row), context=Book.__name__) continue if isbn13 and len(isbn13) != 13: ErrorLog.log( url='', stacktrace= 'ISBN13 number must be 13 digit long. Skipping... Data %s' % str(row), context=Book.__name__) continue try: if not show_2: show_2 = 0 show_2 = int(show_2) if show_2 == 1: if not book_title_2 or not description_2: ErrorLog.log( url='', stacktrace= 'Book Title, Description is mandatory. Data: %s skipping...' % row, context=Book.__name__) continue except Exception as exp: ErrorLog.log( url='', stacktrace= 'show_2 must be number. Given %s. skipping...' % show_2, context=Book.__name__) continue try: int(float(edition)) except Exception as exp: ErrorLog.log( url='', stacktrace= 'Invalid edition. Must be number. Skipping... data: %s' % str(row), context=Book.__name__) continue try: total_page = int(float(total_page)) except Exception as exp: ErrorLog.log( url='', stacktrace= 'Total page must be number. Skipping... data: %s' % str(row), context=Book.__name__) continue book_languages = BookLanguage.objects.filter( short_name=language) if not book_languages.exists(): ErrorLog.log( url='', stacktrace= 'Invalid language code given. Skipping... Data: %s' % str(row), context=Book.__name__) continue else: language = book_languages.first().pk try: published_date = published_date.date() except Exception as exp: ErrorLog.log( url='', stacktrace= 'Invalid published date format given. Skipping... Data: %s' % str(row), context=Book.__name__) continue cat_ids = [] if category_codes: category_codes = category_codes.split(',') # Create Book Object cat_not_found = False category_objects = ProductCategory.objects.filter( code__in=category_codes).values('id', 'code') for cat_object in category_objects: if not cat_object['code'] in category_codes: cat_not_found = True break else: cat_ids += [cat_object['id']] if cat_not_found: ErrorLog.log( url='', stacktrace= 'Invalid category code given. Skipping... Data: %s' % str(row), context=Book.__name__) continue else: ErrorLog.log( url='', stacktrace='No Category found. Skipping... Data: %s' % str(row), context=Book.__name__) continue publisher_objects = BookPublisher.objects.filter( code=str(publisher_code)) if not publisher_objects.exists(): ErrorLog.log( url='', stacktrace='Invalid publisher code given. Data: %s' % str(row), context=Book.__name__) continue publisher_id = publisher_objects.first().pk author_object_list = [] if authors: athr_not_found = False for author_code in authors: athr_objects = Author.objects.filter( code=str(author_code.strip())) if not athr_objects.exists(): ErrorLog.log( url='', stacktrace= 'Invalid author code given. Data: %s' % str(row), context=Book.__name__) athr_not_found = True break author_object_list += [athr_objects.first()] if athr_not_found: ErrorLog.log( url='', stacktrace='Invalid author code given. Data: %s' % str(row), context=Book.__name__) continue else: ErrorLog.log(url='', stacktrace='NO author found. Data: %s' % str(row), context=Book.__name__) continue try: sale_available = int(sale_available) except: sale_available = False if sale_available: sale_available = True try: rent_available = int(rent_available) except: rent_available = False if rent_available: rent_available = True keyword_object_list = [] for keyword in keywords: keyword_instances = TagKeyword.objects.filter( name=keyword) if keyword_instances.exists(): keyword_instance = keyword_instances.first() else: keyword_instance = TagKeyword() keyword_instance.name = keyword keyword_instance.save() keyword_object_list += [keyword_instance] # Check if code given. If given then check if book_edition exists else set book_rental # instance to None if code: book_objects = Book.objects.filter(code=code) if book_objects.exists(): book_object = book_objects.first() else: ErrorLog.log( url='', stacktrace= 'Invalid code given for book. Skipping.... Data %s' % str(code), context=Book.__name__) continue else: book_objects = Book.objects.filter(title=book_title, isbn=str(isbn)) if book_objects.exists(): book_object = book_objects.first() else: book_object = Book() book_object.title = str(book_title) book_object.subtitle = str(sub_title) book_object.description = str(description) if show_2: book_object.title_2 = book_title_2 book_object.subtitle_2 = sub_title_2 book_object.description_2 = description_2 book_object.show_2 = True if show_2 else False book_object.sale_available = sale_available book_object.rent_available = rent_available book_object.publish_date = published_date if isbn: book_object.isbn = str(isbn) if isbn13: book_object.isbn13 = str(isbn13) book_object.edition = edition book_object.publisher_id = publisher_id book_object.language_id = language book_object.page_count = total_page book_object.save() # Product Image if cover_photo: book_object.images.all().delete() book_object.images.clear() image_full_path = os.path.join( settings.MEDIA_BOOK_PATH, cover_photo) if os.path.exists(image_full_path): image_name_relative_media = get_relative_path_to_media( image_full_path) product_image = ProductImage() product_image.image.name = image_name_relative_media product_image.save() book_object.images.add(product_image) else: ErrorLog.log( url='', stacktrace='Product image %s not found...' % cover_photo, context=Book.__name__) book_object.tags.clear() book_object.tags.add(*keyword_object_list) book_object.categories.clear() cat_object_list = [] for cat_id in cat_ids: cat_obj = ProductCategory.objects.get(pk=cat_id) cat_object_list += [cat_obj] book_object.categories.add(*cat_object_list) book_object.authors.clear() book_object.authors.add(*author_object_list) except Exception as exp: ErrorLog.log(url='', stacktrace='Exception Occured. Message: %s' % str(exp), context=Book.__name__) return True
def is_valid(self): product = self.data["product"] is_new = self.data["is_new"] print_type = self.data["print_type"] base_price = self.data["base_price"] market_price = self.data["market_price"] sale_price = self.data["sale_price"] currency = self.data["currency"] if any([not base_price, not market_price, not sale_price]): return False Book = load_model(app_label="book_rental", model_name="Book") product = Book.objects.get(pk=int(product)) currency = Currency.objects.get(pk=int(currency)) is_new = 0 if not is_new else 1 is_new = bool(is_new) try: base_price = Decimal(base_price) except: return False try: market_price = Decimal(market_price) except: return False try: sale_price = Decimal(sale_price) except: return False special_price = self.data.get("special_price") offer_price_p = self.data.get("offer_price_p") offer_price_v = self.data.get("offer_price_v") offer_date_start = self.data.get("offer_start_date") offer_date_end = self.data.get("offer_end_date") special_price = 0 if not special_price else 1 special_price = bool(special_price) if special_price: if any([ not offer_price_p, not offer_price_v, not offer_date_start, not offer_date_end ]): return False try: offer_price_p = Decimal(offer_price_p) except: return False try: offer_price_v = Decimal(offer_price_v) except: return False try: offer_date_start = datetime.strptime(offer_date_start, "%m/%d/%Y") offer_date_end = datetime.strptime(offer_date_end, "%m/%d/%Y") except: return False is_rent = self.data.get("is_rent") initial_payable_rent_price = self.data.get( "initial_payable_rent_price") custom_rent_plan_available = self.data.get( "custom_rent_plan_available") is_rent = 0 if not is_rent else 1 is_rent = bool(is_rent) custom_rent_plan_available = 0 if not custom_rent_plan_available else 1 custom_rent_plan_available = bool(custom_rent_plan_available) if is_rent: if not initial_payable_rent_price: return False initial_payable_rent_price = Decimal(initial_payable_rent_price) self.cleaned_data = {} self.cleaned_data["product"] = product self.cleaned_data["is_new"] = is_new self.cleaned_data["print_type"] = print_type self.cleaned_data["base_price"] = base_price self.cleaned_data["market_price"] = market_price self.cleaned_data["sale_price"] = sale_price self.cleaned_data["currency"] = currency self.cleaned_data["special_price"] = special_price if special_price: self.cleaned_data["offer_price_p"] = offer_price_p self.cleaned_data["offer_price_v"] = offer_price_v self.cleaned_data[ "offer_date_start"] = Clock.convert_datetime_to_utc_timestamp( offer_date_start) self.cleaned_data[ "offer_date_end"] = Clock.convert_datetime_to_utc_timestamp( offer_date_end) self.cleaned_data["is_rent"] = is_rent if is_rent: self.cleaned_data[ "initial_payable_rent_price"] = initial_payable_rent_price self.cleaned_data[ "custom_rent_plan_available"] = custom_rent_plan_available return True
from django.db import models from django.template.defaultfilters import slugify from ecommerce.models.sales.category import ProductCategory from ecommerce.models.sales.keyword import TagKeyword from ecommerce.models.sales.product_images import ProductImage from generics.libs.loader.loader import load_model from generics.models.base_entity import BaseEntity from engine.clock.Clock import Clock from ecommerce.models.sales.rent_plan_relation import RentPlanRelation PriceMatrix = load_model(app_label="ecommerce", model_name="PriceMatrix") class Product(BaseEntity): title = models.CharField(max_length=500) title_2 = models.CharField(max_length=500) subtitle = models.CharField(max_length=500) subtitle_2 = models.CharField(max_length=500) description = models.TextField(blank=True) description_2 = models.TextField(blank=True) show_2 = models.BooleanField(default=False) categories = models.ManyToManyField(ProductCategory) sale_available = models.BooleanField(default=True) rent_available = models.BooleanField(default=False) tags = models.ManyToManyField(TagKeyword) mfg_date = models.IntegerField(default=0) expire_date = models.IntegerField(default=0) slug = models.SlugField() images = models.ManyToManyField(ProductImage) rating = models.DecimalField(max_digits=6, decimal_places=2, default=0.0)
def handle_sale_price_upload(self): PriceMatrix = load_model(app_label="ecommerce", model_name="PriceMatrix") for row in self.data: with transaction.atomic(): index = 0 product_code = row[index] index += 1 is_new = row[index] index += 1 print_type = row[index] index += 1 market_price = row[index] index += 1 base_price = row[index] index += 1 sale_price = row[index] index += 1 initial_rent_payable_price = row[index] index += 1 is_special_sale = row[index] index += 1 special_sale_rate = row[index] index += 1 offer_start_date = row[index] index += 1 offer_end_date = row[index] index += 1 currency = row[index] if any([ not item for item in [ product_code, is_new, print_type, market_price, base_price, currency ] ]): error_log = ErrorLog() error_log.url = '' error_log.stacktrace = 'Missing data.' error_log.save() continue product_objects = Book.objects.filter(code=product_code) if product_objects.exists(): product_object = product_objects.first() else: ErrorLog.log( url='', stacktrace= 'Invalid product code supplied. Skipping... Data %s' % product_code) continue try: is_new = int(is_new) if is_new != 1 and is_new != 0: ErrorLog.log( url='', stacktrace= 'Invalid is_new supplied. 1 or 0 expected. Skipping... Data %s' % row) continue if is_new == 1: is_new = True else: is_new = False except: ErrorLog.log( url='', stacktrace= 'Invalid is_new supplied. 1 or 0 expected. Skipping... Data %s' % row) continue if not print_type in settings.SUPPORTED_PRINTING_TYPES: ErrorLog.log( url='', stacktrace='printing type must be in %s. Skipping...' % settings.SUPPORTED_PRINTING_TYPES) continue try: market_price = Decimal(market_price) except: ErrorLog.log( url='', stacktrace= 'Invalid market_price value. Decimal expected. Given: %s' % row) continue try: base_price = Decimal(base_price) except: ErrorLog.log( url='', stacktrace= 'Invalid base_price value. Decimal expected. Given: %s' % row) continue try: if sale_price: sale_price = Decimal(sale_price) except: ErrorLog.log( url='', stacktrace= 'Invalid sale_price value. Decimal expected. Given: %s' % row) continue try: if initial_rent_payable_price: initial_rent_payable_price = Decimal( initial_rent_payable_price) except: ErrorLog.log( url='', stacktrace= 'Invalid initial_rent_payable_price value. Decimal expected. Given: %s' % row) continue try: is_special_sale = int( is_special_sale) if is_special_sale else 0 if is_special_sale != 1 and is_special_sale != 0: ErrorLog.log( url='', stacktrace= 'Invalid is_special_sale supplied. 1 or 0 expected. Skipping... Data %s' % row) continue if is_special_sale == 1: is_special_sale = True else: is_special_sale = False except: ErrorLog.log( url='', stacktrace= 'Invalid is_special_sale supplied. 1 or 0 expected. Skipping... Data %s' % row) continue try: special_sale_rate = Decimal(special_sale_rate) except: if is_special_sale: ErrorLog.log( url='', stacktrace= 'Invalid special_sale_rate value. Decimal expected. Given: %s' % row) continue try: offer_start_date = offer_start_date except: if is_special_sale: ErrorLog.log( url='', stacktrace= 'Invalid offer_start_date value. Skipping... Expected format: dd/mm/yyyy. Given' % row) continue try: offer_end_date = offer_end_date except: if is_special_sale: ErrorLog.log( url='', stacktrace= 'Invalid offer_end_date value. Skipping... Expected format: dd/mm/yyyy. Given' % row) continue currency_objects = Currency.objects.filter(short_name=currency) if currency_objects.exists(): currency_object = currency_objects.first() else: ErrorLog.log( url='', stacktrace= 'Invalid currency code value. Skipping...Data: ' % row) continue price_objects = PriceMatrix.objects.filter( product_model='Book', product_code=product_code, is_new=is_new, print_type=print_type) if price_objects.exists(): price_object = price_objects.first() else: price_object = PriceMatrix(product_model='Book', product_code=product_code, is_new=is_new, print_type=print_type) price_object.is_rent = False price_object.market_price = market_price price_object.base_price = base_price price_object.currency_id = currency_object.pk if is_special_sale: offer_start_ts = Clock.convert_datetime_to_timestamp( offer_start_date) offer_end_ts = Clock.convert_datetime_to_timestamp( offer_end_date) price_object.offer_date_start = offer_start_ts price_object.offer_date_end = offer_end_ts price_object.special_price = is_special_sale if is_special_sale: price_object.offer_price_p = float(special_sale_rate) / 100 price_object.offer_price_v = float(base_price) * ( float(special_sale_rate) / 100) else: price_object.offer_price_p = 1.0 price_object.offer_price_v = base_price if sale_price: price_object.sale_price = sale_price if initial_payable_rent_price: price_object.initial_payable_rent_price = initial_payable_rent_price price_object.save()
def get_product(self): Book = load_model(app_label="book_rental", model_name="Book") if self.product_model == Book.__name__: return Book.objects.get(pk=self.product_id)