class Api(Document): """ Api model used to authorize api usages! """ secret = StringField(required=True) token = StringField(required=True) expires = DateTimeField(required=True, default=(datetime.datetime.utcnow() + datetime.timedelta(minutes=15))) # TODO: expire each Anonymous token after 15 minutes! # TODO: no more than 2 tokens each 15 minutes for each ip ownerID = IntField(default=0) ip_address = StringField(required=True) generated_on = DateTimeField(default=datetime.datetime.utcnow()) meta = {'collection': 'tokens'} def genSecret(self, len=16): allchar = string.ascii_letters + string.digits self.secret = "".join(choice(allchar) for x in range(len)) def genToken(self): payload = { 'exp': self.expires, 'iat': datetime.datetime.utcnow(), 'ownerID': self.ownerID } self.token = jwt.encode(payload, self.secret, algorithm='HS256').decode('utf-8')
class TokenModel(Document): token = StringField(required=True, min_length=32, max_length=512) meta = {'collection': 'tokens'} async def getById(self, tokenid): curr_id = await self.objects(tokenid=tokenid).first() if curr_id is not None: ret_data_json = curr_id.to_json() ret_data = json.loads(ret_data_json) return ret_data else: return None
class UserModel(Document): userid = IntField(required=True, primary_key=True) email = EmailField(required=True, min_length=6, max_length=127, unique=True) password = StringField(required=True, min_length=6, max_length=127) username = StringField(required=True, min_length=6, max_length=30, unique=True) registered = DateTimeField(default=datetime.datetime.utcnow()) async def authorize(self, username, password): curr_user = await self.objects(username=username).first() if curr_user is not None: if sha512_crypt.verify(password, curr_user.password): userid = curr_user.userid else: userid = None return userid else: return None async def getById(self, userid): curr_user = await self.objects(username=username).first() if curr_user is not None: ret_data_json = curr_user.to_json() ret_data = json.loads(ret_data_json) return ret_data else: return None async def setPassword(self, password): self.password = sha512_crypt.hash(password)
class PasteModel(Document): """ The PASTE class have attributes for a paste such as raw text (encoded inBase64), pasteDate and expiretDate and so on! This class is developing. """ author = StringField(default="Anonymous", max_length=127) title = StringField(default="Untitled", max_length=127) shorturl = URLField(default="https://beepaste.io/") uri = StringField(required=True, min_length=6, max_length=6, unique=True) date = DateTimeField(default=datetime.datetime.utcnow()) expiryDate = DateTimeField(default=datetime.datetime.utcnow()) expireAfter = IntField(default=0) toExpire = BooleanField(default=False) raw = StringField(required=True) encryption = StringField(choices=validEncriptions, default="no") syntax = StringField(choices=validSyntax, default="text") views = IntField(default=0) ownerID = IntField(default=0) meta = {'collection': 'pastes'} def generate_uri(self, len=6): allchar = string.ascii_letters + string.digits return "".join(choice(allchar) for x in range(len)) async def generate_url(self): new_uri = self.generate_uri() count = await PasteModel.objects(uri=new_uri).count() while count > 0: new_uri = self.generate_uri() count = await PasteModel.objects(uri=new_uri).count() self.uri = new_uri url = global_cnf['base_url'] + 'paste/view/' + new_uri if bitly_cnf['use'] is True: access_token = bitly_cnf['token'] shortener = Shortener('Bitly', bitly_token=access_token) self.shorturl = shortener.short(url) else: self.shorturl = url
class DataModel(Document): # id = IntField(default=148290) text = StringField(required=True) meta = {'collection': 'texts'}
class TestDoc(Document): a = StringField() lf = ListField() docint = IntField()
class MainDoc(SuperDoc): docname = StringField() docint = IntField() list_field = ListField(StringField()) embedded = EmbeddedDocumentField(Embed) ref = ReferenceField(RefDoc)
class SuperDoc(Document): some_field = StringField() reflist = ListField(ReferenceField(RefDoc)) meta = {'allow_inheritance': True}
class RefDoc(Document): refname = StringField() embedlist = ListField(EmbeddedDocumentField(EmbedRef))
class Embed(EmbeddedDocument): field = StringField()
class TestCls(Document): someattr = StringField() ref = ReferenceField(TestRef)
class TestRef(Document): attr = StringField()