def add_item(request): store = Store.objects.get(id=request.BODY['id']) columns = store.columns linked_columns = store.link_columns # Check if all link columns has static data item = checker.check_link_columns(linked_columns, request.BODY['item']) if isinstance(item, HttpResponseBadRequest): return item for key, val in item.iteritems(): if key not in columns: return HttpResponseBadRequest('All columns do not exist.', 'application/json') items = store.inventory if len(items): item_id = int(max(items, key=int)) + 1 else: item_id = 1 items[item_id] = item store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'item': {item_id: item}, 'store': model_to_dict(store)}, safe=False)
def link_columns(request): current_user = request.user current_boss = get_boss(current_user) column = request.POST['column'] link_type = request.POST['link_type'] store = Store.objects.get(id=request.POST['id']) store_inventory = store.inventory if link_type not in store.link_columns: return HttpResponseBadRequest('Link type does not exist.', 'application/json') store.link_columns[link_type] = column.strip() if link_type == 'price' or link_type == 'cost': # Turn all data to float values for item_id, item in store_inventory.iteritems(): current_price = item[column] if current_price.replace('.', '', 1).isdigit(): item[column] = decimal_format(float(current_price), 2, False) else: item[column] = '0.00' elif link_type == 'quantity': # Turn all data to int values for key, item in store_inventory.iteritems(): current_quantity = item[column] if str(current_quantity).isdigit(): item[column] = int(current_quantity) else: item[column] = 0 store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store)}, safe=False)
def delete_item(request): store = Store.objects.get(id=request.POST['id']) item_id = str(request.POST['item_id']) items = store.inventory items.pop(item_id, None) store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store)}, safe=False)
def edit_column(request): current_user = request.user current_boss = get_boss(current_user) store = Store.objects.get(id=request.POST['id']) prev_column_name = request.POST['prev_column_name'] new_column_name = request.POST['new_column_name'] columns = store.columns items = store.inventory linked_columns = store.link_columns if prev_column_name not in columns: return HttpResponseBadRequest('Column name does not exist.', 'application/json') if new_column_name == '': return HttpResponseBadRequest('Column name is an empty string.', 'application/json') # Edit column list store.columns = [w.replace(prev_column_name, new_column_name) for w in columns] # Edit inventory for key, item in items.iteritems(): item[new_column_name] = item.pop(prev_column_name) # Check link columns for key, item in linked_columns.iteritems(): if linked_columns[key] == prev_column_name: linked_columns[key] = new_column_name # Check name link column name_regex = linked_columns['name'] if name_regex: linked_columns['name'] = name_regex.replace('{{' + prev_column_name + '}}', '{{' + new_column_name + '}}') user_settings = current_boss.settings # Check transaction filters store.transaction_filter = [w.replace(prev_column_name, new_column_name) for w in columns] if prev_column_name == store.order_by: store.order_by = new_column_name store.save() user_settings.save() user_types = UserType.objects.filter(boss=current_boss) for user_type in user_types: permission = user_type.permission permission.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'columns': store.columns, 'store': model_to_dict(store)}, safe=False)
def delete_column(request): current_user = request.user current_boss = get_boss(current_user) store = Store.objects.get(id=request.POST['id']) column_name = request.POST['column_name'] columns = store.columns items = store.inventory linked_columns = store.link_columns if column_name == '': return HttpResponseBadRequest('Column name is an empty string.', 'application/json') if column_name not in columns: return HttpResponseBadRequest('Column name does not exist.', 'application/json') # Remove column from list columns.remove(column_name) # Delete column from inventory for key, item in items.iteritems(): item.pop(column_name, None) # Check link columns for key, item in linked_columns.iteritems(): if linked_columns[key] == column_name: linked_columns[key] = False user_settings = current_boss.settings transaction_filter = store.transaction_filter if column_name == store.order_by: store.order_by = "none" # Check transaction filter if column_name in transaction_filter: transaction_filter.remove(column_name) store.save() user_settings.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store)}, safe=False)
def add_picture_column(request): store_id = request.POST['id'] store = Store.objects.get(id=store_id) column_name = request.POST['column_name'].lower().strip() items = store.inventory if column_name == '': return HttpResponseBadRequest('Column name is an empty string.', 'application/json') store.picture_column = column_name for key, item in items.iteritems(): item.update({column_name: []}) store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store)}, safe=False)
def edit_item(request): store = Store.objects.get(id=request.BODY['id']) linked_columns = store.link_columns item = checker.check_link_columns(linked_columns, request.BODY['item']) if isinstance(item, HttpResponseBadRequest): return item item_id = str(request.BODY['item_id']) columns = store.columns items = store.inventory for key, val in item.iteritems(): if key not in columns: return HttpResponseBadRequest('All columns do not exist.', 'application/json') items[item_id] = item store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'item': {item_id: item}, 'store': model_to_dict(store)}, safe=False)
def inventory_operation(request, action, operation, link_column, callback_function): current_user = request.user current_boss = get_boss(current_user) store = Store.objects.get(id=request.POST['id']) linked_columns = store.link_columns changing_column = linked_columns[link_column] name_column = linked_columns['name'] item = store.inventory[request.POST['item_id']] previous_value = item[changing_column] # Do operation item[changing_column] = callback_function(item[changing_column], request.POST['change_value']) store.save() created_item_log = ItemLog.objects.create( user=current_user, action=action, operation=operation, item_name=transaction_name_regex(name_column, item), change=request.POST['change_value'], previous_value=previous_value, details={"notes": request.POST['details']}) created_item_log.store = store created_item_log.save() item_logs = list( ItemLog.objects.filter(store=store).order_by('-date').values( 'user__first_name', 'user__last_name', 'action', 'operation', 'item_name', 'change', 'previous_value', 'date', 'details', 'id')) store.inventory = sort_inventory(store, store.inventory) return {'store': model_to_dict(store), 'item_log': item_logs}
def file_upload(request): current_user = request.user if current_user.boss: boss_username = current_user.username else: boss_username = User.objects.get(boss=current_user.employee.boss).username store = Store.objects.get(id=request.POST['id']) store_name = store.name item_id = request.POST['item_id'] try: picture_file = Image.open(request.FILES['file']) except: return HttpResponseBadRequest('Must be an image.', 'application/json') asset_directory = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '..', 'templates', 'bundle', 'assets', 'files')) boss_directory = os.path.join(asset_directory, boss_username) store_directory = os.path.join(boss_directory, store_name) file_name = item_id + '_' + str(int(time.time())) + '.' + picture_file.format.lower() if not os.path.exists(boss_directory): os.mkdir(os.path.join(asset_directory, boss_username)) if not os.path.exists(store_directory): os.mkdir(os.path.join(boss_directory, store_name)) picture_file.save(os.path.join(store_directory, file_name)) store.inventory[item_id][store.picture_column].append(file_name) store.save() store_inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store), 'inventory': store_inventory}, safe=False)
def import_submit(request): store = Store.objects.get(id=request.BODY['id']) post_inventory = request.BODY['inventory'] custom_column = store.columns post_column = request.BODY['columns'] user_inventory = store.inventory linked_columns = store.link_columns missing_columns = [] for column in custom_column: if column in post_column: post_column.remove(column) else: missing_columns.append(column) store.columns = custom_column + post_column # Check every item if len(user_inventory): # If adding new columns add to every item for column in post_column: for key, item in user_inventory.iteritems(): item[column] = "" quantity_column = linked_columns['quantity'] cost_column = linked_columns['cost'] price_column = linked_columns['price'] for key, item in post_inventory.iteritems(): # Add missing columns to new items for column in missing_columns: item[column] = "" if quantity_column: quantity = item[quantity_column].strip() if quantity == "" or not quantity.isdigit(): quantity = 0 else: quantity = int(quantity) item[quantity_column] = quantity if cost_column: cost = item[cost_column].strip() if cost == "" or not cost.isdigit(): cost = 0 else: cost = int(cost) item[cost_column] = cost if price_column: price = item[price_column].strip() if price == "" or not price.isdigit(): price = 0 else: price = int(price) item[price_column] = price store.inventory.update(post_inventory) store.save() store.inventory = sort_inventory(store, store.inventory) return JsonResponse({'store': model_to_dict(store)}, safe=False)