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

Commit

Permalink
Move incrmental id generator to mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
msom committed Feb 23, 2018
1 parent 6c87a01 commit e10b550
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
9 changes: 1 addition & 8 deletions onegov/ballot/models/election/election.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from onegov.core.orm.mixins import meta_property
from onegov.core.orm.mixins import TimestampMixin
from onegov.core.orm.types import HSTORE
from onegov.core.utils import increment_name
from onegov.core.utils import normalize_for_url
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import desc
Expand Down Expand Up @@ -65,12 +63,7 @@ class Election(Base, ContentMixin, TimestampMixin,
@observes('title_translations')
def title_observer(self, translations):
if not self.id:
title = self.get_title(self.session_manager.default_locale)
id = normalize_for_url(title or 'election')
session = object_session(self)
while session.query(Election.id).filter(Election.id == id).first():
id = increment_name(id)
self.id = id
self.id = self.id_from_title(object_session(self))

#: Shortcode for cantons that use it
shortcode = Column(Text, nullable=True)
Expand Down
14 changes: 2 additions & 12 deletions onegov/ballot/models/election/election_composite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections import OrderedDict
from onegov.ballot.models.mixins import DomainOfInfluenceMixin
from onegov.ballot.models.mixins import StatusMixin
from onegov.ballot.models.mixins import TitleTranslationsMixin
from onegov.core.orm import Base
Expand All @@ -8,8 +7,6 @@
from onegov.core.orm.mixins import meta_property
from onegov.core.orm.mixins import TimestampMixin
from onegov.core.orm.types import HSTORE
from onegov.core.utils import increment_name
from onegov.core.utils import normalize_for_url
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import Text
Expand All @@ -20,8 +17,7 @@


class ElectionComposite(
Base, ContentMixin, TimestampMixin,
DomainOfInfluenceMixin, StatusMixin, TitleTranslationsMixin
Base, ContentMixin, TimestampMixin, StatusMixin, TitleTranslationsMixin
):

__tablename__ = 'election_composites'
Expand All @@ -39,12 +35,7 @@ class ElectionComposite(
@observes('title_translations')
def title_observer(self, translations):
if not self.id:
title = self.get_title(self.session_manager.default_locale)
id = normalize_for_url(title or 'election-composite')
session = object_session(self)
while session.query(ElectionComposite.id).filter_by(id=id).first():
id = increment_name(id)
self.id = id
self.id = self.id_from_title(object_session(self))

#: Shortcode for cantons that use it
shortcode = Column(Text, nullable=True)
Expand Down Expand Up @@ -159,7 +150,6 @@ def export(self):
for locale, title in self.title_translations.items():
common['composite_title_{}'.format(locale)] = (title or '').strip()
common['composite_date'] = self.date.isoformat()
common['composite_domain'] = self.domain
common['composite_mandates'] = self.number_of_mandates
common['composite_status'] = self.stats

Expand Down
22 changes: 20 additions & 2 deletions onegov/ballot/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from onegov.core.utils import increment_name
from onegov.core.utils import normalize_for_url
from sqlalchemy import Column
from sqlalchemy import Enum
from sqlalchemy.ext.declarative import declared_attr
Expand Down Expand Up @@ -27,7 +29,7 @@ def domain(cls):


class StatusMixin(object):
""" Mixin providing status indication for votes and elections."""
""" Mixin providing status indication for votes and elections. """

#: Status of the election or vote
@declared_attr
Expand Down Expand Up @@ -64,7 +66,9 @@ def completed(self):

class TitleTranslationsMixin(object):
""" Adds a helper to return the translation of the title without depending
on the locale of the request."""
on the locale of the request.
"""

def get_title(self, locale, default_locale=None):
""" Returns the requested translation of the title, falls back to the
Expand All @@ -79,6 +83,20 @@ def get_title(self, locale, default_locale=None):
translations.get(default_locale, None)
)

def id_from_title(self, session):
""" Returns a unique, user friendly id derived from the title. """

title = self.get_title(self.session_manager.default_locale)
id = normalize_for_url(title or self.__class__.__name__)
while True:
items = [
item for item in session.query(self.__class__).filter_by(id=id)
if item != self
]
if not items:
return id
id = increment_name(id)


def summarized_property(name):
""" Adds an attribute as hybrid_property which returns the sum of the
Expand Down
9 changes: 1 addition & 8 deletions onegov/ballot/models/vote/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from onegov.core.orm.mixins import meta_property
from onegov.core.orm.mixins import TimestampMixin
from onegov.core.orm.types import HSTORE
from onegov.core.utils import increment_name
from onegov.core.utils import normalize_for_url
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import desc
Expand Down Expand Up @@ -63,12 +61,7 @@ class Vote(Base, ContentMixin, TimestampMixin,
@observes('title_translations')
def title_observer(self, translations):
if not self.id:
title = self.get_title(self.session_manager.default_locale)
id = normalize_for_url(title or 'vote')
session = object_session(self)
while session.query(Vote.id).filter(Vote.id == id).first():
id = increment_name(id)
self.id = id
self.id = self.id_from_title(object_session(self))

#: identifies the date of the vote
date = Column(Date, nullable=False)
Expand Down

0 comments on commit e10b550

Please sign in to comment.