From d5dacda6d8d16882541709d26eb0f22a0404362b Mon Sep 17 00:00:00 2001 From: Brian Simpson Date: Wed, 11 Sep 2013 16:26:23 -0400 Subject: [PATCH] VBid to validate promoted link bids. --- r2/r2/controllers/promotecontroller.py | 5 +++-- r2/r2/lib/validator/validator.py | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/r2/r2/controllers/promotecontroller.py b/r2/r2/controllers/promotecontroller.py index d7fb914dd..51c4976ea 100644 --- a/r2/r2/controllers/promotecontroller.py +++ b/r2/r2/controllers/promotecontroller.py @@ -65,6 +65,7 @@ from r2.lib.validator import ( validatedForm, ValidCard, ValidIP, + VBid, VBoolean, VByName, VDate, @@ -486,8 +487,8 @@ class PromoteController(ListingController): business_days=False, sponsor_override=True), link=VLink('link_id'), - bid=VFloat('bid', min=0, max=g.max_promote_bid, - coerce=False, error=errors.BAD_BID), + bid=VBid('bid', min=0, max=g.max_promote_bid, + coerce=False, error=errors.BAD_BID), sr=VSubmitSR('sr', promotion=True), campaign_id36=nop("campaign_id36"), targeting=VLength("targeting", 10)) diff --git a/r2/r2/lib/validator/validator.py b/r2/r2/lib/validator/validator.py index 036788634..4d5a3f803 100644 --- a/r2/r2/lib/validator/validator.py +++ b/r2/r2/lib/validator/validator.py @@ -1402,6 +1402,17 @@ class VNumber(Validator): def cast(self, val): raise NotImplementedError + def _set_error(self): + if self.max is None and self.min is None: + range = "" + elif self.max is None: + range = _("%(min)d to any") % dict(min=self.min) + elif self.min is None: + range = _("any to %(max)d") % dict(max=self.max) + else: + range = _("%(min)d to %(max)d") % dict(min=self.min, max=self.max) + self.set_error(self.error, msg_params=dict(range=range)) + def run(self, val): if not val: return self.num_default @@ -1419,15 +1430,7 @@ class VNumber(Validator): raise ValueError, "" return val except ValueError: - if self.max is None and self.min is None: - range = "" - elif self.max is None: - range = _("%(min)d to any") % dict(min=self.min) - elif self.min is None: - range = _("any to %(max)d") % dict(max=self.max) - else: - range = _("%(min)d to %(max)d") % dict(min=self.min, max=self.max) - self.set_error(self.error, msg_params=dict(range=range)) + self._set_error() class VInt(VNumber): def cast(self, val): @@ -1437,6 +1440,9 @@ class VFloat(VNumber): def cast(self, val): return float(val) +class VBid(VFloat): + def _set_error(self): + self.set_error(self.error, msg_params=dict(min=self.min, max=self.max)) class VCssName(Validator): """