示例#1
0
    def wrap(func, self, *args, **kwargs):
        for rul in rules:
            key = rul[0]
            rule = rul[1]
            oval = self.request.GET.get(key)
            if not oval:
                oval = self.request.POST.get(key)
            if not oval:
                oval = self.request.matchdict.get(key)
            val = str(oval)

            if 'required' == rule and util.is_empty(oval) or util.is_empty(val):
                _die('%s is required' % key, redir_to)

            if 'string' == rule and oval and not util.is_string(val):
                _die('%s must not be a number' % key, redir_to)

            if 'number' == rule and oval and not util.is_number(val):
                _die('%s must be a number' % key, redir_to)

            if 'float' == rule and oval and not util.is_float(val):
                _die('%s must be a float' % key, redir_to)

            if 'int' == rule and oval and not util.is_int(val):
                _die('%s must be a integer' % key, redir_to)

            if 'equals' == rule and oval and len(rul) == 3:
                val2 = self.request.GET.get(rul[2], self.request.POST.get(rul[2], None))
                if util.is_empty(val) != util.is_empty(val2) or str(val) != str(val2):
                    _die('%s must equal %s' % (val2, val), redir_to)

        return func(self, *args, **kwargs)
示例#2
0
 def test_util(self):
     d8e = util.today_date()
     dtime = util.today()
     assert util.format_rss_date(d8e) == d8e.strftime("%a, %d %b %Y %H:%M:%S EST")
     assert util.words_date(dtime) == dtime.strftime("%B %d, %Y")
     assert util.is_empty(" ") == True
     assert util.float_("8") == None
     assert util.page_list([1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 2) == [3, 4]
     assert util.page_list([1, 2, 3, 4, 5, 6, 7, 8, 9], None, None) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
     assert util.parse_date("2012-05-06") == datetime.datetime.strptime("2012-05-06", "%Y-%m-%d")
     today_ = datetime.date.today()
     assert [today_.year + 10, today_.year + 10] in util.year_list()
     assert util.month_list()[0] == ["1", "January"]
     assert util.this_year() == datetime.date.today().year
     assert util.get_first_day(today_) == util.get_first_day(
         today_
     )  # this is pretty dumb.  it works, just get it covered.
     assert util.get_last_day(today_) == util.get_last_day(today_)
     assert util.to_uuid("ken") == None
     assert int(util.average([1, 2, 3])) == 2
     assert util.format_date(util.truncate_datetime(dtime)) == util.str_today()
     assert util.is_today(d8e) == True