From 39cddb2cb59f2d7b3cadd6d5d1ebb5ba265792b5 Mon Sep 17 00:00:00 2001 From: Maxime Bombar Date: Sat, 8 Sep 2018 14:04:44 +0200 Subject: [PATCH] Compute job price --- printer/models.py | 38 +++++++++++++++++++++++++++++++++++++- printer/settings.py | 17 +++++++++++++++++ printer/views.py | 9 +++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/printer/models.py b/printer/models.py index 10c5c500..7c61e298 100644 --- a/printer/models.py +++ b/printer/models.py @@ -23,8 +23,10 @@ from .validators import ( from .settings import ( MAX_PRINTFILE_SIZE, ALLOWED_TYPES, + PRICES, ) +import math """ - ```user_printing_path``` is a function that returns the path of the uploaded file, used with the FileField. @@ -63,6 +65,12 @@ class JobWithOptions(RevMixin, models.Model): Parent class : User + + + + Methods: + ```_compute_price``` compute the printing price + ```_update_price``` update printing price """ STATUS_AVAILABLE = ( ('Printable', 'Printable'), @@ -122,4 +130,32 @@ class JobWithOptions(RevMixin, models.Model): def _update_price(self): - self.price = 0.0 + self.price = self._compute_price() + + def _compute_price(self): + pages = int(self.pages) + price_paper = PRICES[self.format] + price_stapling = 0.0 + nb_staples = 0 + + if self.disposition == 'Booklet': + sheets = int((pages+3)/4) + pages = 2 * sheets + elif self.disposition == 'TwoSided': + sheets = int(pages/2.+0.5) + else: + sheets = pages + + if self.format == 'A3': + pages*=2 + + price_ink = price_paper*sheets + PRICES[self.color]*pages + + if self.stapling: + nb_staples = 2 - int('Top' in self.stapling) + + price_stapling = nb_staples * PRICES['Staples'] + + total_price = math.floor(self.count * (price_ink + price_stapling)) + + return float(total_price)/100 diff --git a/printer/settings.py b/printer/settings.py index c8879bd4..4aac7c93 100644 --- a/printer/settings.py +++ b/printer/settings.py @@ -11,3 +11,20 @@ Define variables, to be changed into a configuration table. MAX_PRINTFILE_SIZE = 25 * 1024 * 1024 # 25 MB ALLOWED_TYPES = ['application/pdf'] + + + + +## Config + +## Depreciation +depr = 2.16 + +PRICES = { + 'Depreciation': depr, + 'A3': 0.670, + 'A4': 2.1504, + 'Color': 9.0 + depr, + 'Greyscale': 0.9 + depr, + 'Staples': 1.3333, +} diff --git a/printer/views.py b/printer/views.py index e7c932f7..0a45d9af 100644 --- a/printer/views.py +++ b/printer/views.py @@ -41,7 +41,7 @@ def new_job(request): request.POST, request.FILES, ) - + if job_formset.is_valid(): files = request.FILES data = [] @@ -80,10 +80,10 @@ def new_job(request): 'printer/print.html', request ) - + # elif 'Print' in request.POST: # raise ValidationError("'%(path)s'", code='path', params = {'path': request.POST }) - + # raise Exception('On a déjà upload !') n = int(request.POST['form-TOTAL_FORMS']) job_formset = formset_factory(PrintForm)( @@ -99,6 +99,7 @@ def new_job(request): job.user = request.user job.status = 'Running' job.file = old_job.file + job._update_price() job.save() i+=1 # raise ValidationError("'%(plop)s'", code='plop', params = {'plop': request.method}) @@ -107,7 +108,7 @@ def new_job(request): 'printer:success', )) raise Exception("Invalid Job_formset") - + else: job_formset = formset_factory(JobWithOptionsForm)( None,