<commit_before>import datetime
from flask import url_for
from extensions import db
from helpers import slugify
import json
from decimal import Decimal

from collections import OrderedDict


class Organization(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    username = db.Column(db.String(50), unique=True)
    slug = db.Column(db.String(50), unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.username)
        super(Organization, self).save(*args, **kwargs)

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

class Repository(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    gh_id = db.Column(db.Integer)
    name = db.Column(db.String(50))
    forks = db.Column(db.Integer)
    watchers = db.Column(db.Integer)
    size = db.Column(db.Integer)
    open_issues = db.Column(db.Integer)
    description = db.Column(db.Text)
    organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'))
    organization = db.relationship('Organization',
        backref=db.backref('repositories', lazy='dynamic'))
    users = db.relationship("User", secondary="commit")
    slug = db.Column(db.String(50), unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Repository, self).save(*args, **kwargs)

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

class Commit(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sha = db.Column(db.String(70))
    message = db.Column(db.Text)
    date = db.Column(db.DateTime)
    repository_id = db.Column(db.Integer, db.ForeignKey('repository.id'))
    repository = db.relationship('Repository',
        backref=db.backref('commits', lazy='dynamic'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User',
        backref=db.backref('commits', lazy='dynamic'))

    def __repr__(self):
        return '<Commit %s>' % self.message

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    gh_id = db.Column(db.Integer)
    login = db.Column(db.String(70))
    avatar_url = db.Column(db.String(255))
    slug = db.Column(db.String(50), unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.login)
        super(User, self).save(*args, **kwargs)


    def __repr__(self):
        return '<User %s>' % self.login
<commit_msg>Listen to events to slug the model<commit_after>import datetime
from flask import url_for
from extensions import db
from helpers import slugify
import json
from decimal import Decimal
from sqlalchemy import event

from collections import OrderedDict


class Organization(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    username = db.Column(db.String(50), unique=True)
    slug = db.Column(db.String(100), unique=True)

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

class Repository(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    gh_id = db.Column(db.Integer)
    name = db.Column(db.String(50))
    forks = db.Column(db.Integer)
    watchers = db.Column(db.Integer)
    size = db.Column(db.Integer)
    open_issues = db.Column(db.Integer)
    description = db.Column(db.Text)
    organization_id = db.Column(db.Integer, db.ForeignKey('organization.id'))
    organization = db.relationship('Organization',
        backref=db.backref('repositories', lazy='dynamic'))
    users = db.relationship("User", secondary="commit")
    slug = db.Column(db.String(100), unique=True)

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

class Commit(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sha = db.Column(db.String(70))
    message = db.Column(db.Text)
    date = db.Column(db.DateTime)
    repository_id = db.Column(db.Integer, db.ForeignKey('repository.id'))
    repository = db.relationship('Repository',
        backref=db.backref('commits', lazy='dynamic'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User',
        backref=db.backref('commits', lazy='dynamic'))

    def __repr__(self):
        return '<Commit %s>' % self.message

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    gh_id = db.Column(db.Integer)
    login = db.Column(db.String(70))
    avatar_url = db.Column(db.String(255))
    slug = db.Column(db.String(70), unique=True)

    def __repr__(self):
        return '<User %s>' % self.login

def add_slug(mapper, connection, target):
    if isinstance(target, Organization):
        target.slug = slugify(target.name)
    elif isinstance(target, Repository):
        target.slug = slugify("-".join([target.organization.username,target.name]))
    elif isinstance(target, User):
        target.slug = slugify(target.login)

event.listen(Organization, 'before_insert', add_slug)
event.listen(Organization, 'before_update', add_slug)
event.listen(Repository, 'before_insert', add_slug)
event.listen(Repository, 'before_update', add_slug)
event.listen(User, 'before_insert', add_slug)
event.listen(User, 'before_update', add_slug)
{'fraction_non_alphanumeric': 0.14505579068872643, 'fraction_numerical': 0.008080030781069641, 'mean_word_length': 3.836279069767442, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2907746', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1681, 'n_words': 338}