Skip to content

Instantly share code, notes, and snippets.

@kordless
Created March 5, 2021 03:46
Show Gist options
  • Select an option

  • Save kordless/c943f26fcf59c8c2adca7064c1495758 to your computer and use it in GitHub Desktop.

Select an option

Save kordless/c943f26fcf59c8c2adca7064c1495758 to your computer and use it in GitHub Desktop.
Models for Mitta
import datetime
import os
import requests
from google.cloud import ndb
import flask_login
from lib.util import random_string, random_number, generate_token, random_name
from lib.solr import doc_count
import config
# client connection
client = ndb.Client()
timestring = "%Y-%m-%dT%H:%M:%SZ"
# transactions allow one shot sending data to shell via URL
class Transaction(ndb.Model):
uid = ndb.StringProperty() # owner
tid = ndb.StringProperty()
created = ndb.DateTimeProperty()
@classmethod
def get_by_tid(cls, tid):
with client.context():
return cls.query(cls.tid == tid).get()
@classmethod
def create(cls, tid=None, uid=None):
with client.context():
cls(
tid = tid,
uid = uid,
created = datetime.datetime.utcnow()
).put()
return cls.query(cls.tid == tid).get()
# user inherits from flask_login and ndb
class User(flask_login.UserMixin, ndb.Model):
email = ndb.StringProperty()
phone = ndb.StringProperty()
phone_code = ndb.StringProperty(default=False)
uid = ndb.StringProperty() # user_id
api_token = ndb.StringProperty()
mail_token = ndb.StringProperty()
mail_confirm = ndb.BooleanProperty(default=False)
mail_tries = ndb.IntegerProperty(default=0)
authenticated = ndb.BooleanProperty(default=False)
active = ndb.BooleanProperty(default=True)
anonymous = ndb.BooleanProperty(default=False)
paid = ndb.BooleanProperty(default=False)
failed_2fa_attempts = ndb.IntegerProperty(default=0)
created = ndb.DateTimeProperty()
# flask-login
def is_active(self): # all accounts are active
return self.active
def get_id(self):
return self.uid
def is_authenticated(self):
return self.authenticated
def is_anonymous(self):
return self.anonymous
@classmethod
def token_reset(cls, uid=uid):
with client.context():
user = cls.query(cls.uid == uid).get()
user.api_token = generate_token()
user.put()
return user
@classmethod
def create(cls, email="noreply@mitta.us", phone="+1"):
with client.context():
cls(
email = email,
phone = phone,
phone_code = generate_token(),
created = datetime.datetime.utcnow(),
uid = random_string(size=17),
mail_token = generate_token(),
api_token = generate_token()
).put()
from lib.solr import create_collection
create_collection(cls.uid)
return cls.query(cls.phone == phone, cls.email == email).get()
@classmethod
def get_by_email(cls, email):
with client.context():
return cls.query(cls.email == email).get()
@classmethod
def get_by_phone(cls, phone):
with client.context():
return cls.query(cls.phone == phone).get()
@classmethod
def get_by_mail_token(cls, mail_token):
with client.context():
return cls.query(cls.mail_token == mail_token).get()
@classmethod
def get_by_uid(cls, uid):
with client.context():
return cls.query(cls.uid == uid).get()
@classmethod
def get_by_token(cls, api_token):
with client.context():
return cls.query(cls.api_token == api_token).get()
# search indexes or collections
class Sidekick(ndb.Model):
uid = ndb.StringProperty() # user
name = ndb.StringProperty() # name
nick = ndb.StringProperty() # nick
updated = ndb.DateTimeProperty() # creation time
solr_cluster = ndb.StringProperty() # url to talk to solr
mood = ndb.StringProperty() # sidekick's mood
# instance method for the number of docs in the sidekick
def numdocs(self):
return doc_count(self.name)
@classmethod
def get_all(cls, uid):
with client.context():
return cls.query(cls.uid == uid).fetch(3) # set this in config.py TODO
@classmethod
def get_by_uid_name(cls, uid, name):
# query number of documents
with client.context():
return cls.query(cls.uid == uid, cls.name == name).get()
@classmethod
def get_by_uid_nick(cls, uid, nick):
with client.context():
return cls.query(cls.uid == uid, cls.nick == nick).get()
@classmethod
def create(cls, uid=None):
# new name
name = "sidekick_%s" % random_string(size=13)
nick = random_name(2)
with client.context():
cls(
uid = uid,
name = name,
nick = nick,
updated = datetime.datetime.utcnow(),
solr_cluster = "cluster-one-not-used"
).put()
return cls.query(cls.uid == uid, cls.name == name).get()
# user settings
class Settings(ndb.Model):
uid = ndb.StringProperty() # user
name = ndb.StringProperty() # name
value = ndb.JsonProperty() # value or json object
updated = ndb.DateTimeProperty()
@classmethod
def create(cls, uid=None, name="var1", value=[], methods=['GET','POST']):
with client.context():
cls(
uid = uid,
name = name,
value = value,
updated = datetime.datetime.utcnow()
).put()
return cls.query(cls.uid == uid, cls.name == name).get()
@classmethod
def get_by_uid_name(cls, uid, name):
with client.context():
return cls.query(cls.uid == uid, cls.name == name).get()
@classmethod
def get_all(cls, uid):
with client.context():
return cls.query(cls.uid == uid).fetch(10) # needs to be set in config.py TODO
# image spools (upload & crawl creates)
class Spool(ndb.Model):
created = ndb.DateTimeProperty()
updated = ndb.DateTimeProperty()
sidekick = ndb.StringProperty()
url = ndb.StringProperty() # url
uid = ndb.StringProperty() # user
name = ndb.StringProperty() # reference name
nick = ndb.StringProperty() # user friendly name
upload_url = ndb.StringProperty() # upload target
title = ndb.TextProperty() # title
description = ndb.TextProperty() # description
tags = ndb.JsonProperty() # tags for spool
filenames = ndb.JsonProperty() # list of files
@classmethod
def get_by_uid_name(cls, uid, name):
with client.context():
return cls.query(cls.uid == uid, cls.name == name).get()
@classmethod
def get_by_uid_nick(cls, uid, nick):
with client.context():
return cls.query(cls.uid == uid, cls.nick == nick).get()
@classmethod
def get_by_uid_url(cls, uid, url):
with client.context():
return cls.query(cls.uid == uid, cls.url == url).get()
@classmethod
def get_all(cls, uid):
with client.context():
return cls.query(cls.uid == uid).fetch(100)
@classmethod
def create(
cls,
uid = uid,
url = url,
title = title,
description = description,
tags = tags
):
# new spool names
name = "spool_%s" % random_string(size=13)
nick = random_name(3)
# if no URL, use the public URL for the spool
if url == "":
url = "%sd/%s" % (config.website_url, name)
# build the upload url
grub_url = "https://mitta.us/" # target system
upload_url = "%su/%s" % (grub_url, name)
with client.context():
cls(
created = datetime.datetime.utcnow(),
updated = datetime.datetime.utcnow(),
uid = uid,
name = name,
nick = nick,
upload_url = upload_url,
url = url,
title = title,
description = description,
tags = tags,
filenames = []
).put()
return cls.query(cls.name == name).get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment