<commit_before>import datetime
from sqlalchemy import desc

from application import db

class Page(db.Model):
	__tablename__ = 'page'

	id = db.Column(db.Integer, primary_key=True)
	path = db.Column(db.String(256), unique=True)
	revisions = db.relationship('PageRevision', backref='page', lazy='dynamic')

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

	def get_most_recent_revision(self):
		return PageRevision.query.filter(PageRevision.page_id==self.id).order_by(PageRevision.timestamp.desc()).first()

class PageRevision(db.Model):
	__tablename__ = 'page_revision'

	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(128))
	content = db.Column(db.Text)
	timestamp = db.Column(db.DateTime)
	user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
	page_id = db.Column(db.Integer, db.ForeignKey('page.id'))

	def __init__(self, page, author, title, content,
		timestamp=datetime.datetime.utcnow()):
		self.title = title
		self.path = ''
		self.content = content
		self.user_id = author.id
		self.page_id = page.id
		self.timestamp = timestamp

class PagePermission(db.Model):
	__tablename__ = 'page_permission'

	id = db.Column(db.Integer, primary_key=True)
	view = db.Column(db.Boolean)
	create = db.Column(db.Boolean)
	edit = db.Column(db.Boolean)
	delete = db.Column(db.Boolean)
	page_id = db.Column(db.Integer, db.ForeignKey('page.id'))
	group_id = db.Column(db.Integer, db.ForeignKey('group.id'))

	def __init__(self, group, page, view=False, create=False, edit=False,
		delete=False):
		self.view = view
		self.create = create
		self.edit = edit
		self.delete = delete
		self.group_id = group.id
		self.page_id = page.id

	@staticmethod
	def get_rights(user, page_path):
		rights = []
		groups = user.groups.all()

		for group in groups:
			current_path = page_path

			while True:
				page = Page.query.filter(Page.path==current_path)

				if page:
					permissions = group.page_permissions.filter(Page.id==page.id).first()

					if permissions:
						if permissions.view and not 'view' in rights:
							rights.append('view')

						if permissions.create and not 'create' in rights:
							rights.append('create')

						if permissions.edit and not 'edit' in rights:
							rights.append('edit')

						if permissions.delete and not 'delete' in rights:
							rights.append('delete')

						break
<commit_msg>Yes, you can't operate on query objects :/.<commit_after>import datetime
from sqlalchemy import desc

from application import db

class Page(db.Model):
	__tablename__ = 'page'

	id = db.Column(db.Integer, primary_key=True)
	path = db.Column(db.String(256), unique=True)
	revisions = db.relationship('PageRevision', backref='page', lazy='dynamic')

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

	def get_most_recent_revision(self):
		return PageRevision.query.filter(PageRevision.page_id==self.id).order_by(PageRevision.timestamp.desc()).first()

class PageRevision(db.Model):
	__tablename__ = 'page_revision'

	id = db.Column(db.Integer, primary_key=True)
	title = db.Column(db.String(128))
	content = db.Column(db.Text)
	timestamp = db.Column(db.DateTime)
	user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
	page_id = db.Column(db.Integer, db.ForeignKey('page.id'))

	def __init__(self, page, author, title, content,
		timestamp=datetime.datetime.utcnow()):
		self.title = title
		self.path = ''
		self.content = content
		self.user_id = author.id
		self.page_id = page.id
		self.timestamp = timestamp

class PagePermission(db.Model):
	__tablename__ = 'page_permission'

	id = db.Column(db.Integer, primary_key=True)
	view = db.Column(db.Boolean)
	create = db.Column(db.Boolean)
	edit = db.Column(db.Boolean)
	delete = db.Column(db.Boolean)
	page_id = db.Column(db.Integer, db.ForeignKey('page.id'))
	group_id = db.Column(db.Integer, db.ForeignKey('group.id'))

	def __init__(self, group, page, view=False, create=False, edit=False,
		delete=False):
		self.view = view
		self.create = create
		self.edit = edit
		self.delete = delete
		self.group_id = group.id
		self.page_id = page.id

	@staticmethod
	def get_rights(user, page_path):
		rights = []
		groups = user.groups.all()

		for group in groups:
			current_path = page_path

			while True:
				page = Page.query.filter(Page.path==current_path).first()

				if page:
					permissions = group.page_permissions.filter(Page.id==page.id).first()

					if permissions:
						if permissions.view and not 'view' in rights:
							rights.append('view')

						if permissions.create and not 'create' in rights:
							rights.append('create')

						if permissions.edit and not 'edit' in rights:
							rights.append('edit')

						if permissions.delete and not 'delete' in rights:
							rights.append('delete')

						break

{'fraction_non_alphanumeric': 0.1328819297503174, 'fraction_numerical': 0.0025391451544646637, 'mean_word_length': 4.099244875943905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2069536', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1586, 'n_words': 341}