Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
PEP8 data model
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Nov 21, 2013
1 parent 209d522 commit 0a095b8
Showing 1 changed file with 37 additions and 43 deletions.
80 changes: 37 additions & 43 deletions synoptic/datamodel.py
@@ -1,25 +1,20 @@
MAPPERS_DEFINED = [False]



BUMP_INTERVALS = [
("hour", "hour"),
("day", "day"),
("day", "day"),
("week", "week"),
("2week", "2 weeks"),
("month", "month"),
("year", "year")
]




def is_valid_tag(tag):
return tag.isalnum()




class DataModel(object):
def __init__(self):
if MAPPERS_DEFINED[0]:
Expand Down Expand Up @@ -51,20 +46,21 @@ def __init__(self):
Column('timestamp', Float, index=True),
Column('contents', UnicodeText()),

# all-day events are saved with start/end dates at 0:00
# all-day events are saved with start/end dates at 0:00
# in the GMT timezone
Column('all_day', Boolean()),

Column('start_date', Float()),
Column('end_date', Float()),
Column('bump_interval',
Column('bump_interval',
Enum(*[key for key, val in BUMP_INTERVALS])),
Column('hide_until', Float()),
Column('highlight_at', Float()),
)

cls.itemversions_tags = Table('itemversions_tags', self.metadata,
Column('itemversion_id', Integer, ForeignKey('itemversions.id'), index=True),
Column('itemversion_id', Integer,
ForeignKey('itemversions.id'), index=True),
Column('tag_id', Integer, ForeignKey('tags.id'), index=True),
)

Expand All @@ -88,7 +84,7 @@ def __init__(self):
})
mapper(Tag, self.tags)
mapper(ItemVersion, self.itemversions, properties={
'tags':relation(Tag, secondary=self.itemversions_tags)
'tags': relation(Tag, secondary=self.itemversions_tags)
})

mapper(ViewOrdering, self.vieworderings, properties={
Expand All @@ -99,9 +95,8 @@ def __init__(self):
})


# {{{ mapped instances


# mapped instances ------------------------------------------------------------
class Tag(object):
def __init__(self, name):
self.name = name
Expand All @@ -116,16 +111,14 @@ def __eq__(self, other):
return self.name == other.name




class Item(object):
def __repr__(self):
return "<Item %s>" % self.id


_html_cache = {}


_html_cache = {}
class ItemVersion(object):
def __init__(self, **kwargs):
self.item = kwargs.pop("item")
Expand Down Expand Up @@ -179,7 +172,7 @@ def htmlize(text):
try:
result = markdown(text)
except:
result = "*** MARKDOWN ERROR *** "+text;
result = "*** MARKDOWN ERROR *** "+text

_html_cache[text] = result
return result
Expand All @@ -190,15 +183,12 @@ def contents_html(self):
return self.htmlize(self.contents)



class ViewOrdering(object):
def __init__(self, norm_query, timestamp):
self.norm_query = norm_query
self.timestamp = timestamp




class ViewOrderingEntry(object):
def __init__(self, viewordering, item, weight):
self.viewordering = viewordering
Expand All @@ -208,10 +198,11 @@ def __init__(self, viewordering, item, weight):
def __repr__(self):
return "<VOEntry item=%s, weight=%s>" % (self.item, self.weight)

# }}}


# {{{ tools

# tools -----------------------------------------------------------------------
def find_tags(session, tags, create_them):
if isinstance(tags, basestring):
tags = [s.strip() for s in tags.split(",")]
Expand All @@ -235,8 +226,6 @@ def find_tags(session, tags, create_them):
return result




def store_itemversion(dbsession, **kwargs):
import re
from htmlentitydefs import name2codepoint
Expand Down Expand Up @@ -279,10 +268,11 @@ def replace_special_char(match):

return itemversion

# }}}


# {{{ query rewrite visitor

# query rewrite visitor -------------------------------------------------------
class SQLifyQueryVisitor(object):
def __init__(self, session):
self.session = session
Expand Down Expand Up @@ -338,7 +328,7 @@ def visit_date_query(self, q):

def visit_dated_query(self, q):
from sqlalchemy.sql import or_
return or_(ItemVersion.start_date != None,
return or_(ItemVersion.start_date != None, # noqa
ItemVersion.end_date != None)

def visit_no_hide_query(self, q):
Expand All @@ -347,14 +337,15 @@ def visit_no_hide_query(self, q):
def visit_sort_by_date(self, q):
self.sort_by_date = True

# }}}


# {{{ query helpers

# query helpers ---------------------------------------------------------------
def get_current_itemversions_join(model, max_timestamp=None):
"""Find the current version of all items."""

from sqlalchemy.sql import select, and_, or_, not_, func
from sqlalchemy.sql import select, and_, func

max_timestamps_per_item = select(
[model.itemversions.c.item_id,
Expand All @@ -371,19 +362,17 @@ def get_current_itemversions_join(model, max_timestamp=None):
return model.itemversions.join(max_timestamps_per_item,
and_(
max_timestamps_per_item.c.item_id
==model.itemversions.c.item_id,
== model.itemversions.c.item_id,
max_timestamps_per_item.c.max_timestamp
==model.itemversions.c.timestamp,
== model.itemversions.c.timestamp,
))




def query_itemversions(session, model, parsed_query, max_timestamp=None):
"""Given parsed_query and max_timestamp, find the resulting ItemVersion ids,
in the right order."""

from sqlalchemy.sql import select, and_, or_, not_, func
from sqlalchemy.sql import select, and_, or_

# find view ordering
view_orderings = (session.query(ViewOrdering)
Expand All @@ -406,22 +395,22 @@ def query_itemversions(session, model, parsed_query, max_timestamp=None):
vo_entries = (
select([model.viewordering_entries])
.where(model.viewordering_entries.c.viewordering_id
==view_orderings[0].id)).alias("vo_entries")
== view_orderings[0].id)).alias("vo_entries")

from_obj = from_obj.outerjoin(vo_entries,
model.itemversions.c.item_id==vo_entries.c.item_id)
model.itemversions.c.item_id == vo_entries.c.item_id)

from synoptic.datamodel import SQLifyQueryVisitor
visitor = SQLifyQueryVisitor(session)

conditions = [
model.itemversions.c.contents != None,
model.itemversions.c.contents != None, # noqa
parsed_query.visit(visitor)
]

if visitor.use_hide_until:
from time import time
conditions.append(
conditions.append( # noqa
or_(ItemVersion.hide_until < time(),
ItemVersion.hide_until == None))

Expand All @@ -439,10 +428,11 @@ def query_itemversions(session, model, parsed_query, max_timestamp=None):

return result.group_by(model.itemversions.c.item_id)

# }}}


# {{{ view ordering handler

# view ordering handler -------------------------------------------------------
class ViewOrderingHandler:
def __init__(self, session, model, parsed_query):
self.session = session
Expand All @@ -466,20 +456,20 @@ def has_ordering(self):
def load(self):
self.item_ids = [row[self.model.itemversions.c.item_id]
for row in self.session.execute(
query_itemversions(self.session,
self.model, self.parsed_query)
.alias("currentversions"))]
query_itemversions(self.session,
self.model, self.parsed_query)
.alias("currentversions"))]

def __contains__(self, sought_item_id):
return sought_item_id in self.item_ids

def index_from_id(self, sought_item_id):
if sought_item_id == None:
if sought_item_id is None:
return len(self.item_ids)
for idx, item_id in enumerate(self.item_ids):
if item_id == sought_item_id:
return idx
raise ValueError, "invalid item id supplied"
raise ValueError("invalid item id supplied")

def reorder(self, moved_idx, before_idx):
assert moved_idx != before_idx
Expand All @@ -501,8 +491,8 @@ def delete(self):
from time import time

viewordering = ViewOrdering(str(self.parsed_query), time())
session.add(viewordering)
session.commit()
self.session.add(viewordering)
self.session.commit()

def save(self):
from time import time
Expand All @@ -513,3 +503,7 @@ def save(self):
viewordering.entries.append(ViewOrderingEntry(viewordering,
self.session.query(Item).get(item_id), idx))
self.session.add(viewordering)

# }}}

# vim: foldmethod=marker

0 comments on commit 0a095b8

Please sign in to comment.