This file looks large and may slow your browser down if we attempt
to syntax highlight it, so we are showing it without any
pretty colors.
Highlight
it anyway.
1 |
# coding utf-8 |
2 |
# classes/context.py |
3 |
# class:: Context |
4 |
|
5 |
from datetime import datetime |
6 |
import json |
7 |
|
8 |
from swtstore.classes import db |
9 |
from swtstore.classes.models.types import JSONType |
10 |
from swtstore.classes.exceptions import AlreadyExistsError |
11 |
|
12 |
|
13 |
class Context(db.Model): |
14 |
""" docstring """ |
15 |
|
16 |
__tablename__ = 'contexts' |
17 |
|
18 |
id = db.Column(db.Integer, primary_key=True) |
19 |
name = db.Column(db.String(256), nullable=False, unique=True) |
20 |
definition = db.Column(JSONType, nullable=False) |
21 |
created = db.Column(db.DateTime, default=datetime.utcnow) |
22 |
modified = db.Column(db.DateTime, default=None) |
23 |
|
24 |
def __init__(self, name, definition): |
25 |
for context in Context.query.all(): |
26 |
if name == context.name: |
27 |
raise AlreadyExistsError('Context with name exists!') |
28 |
return |
29 |
|
30 |
self.name = name |
31 |
self.definition = definition |
32 |
|
33 |
def __repr__(self): |
34 |
print 'Context Object: <%s>' % self.name |
35 |
|
36 |
def __str__(self): |
37 |
return 'Context Object: <%s>' % self.name |
38 |
|
39 |
def persist(self): |
40 |
db.session.add(self) |
41 |
try: |
42 |
db.session.commit() |
43 |
except IntegrityError: |
44 |
raise AlreadyExistsError('Error') |
45 |
|
46 |
# return a context instance given a name |
47 |
@staticmethod |
48 |
def getContextByName(name): |
49 |
return Context.query.filter_by(name=name).first() |