def save(self): """Saves or updates the ShortLink instance in database""" if not self.url: raise ShortLinkError("Token or url are empty") r = get_redis_connection() #If not token or counter then we need to get the apropiate one (last) if not self.token and not self.counter: self.counter = ShortLink.incr_counter() # Do all in pipeline pipe = r.pipeline() # Save token(Hash) and url(set) #If there is not a date then take now if not self.creation_date: self.creation_date = dateutils.unix_now_utc() disabled = 1 if self.disabled else 0 mappings = {'url': self.url, 'creation_date': self.creation_date, 'clicks': self.clicks, 'title': self.title, 'host': self.host, 'disabled': disabled} pipe.hmset(ShortLink.REDIS_TOKEN_KEY.format(self.token), mappings) pipe.sadd(ShortLink.REDIS_URL_KEY.format(self.url), self.token) return pipe.execute()
def test_save_shortLink(self): counter = random.randrange(0, 100000) url = "xlarrakoetxea.org" creation_date = dateutils.unix_now_utc() clicks = 20 title = "This is a title" host = url disabled = True # Save the links sl = ShortLink(counter=counter, url=url, creation_date=creation_date, clicks=clicks, title=title, host=host) sl.disabled = disabled sl.save() r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB) # Construct the Keys rtk = ShortLink.REDIS_TOKEN_KEY.format(utils.counter_to_token(counter)) ruk = ShortLink.REDIS_URL_KEY.format(url) # Check token = utils.counter_to_token(counter) self.assertTrue(token in r.smembers(ruk)) keys = ('url', 'creation_date', 'clicks', 'title', 'host') data = [url, creation_date, clicks, title, host] aux = r.hmget(rtk, keys) data_result = [aux[0], int(aux[1]), int(aux[2]), aux[3], aux[4]] self.assertEquals(data, data_result)
def test_click_store(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) c.save() # Check the stored object key = Click.REDIS_CLICK_KEY.format(token, click_id) r = get_redis_connection() values = r.hgetall(key) self.assertEquals(OS, values["os"]) self.assertEquals(browser, values["browser"]) self.assertEquals(ip, values["ip"]) self.assertEquals(click_date, int(values["click_date"])) self.assertEquals(language, values["language"]) self.assertEquals(location, values["location"])
def save(self): """ Save a click in the database. If the instance doens't have the click_id the method will get the correct value, based on the Short link click counter""" r = get_redis_connection() if not self.token: raise ClickError("Not enought data to store click instance") #if there isn't an identification then get the apropiate id if not self.click_id: self.click_id = ShortLink.incr_clicks(self.token) #set the date if there isn't a date if not self.click_date: self.click_date = dateutils.unix_now_utc() key = Click.REDIS_CLICK_KEY.format(self.token, self.click_id) #Create the data to_store = {} for i in Click.FIELDS: value = getattr(self, i) if value: to_store[i] = value r.hmset(key, to_store)
def __init__( self, notification_type=None, title=None, description=None, image=None, date=None, user_id=None, key_read=None, key_unread=None, push_key=None, read=False, ): self._notification_type = notification_type self._title = title self._description = description self._image = image self.date = unix_now_utc() self._user_id = user_id self._read = read if not push_key: self._push_key = Notification.PUSH_KEY_FORMAT.format(user_id) if not key_read: self._key_read = Notification.STORE_KEY_READ_FORMAT.format(user_id) if not key_unread: self._key_unread = Notification.STORE_KEY_UNREAD_FORMAT.format(user_id)
def test_unix_now_utc(self): unix_now = dateutils.unix_now_utc() now = dateutils.unix_to_datetime(unix_now) almost_now = datetime.utcnow() self.assertEquals(almost_now.year, now.year) self.assertEquals(almost_now.month, now.month) self.assertEquals(almost_now.day, now.day) self.assertEquals(almost_now.hour, now.hour) self.assertEquals(almost_now.minute, now.minute) self.assertEquals(almost_now.second, now.second)
def test_click_basic_object_str(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) format = Click.OBJECT_STR_FORMAT.format(click_id, token, ip, OS, browser, click_date, language, location) self.assertEquals(format, str(c))
def test_shortlink_basic_object(self): url = "xlarrakoetxea.org" counter = random.randrange(0, 100000) token = utils.counter_to_token(counter) creation_date = None clicks = 0 title = "This is a title" host = url disabled = True # Setters from counter sl = ShortLink(counter=counter, url=url, title=title, host=host) sl.disabled = disabled # Getters self.assertEquals(url, sl.url) self.assertEquals(counter, sl.counter) self.assertEquals(token, sl.token) self.assertEquals(creation_date, sl.creation_date) self.assertEquals(clicks, sl.clicks) self.assertEquals(title, sl.title) self.assertEquals(host, sl.host) self.assertEquals(disabled, sl.disabled) # Setters from token sl2 = ShortLink(token=token, url=url) creation_date = dateutils.unix_now_utc() sl2.creation_date = creation_date clicks = 5 sl2.clicks = clicks sl2.title = title sl2.host = host sl2.disabled = disabled # Getters self.assertEquals(url, sl2.url) self.assertEquals(counter, sl2.counter) self.assertEquals(token, sl2.token) self.assertEquals(creation_date, sl2.creation_date) self.assertEquals(clicks, sl2.clicks) self.assertEquals(title, sl2.title) self.assertEquals(host, sl2.host) self.assertEquals(disabled, sl2.disabled)
def test_click_find(self): token = utils.counter_to_token(random.randrange(0, 100000)) OS = "linux" ip = "111.222.333.444" browser = "firefox" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" url = "http://xlarrakoetxea.org" sl = ShortLink(token=token, url=url) sl.save() c = Click( token=token, os=OS, ip=ip, browser=browser, click_date=click_date, language=language, location=location ) c.save() c2 = Click.find(token, c.click_id) self.assertEquals(c, c2)
def test_click_basic_object(self): token = utils.counter_to_token(random.randrange(0, 100000)) click_id = random.randrange(0, 100000) OS = "linux" browser = "firefox" ip = "111.222.333.444" click_date = dateutils.unix_now_utc() language = "EN_us" location = "US" c = Click(click_id, token, ip, OS, browser, click_date, language, location) # Check getters self.assertEquals(token, c.token) self.assertEquals(click_id, c.click_id) self.assertEquals(OS, c.os) self.assertEquals(browser, c.browser) self.assertEquals(ip, c.ip) self.assertEquals(click_date, c.click_date) self.assertEquals(language, c.language) self.assertEquals(location, c.location) # Check setters c2 = Click() c2.token = token c2.click_id = click_id c2.os = OS c2.browser = browser c2.ip = ip c2.click_date = click_date c2.language = language c2.location = location self.assertEquals(token, c2.token) self.assertEquals(click_id, c2.click_id) self.assertEquals(OS, c2.os) self.assertEquals(browser, c2.browser) self.assertEquals(ip, c2.ip) self.assertEquals(click_date, c2.click_date) self.assertEquals(language, c2.language) self.assertEquals(location, c2.location)