def get(self): form = CalculatorForm(self.request.arguments) message = None convert_direction = self.get_argument('convert', None) if convert_direction and form.validate(): try: if convert_direction == 'up': source_number = self.get_argument('number_2') source_alphabet = self.get_argument('alphabet_2') target_alphabet = self.get_argument('alphabet_1') num = str_to_int(source_number, source_alphabet) form.number_1.data = int_to_str(num, target_alphabet) else: source_number = self.get_argument('number_1') source_alphabet = self.get_argument('alphabet_1') target_alphabet = self.get_argument('alphabet_2') num = str_to_int(source_number, source_alphabet) form.number_2.data = int_to_str(num, target_alphabet) except ValueError as error: message = str(error) self.render('calculator.html', form=form, message=message)
def test_int_to_str(self): self.assertEqual('a', int_to_str(0, 'abcde')) self.assertEqual('1E0F3', int_to_str(123123, '0123456789ABCDEF')) self.assertEqual( 'w1R', int_to_str( 123123, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ))
def to_dict(self, with_shortcode=False): ans = {x.key:x.value for x in object_state(self).attrs} ans.update({ 'project': self.project.to_dict(), 'datetime_claimed': calendar.timegm(self.datetime_claimed.utctimetuple()) if self.datetime_claimed else None, }) if with_shortcode: ans['lower_shortcode'] = int_to_str(self.lower_sequence_num, self.project.alphabet) ans['upper_shortcode'] = int_to_str(self.upper_sequence_num, self.project.alphabet) return ans
def to_dict(self, with_shortcode=False): ans = {x.key: x.value for x in object_state(self).attrs} ans.update({ 'project': self.project.to_dict(), 'datetime_claimed': calendar.timegm(self.datetime_claimed.utctimetuple()) if self.datetime_claimed else None, }) if with_shortcode: ans['lower_shortcode'] = int_to_str(self.lower_sequence_num, self.project.alphabet) ans['upper_shortcode'] = int_to_str(self.upper_sequence_num, self.project.alphabet) return ans
def to_dict(self, with_shortcode=False): ans = { 'id': self.id, 'project': self.project.to_dict(), 'lower_sequence_num': self.lower_sequence_num, 'upper_sequence_num': self.upper_sequence_num, 'datetime_claimed': calendar.timegm(self.datetime_claimed.utctimetuple()) if self.datetime_claimed else None, 'tamper_key': self.tamper_key, 'username': self.username, 'ip_address': self.ip_address, } if with_shortcode: ans['lower_shortcode'] = int_to_str(self.lower_sequence_num, self.project.alphabet) ans['upper_shortcode'] = int_to_str(self.upper_sequence_num, self.project.alphabet) return ans
def transform_sequence_num(self, sequence_num): seed = struct.pack('>Q', sequence_num) hasher = hashlib.md5(seed) digest = hasher.digest() num = struct.unpack('>Q', digest[:8])[0] shortcode = alphabet.int_to_str(num, self.params['alphabet']) shortcode = shortcode[-self.get_shortcode_width():] return shortcode
def lower_shortcode(self): return int_to_str(self.lower_sequence_num, self.alphabet)
def test_int_to_str(self): self.assertEqual('a', int_to_str(0, 'abcde')) self.assertEqual('1E0F3', int_to_str(123123, '0123456789ABCDEF')) self.assertEqual('w1R', int_to_str(123123, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'))
def transform_sequence_num(self, sequence_number): return alphabet.int_to_str( sequence_number, self.params['alphabet'] )