<commit_before>#!/usr/bin/env python

import unittest
import json
import sqlalchemy
import datetime
import paasmaker
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.orm.interfaces import MapperExtension

Base = declarative_base()

# TODO: Revisit this: DateTime handling: insert UTC timestamps.

# Helper function to return the time in UTC.
def now():
	return datetime.datetime.utcnow()

class OrmExtension(MapperExtension):
	def before_update(self, mapper, connection, instance):
		instance.updated = now()

class OrmBase(object):
	__mapper_args__ = { 'extension': OrmExtension() }
	created = Column(DateTime, nullable=False, default=now)
	deleted = Column(DateTime, nullable=True, default=None, index=True)
	updated = Column(DateTime, nullable=False, default=now)

	def flatten(self, field_list=None):
		# If field_list is not None, return just those fields.
		fields = {}
		fields['id'] = self.__dict__['id']
		fields['updated'] = self.__dict__['updated']
		fields['created'] = self.__dict__['created']
		fields['class'] = self.__class__.__name__
		for field in field_list:
			fields[field] = self.__dict__[field]
		return fields

	def delete(self):
		"""Mark the object as deleted. You still need to save it."""
		self.deleted = now()

class Node(OrmBase, Base):
	__tablename__ = 'node'

	id = Column(Integer, primary_key=True)
	name = Column(String, nullable=False)
	route = Column(String, nullable=False)
	uuid = Column(String, nullable=False, unique=True, index=True)
	state = Column(String, nullable=False)
	last_heard = Column(DateTime, nullable=False)

	def __init__(self, name, route, uuid, state):
		self.name = name
		self.route = route
		self.uuid = uuid
		self.state = state
		self.last_heard = datetime.datetime.utcnow()

	def __repr__(self):
		return "<Node('%s','%s')>" % (self.name, self.route)

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['name', 'route', 'uuid', 'state', 'last_heard'])

class User(OrmBase, Base):
	__tablename__ = 'user'

	id = Column(Integer, primary_key=True)
	username = Column(String, nullable=False, index=True)
	auth_source = Column(String, nullable=False, default="internal")
	auth_meta = Column(String, nullable=True)

	password = Column(String, nullable=True)
	name = Column(String, nullable=True)

	def __init__(self, username, auth_source="internal"):
		self.username = username
		self.auth_source = auth_source

	def __repr__(self):
		return "<User('%s'@'%s')>" % (self.username, self.auth_source)

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['username', 'auth_source', 'name'])

class Role(OrmBase, Base):
	__tablename__ = 'role'

	id = Column(Integer, primary_key=True)
	name = Column(String, nullable=False, unique=True)

	def __init__(self, name):
		self.name = name

	def __repr__(self):
		return "<Role('%s')>" % self.name

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['name', 'permissions'])<commit_msg>Updated the user model to be keyed by an email address instead of a username. This may come back to bite me later...<commit_after>#!/usr/bin/env python

import unittest
import json
import sqlalchemy
import datetime
import paasmaker
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.orm.interfaces import MapperExtension

Base = declarative_base()

# TODO: Revisit this: DateTime handling: insert UTC timestamps.

# Helper function to return the time in UTC.
def now():
	return datetime.datetime.utcnow()

class OrmExtension(MapperExtension):
	def before_update(self, mapper, connection, instance):
		instance.updated = now()

class OrmBase(object):
	__mapper_args__ = { 'extension': OrmExtension() }
	created = Column(DateTime, nullable=False, default=now)
	deleted = Column(DateTime, nullable=True, default=None, index=True)
	updated = Column(DateTime, nullable=False, default=now)

	def flatten(self, field_list=None):
		# If field_list is not None, return just those fields.
		fields = {}
		fields['id'] = self.__dict__['id']
		fields['updated'] = self.__dict__['updated']
		fields['created'] = self.__dict__['created']
		fields['class'] = self.__class__.__name__
		for field in field_list:
			fields[field] = self.__dict__[field]
		return fields

	def delete(self):
		"""Mark the object as deleted. You still need to save it."""
		self.deleted = now()

class Node(OrmBase, Base):
	__tablename__ = 'node'

	id = Column(Integer, primary_key=True)
	name = Column(String, nullable=False)
	route = Column(String, nullable=False)
	uuid = Column(String, nullable=False, unique=True, index=True)
	state = Column(String, nullable=False)
	last_heard = Column(DateTime, nullable=False)

	def __init__(self, name, route, uuid, state):
		self.name = name
		self.route = route
		self.uuid = uuid
		self.state = state
		self.last_heard = datetime.datetime.utcnow()

	def __repr__(self):
		return "<Node('%s','%s')>" % (self.name, self.route)

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['name', 'route', 'uuid', 'state', 'last_heard'])

class User(OrmBase, Base):
	__tablename__ = 'user'

	id = Column(Integer, primary_key=True)
	email = Column(String, nullable=False, index=True)
	auth_source = Column(String, nullable=False, default="internal")
	auth_meta = Column(String, nullable=True)

	password = Column(String, nullable=True)
	name = Column(String, nullable=True)

	def __init__(self, email, auth_source="internal"):
		self.email = email
		self.auth_source = auth_source

	def __repr__(self):
		return "<User('%s'@'%s')>" % (self.email, self.auth_source)

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['email', 'auth_source', 'name'])

class Role(OrmBase, Base):
	__tablename__ = 'role'

	id = Column(Integer, primary_key=True)
	name = Column(String, nullable=False, unique=True)

	def __init__(self, name):
		self.name = name

	def __repr__(self):
		return "<Role('%s')>" % self.name

	def flatten(self, field_list=None):
		return super(Node, self).flatten(['name', 'permissions'])
{'fraction_non_alphanumeric': 0.13377714376287841, 'fraction_numerical': 0.0, 'mean_word_length': 5.677248677248677, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7105668', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2055, 'n_words': 543}