<commit_before>import json
from flask.ext.script import Manager
from flask.ext.assets import ManageAssets

from datawire.core import app, db, assets
from datawire.model import User, Service, Event
from datawire.views import index
from datawire.logs import logger

manager = Manager(app)
manager.add_command("assets", ManageAssets(assets))


@manager.command
def createdb():
    """ Create the database entities. """
    db.create_all()
    admin_data = {'screen_name': 'admin', 'name': 'Systems Admin'}
    if User.by_screen_name(admin_data.get('screen_name')) is None:
        user = User.create(admin_data)
        db.session.flush()
        user.api_key = app.config.get('SECRET_KEY')
    db.session.commit()


@manager.command
def createservice(filename):
    """ Load a service configuration form a JSON configuration. """
    with open(filename, 'rb') as fh:
        data = json.load(fh)
        events = data.pop('events', [])
        service = Service.by_key(data.get('key'))
        if service is not None:
            raise ValueError("Service already exists: %s" % data.get('key'))
        service = Service.create(data)
        for event_data in events:
            event_data['service'] = service
            Event.create(event_data)
        user = User.by_screen_name('admin')
        service.editors.append(user)
        db.session.commit()


@manager.command
def updateservice(filename):
    """ Update a service configuration form a JSON configuration. """
    with open(filename, 'rb') as fh:
        data = json.load(fh)
        service = Service.by_key(data.get('key'))
        if service is None:
            raise ValueError("Service doesn't exist: %s" % data.get('key'))
        service.update(data)
        events = []
        for event_data in data.get('events', []):
            event_data['service'] = service
            event = Event.by_key(service, event_data['key'])
            if event is None:
                event = Event.create(event_data)
            else:
                event.update(event_data)
            events.append(event)
        service.events = events
        db.session.commit()


@manager.command
def deleteservice(key):
    """ Delete a service configuration from the database. """
    service = Service.by_key(key)
    if service is None:
        raise ValueError("Service doesn't exist: %s" % key)
    db.session.delete(service)
    db.session.commit()


@manager.command
def match(urn):
    """ Test entity matching. """<commit_msg>Access control via manage command service updates. <commit_after>import json
from flask.ext.script import Manager
from flask.ext.assets import ManageAssets

from datawire.core import app, db, assets
from datawire.model import User, Service, Event
from datawire.views import index
from datawire.logs import logger

manager = Manager(app)
manager.add_command("assets", ManageAssets(assets))


@manager.command
def createdb():
    """ Create the database entities. """
    db.create_all()
    admin_data = {'screen_name': 'admin', 'name': 'Systems Admin'}
    if User.by_screen_name(admin_data.get('screen_name')) is None:
        user = User.create(admin_data)
        db.session.flush()
        user.api_key = app.config.get('SECRET_KEY')
    db.session.commit()


@manager.command
def createservice(filename):
    """ Load a service configuration form a JSON configuration. """
    with open(filename, 'rb') as fh:
        data = json.load(fh)
        events = data.pop('events', [])
        editors = data.pop('editors', [1])
        service = Service.by_key(data.get('key'))
        if service is not None:
            raise ValueError("Service already exists: %s" % data.get('key'))
        service = Service.create(data)
        for event_data in events:
            event_data['service'] = service
            Event.create(event_data)
        for editor_id in editors:
            user = User.by_id(editor_id)
            service.editors.append(user)
        db.session.commit()


@manager.command
def updateservice(filename):
    """ Update a service configuration form a JSON configuration. """
    with open(filename, 'rb') as fh:
        data = json.load(fh)
        service = Service.by_key(data.get('key'))
        if service is None:
            raise ValueError("Service doesn't exist: %s" % data.get('key'))
        service.update(data)
        events = []
        for event_data in data.get('events', []):
            event_data['service'] = service
            event = Event.by_key(service, event_data['key'])
            if event is None:
                event = Event.create(event_data)
            else:
                event.update(event_data)
            events.append(event)
        service.events = events
        service.editors = []
        for editor_id in data.get('editors', [1]):
            user = User.by_id(editor_id)
            service.editors.append(user)
        db.session.commit()


@manager.command
def deleteservice(key):
    """ Delete a service configuration from the database. """
    service = Service.by_key(key)
    if service is None:
        raise ValueError("Service doesn't exist: %s" % key)
    db.session.delete(service)
    db.session.commit()


@manager.command
def match(urn):
    """ Test entity matching. """
{'fraction_non_alphanumeric': 0.12033511043412033, 'fraction_numerical': 0.0003808073115003808, 'mean_word_length': 2.7019027484143763, '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': '3903191', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1485, 'n_words': 397}