def get_context_data(self, **kwargs): context = super(AdminListContextMixin, self).get_context_data(**kwargs) queryset = self.get_queryset() total_count = queryset.count() paginator = Paginator(queryset, self.paginate_by) page = self.request.GET.get('page') try: object_list = paginator.page(page) except PageNotAnInteger: object_list = paginator.page(1) except EmptyPage: object_list = paginator.page(paginator.num_pages) self.object_list = object_list context["page_title"] = self.get_page_title() context["show_upload"] = self.show_upload() context["show_download"] = self.show_download() context["show_download_template"] = self.show_download_template() context["show_create"] = self.show_create() context["show_edit"] = self.show_edit() context["show_delete"] = self.show_delete() context["show_activate"] = self.show_activate() context["show_deactivate"] = self.show_deactivate() context["show_approve"] = self.show_approve() context["show_reject"] = self.show_reject() context["show_upload_as"] = self.show_upload_as() context["show_download_as"] = self.show_download_as() context["show_download_template_as"] = self.show_download_template_as() context["show_create_as"] = self.show_create_as() context["show_edit_as"] = self.show_edit_as() context["show_delete_as"] = self.show_delete_as() context["show_activate_as"] = self.show_activate_as() context["show_deactivate_as"] = self.show_deactivate_as() context["show_approve_as"] = self.show_approve_as() context["show_reject_as"] = self.show_reject_as() context["search_by_options"] = self.get_search_by_options() context["search_datefields"] = self.model.get_datefields() context["advanced_search_options"] = self.get_advanced_search_options() context["search_filters"] = self.collect_search_filters(request=self.request) context["search_param_url"] = self.collect_search_params(request=self.request) context["search_param_context"] = self.get_search_param_context(request=self.request) context["upload_link"] = self.get_upload_link() context["download_link"] = self.get_download_link() context["download_template_link"] = self.get_download_template_link() context["create_link"] = self.get_create_link() context["edit_link_name"] = self.get_edit_link_name() context["delete_link"] = self.get_delete_link() context["activate_link"] = self.get_activate_link() context["deactivate_link"] = self.get_deactivate_link() context["approve_link"] = self.model.get_approve_link() context["reject_link"] = self.model.get_reject_link() context["upload_redirect"] = self.get_upload_redirect_url(request=self.request) context["breadcumb"] = self.get_breadcumb(request=self.request) left_menu = self.get_left_menu_items() left_menu = sorted(left_menu.items()) context["left_menu_items"] = left_menu context["headers"] = self.get_table_headers() context["table_data"] = self.prepare_table_data(queryset=object_list) context["table_column_count"] = len(context["headers"]) context["total_count"] = total_count context["ttab"] = self.get_ttab_name() context["ltab"] = self.get_ltab_name() extra_context = self.get_extra_context(request=self.request, queryset=object_list) for key, item in extra_context.items(): if key not in context.keys(): context[key] = item else: ErrorLog.log(url='', stacktrace='%s cannot be used as key as it is already in use in the parent context', context='%s' % self.model.__name__) # view_actions = self.get_view_actions() # print(view_actions) # for key, item in view_actions.items(): # if key not in context.keys(): # context[key] = item # else: # ErrorLog.log(url='', # stacktrace='%s cannot be used as key as it is already in use in the parent context', # context='%s' % self.model.__name__) # print(context.get("show_create")) return context
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): 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): 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): 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 handle_rent_price_upload(self): for row in self.data: with transaction.atomic(): index = 0 rent_code = row[index] index += 1 product_code = row[index] index += 1 is_new = row[index] index += 1 print_type = row[index] index += 1 price_in_percentage = row[index] index += 1 is_special_rent = row[index] index += 1 special_rent_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 [ rent_code, product_code, is_new, print_type, price_in_percentage, 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: price_in_percentage = Decimal(price_in_percentage) except: ErrorLog.log( url='', stacktrace= 'Invalid price_in_percentage value. Decimal expected. Given: %s' % row) continue try: is_special_rent = int(is_special_rent) if is_special_rent != 1 and is_special_rent != 0: ErrorLog.log( url='', stacktrace= 'Invalid is_special_rent supplied. 1 or 0 expected. Skipping... Data %s' % row) continue if is_special_rent == 1: is_special_rent = True else: is_special_rent = False except: ErrorLog.log( url='', stacktrace= 'Invalid is_special_rent supplied. 1 or 0 expected. Skipping... Data %s' % row) continue try: special_rent_rate = Decimal(special_rent_rate) except: if is_special_rent: ErrorLog.log( url='', stacktrace= 'Invalid special_rent_rate value. Decimal expected. Given: %s' % row) continue if is_special_rent: if not offer_start_date or not offer_end_date: ErrorLog.log( url='', stacktrace='Offer dates missing. Skipping...Data: ' % 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: ErrorLog.log( url='', stacktrace= 'No price matrix object exists for this. Skipping...Data: ' % row) continue rent_plan_objects = RentPlan.objects.filter(code=rent_code) if rent_plan_objects.exists(): rent_plan_object = rent_plan_objects.first() else: ErrorLog.log( url='', stacktrace='No rent plan exists. Skipping...Data: ' % row) continue rent_rel_objects = RentPlanRelation.objects.filter( plan_id=rent_plan_object.pk, price_matrix_id=price_object.pk) if rent_rel_objects.exists(): rent_rel_object = rent_rel_objects.first() else: rent_rel_object = RentPlanRelation( plan_id=rent_plan_object.pk, price_matrix_id=price_object.pk) rent_plan_object.rent_rate = price_in_percentage if is_special_rent: rent_rel_object.start_time = Clock.convert_datetime_to_timestamp( offer_start_date) rent_rel_object.end_time = Clock.convert_datetime_to_timestamp( offer_end_date) rent_rel_object.special_rate = float( special_rent_rate) / 100 rent_rel_object.is_special_offer = is_special_rent rent_rel_object.save() print("Done! Proceed to the next...")
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()