Add VLocation validator.

This commit is contained in:
Brian Simpson
2013-11-27 11:10:05 -05:00
parent 7f2e9b34d6
commit 138a4c95c6
2 changed files with 21 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ error_list = dict((
('JSON_INVALID', _('unexpected JSON structure')),
('JSON_MISSING_KEY', _('JSON missing key: "%(key)s"')),
('NO_CHANGE_KIND', _("can't change post type")),
('INVALID_LOCATION', _("invalid location")),
))
errors = Storage([(e, e) for e in error_list.keys()])

View File

@@ -37,6 +37,7 @@ from r2.lib.jsonresponse import JQueryResponse, JsonResponse
from r2.lib.log import log_text
from r2.lib.permissions import ModeratorPermissionSet
from r2.models import *
from r2.models.promo import Location
from r2.lib.authorize import Address, CreditCard
from r2.lib.utils import constant_time_compare
from r2.lib.require import require, require_split, RequirementException
@@ -1709,6 +1710,25 @@ class VPriority(Validator):
return PROMOTE_DEFAULT_PRIORITY
class VLocation(Validator):
default_param = ("country", "region", "metro")
def run(self, country, region, metro):
if not (country or region or metro):
return None
if not (country and not (region or metro) or
(country and region and metro)):
# can target just country or country, region, and metro
self.set_error(errors.INVALID_LOCATION, code=400)
elif (country not in g.locations or
region and region not in g.locations[country]['regions'] or
metro and metro not in g.locations[country]['regions'][region]['metros']):
self.set_error(errors.INVALID_LOCATION, code=400)
else:
return Location(country, region, metro)
class VImageType(Validator):
def run(self, img_type):
if not img_type in ('png', 'jpg'):