<commit_before>import os
from datetime import datetime
from uuid import uuid4

from flask import current_app

from ..core import db, JsonMixin


class SimJsonSerializer(JsonMixin):
    __hidden_fields__ = set(['stage_dir'])


class Sim(SimJsonSerializer, db.Model):
    __tablename__ = 'submission'
    __bind_key__ = 'sims'

    id = db.Column(db.Integer, primary_key=True)
    model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
    uuid = db.Column(db.Text)
    name = db.Column(db.Text)
    status = db.Column(db.Text)
    created = db.Column(db.Text)
    updated = db.Column(db.Text)
    message = db.Column(db.Text)
    owner = db.Column(db.Integer, db.ForeignKey('users.id'))
    stage_dir = db.Column(db.Text)

    @property
    def url_for(self):
        return url_for('.sim', id=self.id)

    def __init__(self, name, model_id, owner=None):
        now = datetime.now().isoformat()

        self.model_id = model_id
        self.uuid = str(uuid4())
        self.name = name
        self.status = 'submitted'
        self.message = 'Run has been submitted'
        self.created = now
        self.updated = now
        self.owner = owner or ""
        self.stage_dir = os.path.join(current_app.config['STAGE_DIR'],
                                      self.uuid)

        self._create_stage_dir()

    def __repr__(self):
        return '<Sim %r>' % self.name

    def _create_stage_dir(self):
        try:
            os.mkdir(self.stage_dir)
        except OSError:
            current_app.config['log'].warning(
                '%s: Stage directory already exists' % self.stage_dir)

        #write_readme(self.stage_dir, mode='w', params={
        #    'user': 'anonymous',
        #    'staged_on': datetime.now().isoformat()
        #})

    def update_status(self, sim, status=None, message=None):
        updates = dict(updated=datetime.now().isoformat())
        if status is not None:
            updates['status'] = status
        if message is not None:
            updates['message'] = status

        self.update(sim, **updates)

        return sim<commit_msg>Added link_objects and href properties to Sim.<commit_after>import os
from datetime import datetime
from uuid import uuid4

from flask import current_app, url_for

from ..core import db, JsonMixin


class SimJsonSerializer(JsonMixin):
    __hidden_fields__ = set(['stage_dir'])
    __public_fields__ = set(['id', 'name', 'status', 'owner', 'message',
                             'href'])


class Sim(SimJsonSerializer, db.Model):
    __tablename__ = 'submission'
    __bind_key__ = 'sims'

    id = db.Column(db.Integer, primary_key=True)
    model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
    uuid = db.Column(db.Text)
    name = db.Column(db.Text)
    status = db.Column(db.Text)
    created = db.Column(db.Text)
    updated = db.Column(db.Text)
    message = db.Column(db.Text)
    owner = db.Column(db.Integer, db.ForeignKey('users.id'))
    stage_dir = db.Column(db.Text)

    @property
    def href(self):
        return url_for('sims.sim', id=self.id)

    @property
    def link_objects(self):
        return {
            'user': {'href': url_for('users.user', id=self.owner)},
            'model': {'href': url_for('models.model', id=self.model_id)},
        }

    def __init__(self, name, model_id, owner=None):
        now = datetime.now().isoformat()

        self.model_id = model_id
        self.uuid = str(uuid4())
        self.name = name
        self.status = 'submitted'
        self.message = 'Run has been submitted'
        self.created = now
        self.updated = now
        self.owner = owner or 0
        self.stage_dir = os.path.join(current_app.config['STAGE_DIR'],
                                      self.uuid)

        self._create_stage_dir()

    def __repr__(self):
        return '<Sim %r>' % self.name

    def _create_stage_dir(self):
        try:
            os.mkdir(self.stage_dir)
        except OSError:
            current_app.config['log'].warning(
                '%s: Stage directory already exists' % self.stage_dir)

        #write_readme(self.stage_dir, mode='w', params={
        #    'user': 'anonymous',
        #    'staged_on': datetime.now().isoformat()
        #})

    def update_status(self, sim, status=None, message=None):
        updates = dict(updated=datetime.now().isoformat())
        if status is not None:
            updates['status'] = status
        if message is not None:
            updates['message'] = status

        self.update(sim, **updates)

        return sim
{'fraction_non_alphanumeric': 0.12782780584230177, 'fraction_numerical': 0.0010981770261366132, 'mean_word_length': 2.346069066862601, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3186949', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1405, 'n_words': 308}