Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from datagovuk/move-model-setup-to-command
Browse files Browse the repository at this point in the history
Move model setup to a command
  • Loading branch information
David Read committed Dec 14, 2015
2 parents dde5c8d + f570666 commit 4885e59
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 137 deletions.
36 changes: 36 additions & 0 deletions ckanext/issues/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from ckan.lib.cli import CkanCommand

import logging
import sys

from pylons import config

class Issues(CkanCommand):
"""
Usage:
paster issues init_db
- Creates the database table issues needs to run
"""
summary = __doc__.split('\n')[0]
usage = __doc__

def command(self):
"""
Parse command line arguments and call appropriate method.
"""
if not self.args or self.args[0] in ['--help', '-h', 'help']:
print self.usage
sys.exit(1)

cmd = self.args[0]
self._load_config()

self.log = logging.getLogger(__name__)

if cmd == 'init_db':
from ckanext.issues.model import setup
setup()
self.log.info('Issues tables are initialized')
else:
self.log.error('Command %s not recognized' % (cmd,))
234 changes: 107 additions & 127 deletions ckanext/issues/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,11 @@

log = logging.getLogger(__name__)

issue_table = None
issue_category_table = None
issue_comment_table = None
issue_report_table = None
issue_comment_report_table = None


def setup():
"""
Called at the end of CKAN setup.
Create issue and issue_category tables in the database.
Prepopulate issue_category table with default categories.
"""
if issue_table is None:
define_issue_tables()
report_tables = define_report_tables([Issue, IssueComment])
log.debug('Issue tables defined in memory')

if not model.package_table.exists():
# during tests?
return
Expand Down Expand Up @@ -456,116 +442,110 @@ def clear_all_abuse_reports(self, session):
return self


def define_issue_tables():
global issue_category_table
global issue_table
global issue_comment_table
global issue_report_table
global issue_comment_report_table

issue_category_table = Table(
'issue_category',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('name', types.Unicode(ISSUE_CATEGORY_NAME_MAX_LENGTH),
nullable=False, unique=True),
Column('description', types.Unicode, nullable=False, unique=False),
Column('created', types.DateTime, default=datetime.now,
nullable=False))

# TODO get rid of these foreign key relations - not allowed for extensions
issue_table = Table(
'issue',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('number', types.Integer, nullable=False),
Column('title', types.UnicodeText, nullable=False),
Column('description', types.UnicodeText),
Column('dataset_id', types.UnicodeText, nullable=False),
Column('resource_id', types.UnicodeText),
Column('user_id', types.UnicodeText, nullable=False),
Column('assignee_id', types.UnicodeText),
Column('status', types.String(15), default=ISSUE_STATUS.open,
nullable=False),
Column('resolved', types.DateTime),
Column('created', types.DateTime, default=datetime.now,
nullable=False),
Column('visibility', types.Unicode, default=u'visible'),
Column('abuse_status',
types.Integer,
default=AbuseStatus.unmoderated.value),
Index('idx_issue_number_dataset_id', 'dataset_id', 'number',
unique=True),
)

issue_comment_table = Table(
'issue_comment',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('comment', types.Unicode, nullable=False),
Column('user_id', types.Unicode, nullable=False, index=True),
Column('issue_id', types.Integer,
ForeignKey('issue.id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False, index=True),
Column('created', types.DateTime, default=datetime.now,
nullable=False),
Column('visibility', types.Unicode, default=u'visible'),
Column('abuse_status',
types.Integer,
default=AbuseStatus.unmoderated.value),
)

meta.mapper(
Issue,
issue_table,
properties={
'user': relation(
model.User,
backref=backref('issues',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_table.c.user_id) == remote(User.id),
uselist=False
),
'assignee': relation(
model.User,
backref=backref('resolved_issues',
cascade='all'),
primaryjoin=foreign(issue_table.c.assignee_id) == remote(User.id)
),
'dataset': relation(
model.Package,
backref=backref('issues',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_table.c.dataset_id) == remote(Package.id),
uselist=False
),
'resource': relation(
model.Resource,
backref=backref('issues', cascade='all'),
primaryjoin=foreign(issue_table.c.resource_id) == remote(Resource.id)
),
}
)

meta.mapper(IssueCategory, issue_category_table)

meta.mapper(
IssueComment,
issue_comment_table,
properties={
'user': relation(
model.User,
backref=backref('issue_comments',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_comment_table.c.user_id) == remote(User.id)
),
'issue': relation(
Issue,
backref=backref('comments', cascade='all, delete-orphan'),
primaryjoin=issue_comment_table.c.issue_id.__eq__(Issue.id)
),
}
)
issue_category_table = Table(
'issue_category',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('name', types.Unicode(ISSUE_CATEGORY_NAME_MAX_LENGTH),
nullable=False, unique=True),
Column('description', types.Unicode, nullable=False, unique=False),
Column('created', types.DateTime, default=datetime.now,
nullable=False))

issue_table = Table(
'issue',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('number', types.Integer, nullable=False),
Column('title', types.UnicodeText, nullable=False),
Column('description', types.UnicodeText),
Column('dataset_id', types.UnicodeText, nullable=False),
Column('resource_id', types.UnicodeText),
Column('user_id', types.UnicodeText, nullable=False),
Column('assignee_id', types.UnicodeText),
Column('status', types.String(15), default=ISSUE_STATUS.open,
nullable=False),
Column('resolved', types.DateTime),
Column('created', types.DateTime, default=datetime.now,
nullable=False),
Column('visibility', types.Unicode, default=u'visible'),
Column('abuse_status',
types.Integer,
default=AbuseStatus.unmoderated.value),
Index('idx_issue_number_dataset_id', 'dataset_id', 'number',
unique=True),
)

issue_comment_table = Table(
'issue_comment',
meta.metadata,
Column('id', types.Integer, primary_key=True, autoincrement=True),
Column('comment', types.Unicode, nullable=False),
Column('user_id', types.Unicode, nullable=False, index=True),
Column('issue_id', types.Integer,
ForeignKey('issue.id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False, index=True),
Column('created', types.DateTime, default=datetime.now,
nullable=False),
Column('visibility', types.Unicode, default=u'visible'),
Column('abuse_status',
types.Integer,
default=AbuseStatus.unmoderated.value),
)

meta.mapper(
Issue,
issue_table,
properties={
'user': relation(
model.User,
backref=backref('issues',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_table.c.user_id) == remote(User.id),
uselist=False
),
'assignee': relation(
model.User,
backref=backref('resolved_issues',
cascade='all'),
primaryjoin=foreign(issue_table.c.assignee_id) == remote(User.id)
),
'dataset': relation(
model.Package,
backref=backref('issues',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_table.c.dataset_id) == remote(Package.id),
uselist=False
),
'resource': relation(
model.Resource,
backref=backref('issues', cascade='all'),
primaryjoin=foreign(issue_table.c.resource_id) == remote(Resource.id)
),
}
)

meta.mapper(IssueCategory, issue_category_table)

meta.mapper(
IssueComment,
issue_comment_table,
properties={
'user': relation(
model.User,
backref=backref('issue_comments',
cascade='all, delete-orphan',
single_parent=True),
primaryjoin=foreign(issue_comment_table.c.user_id) == remote(User.id)
),
'issue': relation(
Issue,
backref=backref('comments', cascade='all, delete-orphan'),
primaryjoin=issue_comment_table.c.issue_id.__eq__(Issue.id)
),
}
)

report_tables = define_report_tables([Issue, IssueComment])
10 changes: 0 additions & 10 deletions ckanext/issues/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class IssuesPlugin(p.SingletonPlugin):
"""
implements(p.IConfigurer, inherit=True)
implements(p.ITemplateHelpers, inherit=True)
implements(p.IConfigurable)
implements(p.IRoutes, inherit=True)
implements(p.IActions)
implements(p.IAuthFunctions)
Expand Down Expand Up @@ -51,15 +50,6 @@ def get_helpers(self):
helpers.issues_users_who_reported_issue,
}

# IConfigurable

def configure(self, config):
"""
Called by load_environment
"""
from ckanext.issues.model import setup as model_setup
model_setup()

# IRoutes

def before_map(self, map):
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"""
[ckan.plugins]
issues=ckanext.issues.plugin:IssuesPlugin
[paste.paster_command]
issues = ckanext.issues.commands:Issues
""",
)

0 comments on commit 4885e59

Please sign in to comment.