def token(self, value): self._token = value self._counter = token_to_counter(value)
def find(cls, counter=None, token=None, url=None): """Finds a shortlink or various shortlinks based on the counter, token or url :param counter: The counter of the link (numeric id) :param token: The token calculated based on the counter id :param url: The url of the stored ShortLink """ aux_self = ShortLink() if counter is not None: aux_self.counter = counter aux_self.token = counter_to_token(counter) data = aux_self._find_by_token() #Not found... if not data: raise ShortLinkNotFoundError("Short link not found") aux_self.url = data.get('url') aux_self.creation_date = int(data.get('creation_date', 0)) aux_self.clicks = int(data.get('clicks', 0)) aux_self.title = data.get('title') aux_self.host = data.get('host') aux_self.disabled = True if int(data.get('disabled')) else False return aux_self elif token is not None: aux_self.token = token aux_self.counter = token_to_counter(token) data = aux_self._find_by_token() #Not found... if not data: raise ShortLinkNotFoundError("Short link not found") aux_self.url = data.get('url') aux_self.creation_date = int(data.get('creation_date', 0)) aux_self.clicks = int(data.get('clicks', 0)) aux_self.title = data.get('title') aux_self.host = data.get('host') aux_self.disabled = True if int(data.get('disabled')) else False return aux_self elif url: aux_self.url = url tokens = aux_self._find_by_url() #Not found... if not tokens: ShortLinkNotFoundError("Short links not found") #Check again (maybe we have a list with all Nones) nothing = True for i in tokens: if i: nothing = False break if nothing: raise ShortLinkNotFoundError("Short link not found") short_links = [] for i in tokens: aux_self.token = i data = aux_self._find_by_token() sl = ShortLink() sl.token = i sl.url = data.get('url') sl.creation_date = int(data.get('creation_date', 0)) sl.clicks = int(data.get('clicks', 0)) sl.title = data.get('title') sl.host = data.get('host') #aux_self.title = data.get('title') #aux_self.host = data.get('host') sl.disabled = True if int(data.get('disabled')) else False short_links.append(sl) return short_links raise ShortLinkError("No enought data to search")
def test_number_translation(self): for i in UtilTest.test_data: self.assertEquals(i[1], utils.token_to_counter(i[0]))