def get_by_passphrase(passphrase): jot = None with db.connection() as c: jot = c.hgetall("jot:" + passphrase) if not jot: return None jot["passphrase"] = passphrase jot["expires_on"] = dateutil.parser.parse(jot.get("expires_on")) jot["created_on"] = dateutil.parser.parse(jot.get("created_on")) return Message(jot)
def save(self, overwrite=False): self.validate() jot = Message.get_by_passphrase(self.passphrase) if jot: if overwrite: if jot.edit_key != self.edit_key: raise ModelValidationError({"edit_key": "the edit key you provided does not match"}) else: raise ModelValidationError("Jot already exists") with db.connection() as c: c.hmset( "jot:" + self.passphrase, { "message": self.message, "created_on": self.created_on, "expires_on": self.expires_on, "pin": self.pin, "send_read_receipt": self.send_read_receipt, "edit_key": self.edit_key, }, ) c.expireat(self.passphrase, self.expires_on) return True
def all(): with db.connection() as c: return c.zrange("sug", start=0, end=-1, withscores=True, score_cast_func=parse_timestamp)
def available(count=False): end = dt.datetime.utcnow().strftime('%s') with db.connection() as c: return c.zrangebyscore("sug", min=0, max=end, withscores=True, score_cast_func=parse_timestamp)
def tearDown(self): with db.connection() as c: c.flushdb()
def delete_by_passphrase(passphrase): jot = Message.get_by_passphrase(passphrase) if not jot: raise ModelValidationError("Jot does not exist") with db.connection() as c: return c.delete("jot:" + passphrase)
def update(klass, passphrase, release_at): if isinstance(release_at, dt.datetime): release_at = release_at.strftime("%s") with db.connection() as c: return c.zadd("sug", release_at, passphrase)
def create(klass, passphrase): now = dt.datetime.utcnow().strftime("%s") with db.connection() as c: return c.zadd("sug", now, passphrase)