def ViewEditProduct(request, slug): template = 'settings/products/details.html' product_instance = get_object_or_404(Product, slug=slug) form = ProductForm(request.POST or None, request.FILES or None, slug=product_instance.corporate.slug, instance=product_instance, action=reverse('editProduct', kwargs={'slug': slug})) categoryForm = ProductCategoryForm(request.POST or None) reportForm = BaseReportForm() if form.is_valid(): corporate = CorpManager().get_corp_form_user_profile(request.user) corporate = corporate.corporate form.save(user=request.user, corporate=corporate) return redirect( reverse('productList', kwargs={'slug': product_instance.corporate.slug})) return render(request, template_name=template, context={ 'form': form, 'new': False, 'reportForm': reportForm, 'categoryForm': categoryForm })
def EditForms(request, slug, id): # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) template = 'forms/form.html' # Create the formset, specifying the form and formset we want to use. QuestionFormSet = formset_factory(QuestionForm, formset=BaseQuestionFormSet) # Get our existing data for this user. This is used as initial data. question_data = None form_questions = IForm(slug=slug).getFormQuestions(id) if form_questions: question_data = [{'question': l.question} for l in form_questions] formsForm = FormsForm(request.POST or None, user=request.user, corp=corp, instance=IForm(slug).getFormInfo(id=id)) QuestionFormSetform = QuestionFormSet(request.POST or None, initial=question_data) if formsForm.is_valid(): m = formsForm.save() # Now save the data for each form in the formset new_questions = [] for question_form in QuestionFormSetform: print hasattr(question_form, 'cleaned_data') print question_form new_questions.append( FormQuestions( form=m, question=question_form.cleaned_data.get('question'))) try: with transaction.atomic(): # Replace the old with the new FormQuestions.objects.filter(form=m).delete() FormQuestions.objects.bulk_create(new_questions) # And notify our users that it worked messages.success(request, 'You have updated your form.') # Redirect return redirect(reverse('ViewForms', kwargs={'slug': slug})) except IntegrityError: # If the transaction failed messages.error(request, 'There was an error saving your form.') return redirect(reverse('ViewForms', kwargs={'slug': slug})) context = { 'formsForm': formsForm, 'QuestionFormSetform': QuestionFormSetform, 'edit': True, 'id': id } return render(request, template, context)
def ExportAuditRetails(request): resource = AuditRetailResources() # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) sqs = AuditRetails.objects.filter(corporate=corp) dataset = resource.export(sqs) response = HttpResponse(dataset.csv, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="auditRetails.csv"' return response
def ExportClients(request): clientResources = ClientResource() # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) sqs = Client.objects.filter(corporate=corp) dataset = clientResources.export(sqs) response = HttpResponse(dataset.csv, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="clients.csv"' return response
def ExportSalesForce(request): salesForceResources = SalesForceResource() # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) sqs = SalesForce.objects.filter(corp_id=corp) dataset = salesForceResources.export(sqs) response = HttpResponse(dataset.csv, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="sales_force.csv"' return response
def AddClient(request, slug): template = 'clients/details.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) form = ClientForm(request.POST or None, action=reverse('AddClient', kwargs={'slug': slug}), corp_instance=corp) if form.is_valid(): corporate = CorpManager().get_corp_form_user_profile(request.user) corporate = corporate.corporate m = form.save(user=request.user, corporate=corporate, main_branch=None) return redirect(reverse('viewClient', kwargs={'slug': slug})) return render(request, template_name=template, context={ 'form': form, 'new': True })
def EditClient(request, slug): template = 'clients/details.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) client_instance = get_object_or_404(Client, slug=slug) form = ClientForm(request.POST or None, instance=client_instance, corp_instance=corp, action=reverse('EditClient', kwargs={'slug': slug})) reportForm = ClientReportForm(client=client_instance.id) if form.is_valid(): corporate = CorpManager().get_corp_form_user_profile(request.user) corporate = corporate.corporate m = form.save(user=request.user, corporate=corporate, main_branch=None) return redirect(reverse('viewClient', kwargs={'slug': corp.slug})) # retrieve schedual schedular = SchedulerManager().get_scheduler_by_client(client_slug=slug) # retrieve client salesforce sqs = ClientManager().get_sales_force_by_client(slug=slug) data = [] # serialize data if sqs: for row in sqs: data.append( SalesForceSerializer(row, context={ 'request': request }).data) data = json.dumps(data, ensure_ascii=False) return render(request, template_name=template, context={ 'form': form, 'new': False, 'reportForm': reportForm, 'schedular': schedular, 'sales_force': data, 'client_id': client_instance.pk })
def newBillBoard(request): template = 'settings/billBoard/form.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) form = BillBoardForm(request.POST or None, action=reverse('newBillBoards')) if form.is_valid(): m = form.save(user=request.user, corporte=corp) if m: return redirect(reverse('billBoards', kwargs={'slug': corp.slug})) context = {'form': form} return render(request, template_name=template, context=context)
def AddCategory(request): if request.POST: valid = False user = request.user categoryForm = ProductCategoryForm(request.POST or None) if categoryForm.is_valid(): corporate = CorpManager().get_corp_by_user(request.user) result = categoryForm.save(user=user, corporate=corporate) if result: valid = True objects = ProductGroupWithoutProductSerialziers(result).data ret = {"valid": valid, "objects": objects} return HttpResponse(json.dumps(ret, ensure_ascii=False))
def dashboard(request, slug): template = 'clients/dashboard.html' context = {} if request.POST: # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) dashboards = DashBoardReports(start_date=request.POST['dateFrom'], end_date=request.POST['dateTo'], city=None, corp=corp, context=None) context['visitDetails'] = dashboards.get_visit_details_charts() context['orders'] = dashboards.get_total_orders_chart() return render(request, template_name=template, context=context)
def home(request): context = {} template = 'index.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) currentDate = datetime.datetime.today().strftime('%Y-%m-%d') if request.POST: currentDate = request.POST.get('date', currentDate) DashBoardReports(currentDate, currentDate, None, corp, context).get_dashboard_statistics() context['locations'] = TrackingReports( currentDate, currentDate).tracking_sales_force_by_corp(corp) return render(request, template_name=template, context=context)
def editBillBoard(request, id): template = 'settings/billBoard/form.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) # Bill boards instance = get_object_or_404(BillBoard, id=id) form = BillBoardForm(request.POST or None, instance=instance, action=reverse('editBillBoard', kwargs={'id': id})) if form.is_valid(): m = form.save(user=request.user, corporte=corp) if m: return redirect(reverse('billBoards', kwargs={'slug': corp.slug})) context = {'form': form} return render(request, template_name=template, context=context)
def CreateForms(request, slug): # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) template = 'forms/form.html' # Create the formset, specifying the form and formset we want to use. QuestionFormSet = formset_factory(QuestionForm, formset=BaseQuestionFormSet) formsForm = FormsForm(request.POST or None, user=request.user, corp=corp) QuestionFormSetform = QuestionFormSet(request.POST or None) if formsForm.is_valid(): m = formsForm.save() # Now save the data for each form in the formset new_questions = [] for question_form in QuestionFormSetform: print question_form new_questions.append( FormQuestions( form=m, question=question_form.cleaned_data.get('question'))) try: with transaction.atomic(): # Replace the old with the new FormQuestions.objects.filter(form=m).delete() FormQuestions.objects.bulk_create(new_questions) # And notify our users that it worked messages.success(request, 'You have updated your form.') # Redirect return redirect(reverse('ViewForms', kwargs={'slug': slug})) except IntegrityError: # If the transaction failed messages.error(request, 'There was an error saving your form.') context = { 'formsForm': formsForm, 'QuestionFormSetform': QuestionFormSetform, } return render(request, template, context)
def AddSalesForce(request): template = 'sales_force/details.html' # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) form = SalesForceForm(request.POST or None, request.FILES or None, user_instance=request.user, corp_instance=corp, action=reverse('createSalesForce')) if form.is_valid(): form.save(user=request.user) return redirect( reverse('viewSalesForceByUser', kwargs={'slug': corp.slug})) return render(request, template_name=template, context={ 'form': form, 'new': True })
def ExportOrders(request): sqs = Orders.objects orderResources = OrderResource() if request.POST: if 'fromDate' in request.POST and len(request.POST['fromDate']) > 0: sqs = sqs.filter(order_date__gte=request.POST['fromDate']) if 'toDate' in request.POST and len(request.POST['toDate']) > 0: sqs = sqs.filter(order_date__lte=request.POST['toDate']) if 'salesForce' in request.POST and len( request.POST['salesForce']) > 0: sqs = sqs.filter(sales_force__id=request.POST['toDate']) # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) sqs = sqs.filter(branch__corporate=corp) dataset = orderResources.export(sqs) response = HttpResponse(dataset.csv, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="orders.csv"' return response
def AddProduct(request, slug): template = 'settings/products/details.html' form = ProductForm(request.POST or None, request.FILES or None, slug=slug, action=reverse('createProduct', kwargs={'slug': slug})) categoryForm = ProductCategoryForm(request.POST or None) if form.is_valid(): corporate = CorpManager().get_corp_form_user_profile(request.user) corporate = corporate.corporate m = form.save(user=request.user, corporate=corporate) return redirect(reverse('productList', kwargs={'slug': slug})) return render(request, template_name=template, context={ 'form': form, 'new': True, 'categoryForm': categoryForm })
def EditSalesForce(request, slug): template = 'sales_force/details.html' corp = CorpManager().get_corp_by_user(request.user) sales_force_instance = get_object_or_404(SalesForce, slug=slug) form = SalesForceForm(request.POST or None, request.FILES or None, user_instance=request.user, instance=sales_force_instance, corp_instance=corp, action=reverse('editSalesForce', kwargs={'slug': slug})) reportForm = SalesForceReportForm(sales_force=sales_force_instance.id) if form.is_valid(): form.save(user=request.user) return redirect( reverse('viewSalesForceByUser', kwargs={'slug': corp.slug})) # Retreve clients sqs = ClientManager().get_client_by_sales_force(slug) data = [] # serialize data if sqs: for row in sqs: data.append( ClientSerializer(row, context={ 'request': request }).data) data = json.dumps(data, ensure_ascii=False) # retrieve schedual schedular = SchedulerManager().get_schedul_by_sales_force(sales_force=slug) return render(request, template_name=template, context={ 'form': form, 'new': False, 'reportForm': reportForm, 'clients': data, 'sales_force_id': sales_force_instance.pk, 'schedular': schedular })
def DeleteSalesForce(request, slug): corp = CorpManager().get_corp_by_user(request.user) SalesForceManager().deleteSalesForce(slug) return redirect(reverse('viewSalesForceByUser', kwargs={'slug': corp.slug}))
def DeleteClient(request, slug): corp = CorpManager().get_corp_by_user(request.user) ClientManager().deleteClient(slug) return redirect(reverse('viewClient', kwargs={'slug': corp.slug}))
def DeleteProduct(request, slug): # Get product corp = CorpManager().get_corp_by_user(request.user) ProductManager().deleteProduct(slug=slug) return redirect(reverse('productList', kwargs={'slug': corp.slug}))
def DeleteForm(request, id): # Get Corp Info corp = CorpManager().get_corp_by_user(request.user) IForm(slug=None).deleteForm(id) return redirect(reverse('ViewForms', kwargs={'slug': corp.slug}))