<commit_before>import app
import math
from sqlalchemy import and_

class Mapping(app.db.Model):
    id = app.db.Column(app.db.Integer, primary_key=True)
    creation_time = app.db.Column(app.db.DateTime)
    name = app.db.Column(app.db.String(120))
    name_route = app.db.Column(app.db.String(140))
    kaizen_user_id = app.db.Column(app.db.Integer, app.db.ForeignKey('kaizen_user.id'), index=True)
    binding = app.db.Column(app.db.SmallInteger)
    notes = app.db.relationship('Note', backref='mapping', lazy='dynamic')
    deleted = app.db.Column(app.db.Boolean)

    def __init__(self, name, kaizen_user_id, binding, deleted=False):
        self.name = name.title()
        self.kaizen_user_id = kaizen_user_id
        self.binding = binding
        self.creation_time = app.utility.get_time()
        self.deleted = deleted
        self.name_route = app.models.kaizen_user.dashify_name(self.name)

    def set_binding(self, binding):
        self.binding = binding
        app.models.sql.commit()

    def add_note(self, data, keyCode):
        text = app.utility.decodeJS(data['text'])
        title = app.utility.decodeJS(data['title'])
        href = app.utility.decodeJS(data['href'])
        app.models.note.create_note(text, title, href, self.id, keyCode)

    def get_all_live_notes(self):
        return [n for n in self.notes if n.deleted==False]

    def get_all_live_sites(self):
        return list(set([n.site for n in self.notes if n.deleted==False]))

    def has_notes(self):
        for n in self.notes.filter(app.models.note.Note.deleted==False):
            return True
        return False

    def time_descending_notes(self, limit=10):
        return self.notes.filter(
            app.models.note.Note.deleted==False).order_by(
            app.models.note.Note.creation_time.desc()).limit(limit)

    def last_note_time(self):
        for n in self.time_descending_notes(1):
            return n.creation_time
        return app.utility.start_date

    def serialize(self, include_notes=False, include_user=False):
        ret = {'mid'   : self.id,
               'name' : self.name}
        if include_user:
            ret['uid'] = self.kaizen_user_id
        if include_notes:
            ret['notes'] = [note.serialize() for note in self.notes]
        return ret

    def __repr__(self):
        return self.name

def create_mapping(kaizen_user_id, name, binding=-1):
    mapping = Mapping(name=name, binding=binding, kaizen_user_id=kaizen_user_id)
    app.models.sql.add(mapping)
    return mapping

def json_mappings(maps):
    ret = {}
    for m in maps:
        if m.binding < 0:
            continue
        ret[m.binding] = m.name
    return ret

def mapping_with_userid_and_name(uid, name):
    filtered = Mapping.query.filter(and_(Mapping.name==name, Mapping.kaizen_user_id==uid)).all()
    if len(filtered) == 1:
        return filtered[0]
    return None

def mapping_with_userid_and_name_route(uid, name_route):
    filtered = Mapping.query.filter(and_(Mapping.name_route==name_route, Mapping.kaizen_user_id==uid)).all()
    if len(filtered) == 1:
        return filtered[0]
    return None

def mapping_with_id(id):
    return Mapping.query.get(id)

def visualize(m):<commit_msg>mapping: allow for malformed url strings<commit_after>import app
import math
from sqlalchemy import and_

class Mapping(app.db.Model):
    id = app.db.Column(app.db.Integer, primary_key=True)
    creation_time = app.db.Column(app.db.DateTime)
    name = app.db.Column(app.db.String(120))
    name_route = app.db.Column(app.db.String(140))
    kaizen_user_id = app.db.Column(app.db.Integer, app.db.ForeignKey('kaizen_user.id'), index=True)
    binding = app.db.Column(app.db.SmallInteger)
    notes = app.db.relationship('Note', backref='mapping', lazy='dynamic')
    deleted = app.db.Column(app.db.Boolean)

    def __init__(self, name, kaizen_user_id, binding, deleted=False):
        self.name = name.title()
        self.kaizen_user_id = kaizen_user_id
        self.binding = binding
        self.creation_time = app.utility.get_time()
        self.deleted = deleted
        self.name_route = app.models.kaizen_user.dashify_name(self.name)

    def set_binding(self, binding):
        self.binding = binding
        app.models.sql.commit()

    def add_note(self, data, keyCode):
        text = app.utility.decodeJS(data['text'])
        title = app.utility.decodeJS(data['title'])
        href = app.utility.decodeJS(data['href'])
        app.models.note.create_note(text, title, href, self.id, keyCode)

    def get_all_live_notes(self):
        return [n for n in self.notes if n.deleted==False]

    def get_all_live_sites(self):
        return list(set([n.site for n in self.notes if n.deleted==False]))

    def has_notes(self):
        for n in self.notes.filter(app.models.note.Note.deleted==False):
            return True
        return False

    def time_descending_notes(self, limit=10):
        return self.notes.filter(
            app.models.note.Note.deleted==False).order_by(
            app.models.note.Note.creation_time.desc()).limit(limit)

    def last_note_time(self):
        for n in self.time_descending_notes(1):
            return n.creation_time
        return app.utility.start_date

    def serialize(self, include_notes=False, include_user=False):
        ret = {'mid'   : self.id,
               'name' : self.name}
        if include_user:
            ret['uid'] = self.kaizen_user_id
        if include_notes:
            ret['notes'] = [note.serialize() for note in self.notes]
        return ret

    def __repr__(self):
        return self.name

def create_mapping(kaizen_user_id, name, binding=-1):
    mapping = Mapping(name=name, binding=binding, kaizen_user_id=kaizen_user_id)
    app.models.sql.add(mapping)
    return mapping

def json_mappings(maps):
    ret = {}
    for m in maps:
        if m.binding < 0:
            continue
        ret[m.binding] = m.name
    return ret

def mapping_with_userid(uid):
    return Mapping.query.filter(Mapping.kaizen_user_id==uid).all()

def mapping_with_userid_and_name(uid, name):
    filtered = [m for m in mapping_with_userid(uid) if m.name.lower() == name.lower()]
    if len(filtered) == 1:
        return filtered[0]
    return None

def mapping_with_userid_and_name_route(uid, name_route):
    filtered = [m for m in mapping_with_userid(uid) if m.name_route.lower() == name_route.lower()]
    if len(filtered) == 1:
        return filtered[0]
    return None

def mapping_with_id(id):
    return Mapping.query.get(id)

def visualize(m):
{'fraction_non_alphanumeric': 0.12078694524935184, 'fraction_numerical': 0.0045752630776269634, 'mean_word_length': 2.9577549788774893, 'pattern_counts': {'":': 0, '<': 5, '<?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': '783038', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2148, 'n_words': 424}