def fetch_all(cls): db = get_db() messages_ref = db.collection(u'messages') # a reference to the messages collection # messages generator: holds all message documents (these documents need to be converted to dicts) messages_gen = messages_ref.stream() messages = [] for message in messages_gen: message_dict = message.to_dict() # converting DocumentSnapshot into a dictionary message_dict["id"] = message.id # adding message ID to the dict, because it's not there by default messages.append(message_dict) # appending the message dict to the messages list return messages
def create(cls, text): db = get_db() messages_ref = db.collection(u'messages') # a reference to the messages collection message_ref = messages_ref.document() # create a message document reference # now you can create or update the message document (set: if it exists, update it. If not, create a new one). message_ref.set({ u'text': u'{}'.format(text), # we could also name this smth else, like "text", to avoid confusion u'created': datetime.datetime.now(), # you could add other fields here, like "author", "email" etc. }) # create message dict message_dict = message_ref.get().to_dict() message_dict["id"] = message_ref.id # add ID to the message dict (because it's not added automatically) return message_dict
def get(cls, name): db = get_db() col_ref = db.collection( u'envvars') # a reference to the vars collection env_var_ref = col_ref.document(u"{}".format( name.upper())) # get the env var document try: env_var = env_var_ref.get() env_var_dict = env_var.to_dict( ) # convert env var document to dictionary env_var_dict["id"] = env_var.id # add ID to the dictionary return env_var_dict except Exception as e: logging.warning(e) return None
def create(cls, name, value): db = get_db() col_ref = db.collection( u'envvars') # a reference to the vars collection env_var_ref = col_ref.document(u"{}".format( name.upper())) # create an env var document reference # now you can create or update the env var document (set: if it exists, update it. If not, create a new one). env_var_ref.set({ u'name': u'{}'.format(name.upper()), # env vars should be always uppercase u'value': u'{}'.format(value), }) # create env var dict env_var_dict = env_var_ref.get().to_dict() env_var_dict[ "id"] = env_var_ref.id # add ID to the env var dict (because it's not added automatically) return env_var_dict
def fetch_all(cls): db = get_db() col_ref = db.collection( u'envvars') # a reference to the vars collection # collection generator: holds all env var documents (these documents need to be converted to dicts) col_gen = col_ref.stream() env_vars = [] for env_var in col_gen: env_var_dict = env_var.to_dict( ) # converting DocumentSnapshot into a dictionary env_var_dict[ "id"] = env_var.id # adding env var ID to the dict, because it's not there by default env_vars.append( env_var_dict ) # appending the env var dict to the env vars list return env_vars
import datetime import hashlib import secrets import bcrypt from operator import attrgetter from google.cloud import ndb from models.db_settings import get_db client = get_db() class Session(ndb.Model): token_hash = ndb.StringProperty() ip = ndb.StringProperty() platform = ndb.StringProperty() browser = ndb.StringProperty() city = ndb.StringProperty() country = ndb.StringProperty() user_agent = ndb.StringProperty() expired = ndb.DateTimeProperty() class CSRFToken(ndb.Model): """CSRF token (also called XSRF) is a mechanism that prevents CSRF attacks.""" token = ndb.StringProperty() expired = ndb.DateTimeProperty() class User(ndb.Model): first_name = ndb.StringProperty()
from models.db_settings import get_db db = get_db() class Message(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False) def __repr__(self): return '<Message %r>' % self.title @classmethod def create(cls, text): message = Message(text=text) db.add(message) db.commit() return message @classmethod def fetch_all(cls): messages = db.query(Message).all() return messages # this is needed to create the table in the database db.create_all()