Skip to content

Commit

Permalink
implement persistent URLs for results
Browse files Browse the repository at this point in the history
  • Loading branch information
t-8ch committed Dec 16, 2015
1 parent 230d4a9 commit 233e178
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
1 change: 1 addition & 0 deletions oscad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def main(global_config, **settings):
config.add_route('request', 'request')
config.add_route('matrix_request', 'matrix')
config.add_route('result', 'result')
config.add_route('lsuc', 'result/{osuc}/{lsuc}')
config.add_route('imprint', 'imprint')
config.add_route('about', 'about')
config.add_route('help', 'help')
Expand Down
9 changes: 8 additions & 1 deletion oscad/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,19 @@ def __str__(self):
def __json__(self, request):
return self.__dict__

@property
def mnemonic(self):
return self.name

@classmethod
def from_name(cls, name):
# why not....
name = name.replace('PGL', 'PgL')

store = data.lsuc[name]
store = data.lsuc.get(name)

if store is None:
return None

l = store['license']

Expand Down
75 changes: 43 additions & 32 deletions oscad/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import oscad_data as data

from . import exceptions
from .models import OSUC
from .models import OSUC, LSUC
from .version import __version__ as oscad_version

PLANNED_LICENSES = sorted(['AGPLv3.0', 'CDDLv1.0', 'ZLIB'])
Expand Down Expand Up @@ -61,33 +61,6 @@ def translation_texts_for_osuc(request, osuc):
}


def results_for_osuc_and_license(request, osuc, license):
if osuc is None:
raise exceptions.InvalidParameterCombination()

lsuc = osuc.get_lsuc(license)

if lsuc is None:
raise exceptions.InvalidParameterCombination()

lsuc_extra_info_hook = request.oscad_settings.lsuc_extra_info

if lsuc_extra_info_hook:
lsuc_extra_info = lsuc_extra_info_hook(request, lsuc, osuc)
else:
lsuc_extra_info = None

result = {
'osuc': osuc,
'lsuc': lsuc,
'lsuc_extra_info': lsuc_extra_info,
}

result.update(translation_texts_for_osuc(request, osuc))

return result


def extract_params(request, params):
try:
return [request.params[p] for p in params]
Expand Down Expand Up @@ -138,7 +111,8 @@ def about(request):
}


@view_config(route_name='components', renderer='templates/oscad/components.jinja2')
@view_config(route_name='components',
renderer='templates/oscad/components.jinja2')
def components(request):
avail = request.available_locales

Expand All @@ -158,12 +132,39 @@ def change_language(request):
if lang is None:
resp.unset_cookie('_LOCALE_')
else:
resp.set_cookie('_LOCALE_', lang, max_age=timedelta(days=365)) # max_age = year
# max_age = year
resp.set_cookie('_LOCALE_', lang, max_age=timedelta(days=365))

return resp


@view_config(route_name='result', renderer='templates/oscad/result.jinja2')
@view_config(route_name='lsuc', renderer='templates/oscad/result.jinja2')
def lsuc(request):
lsuc = LSUC.from_name(request.matchdict.get('lsuc'))
osuc = OSUC.from_number(request.matchdict.get('osuc'))

if lsuc is None or osuc is None:
return HTTPNotFound()

lsuc_extra_info_hook = request.oscad_settings.lsuc_extra_info

if lsuc_extra_info_hook:
lsuc_extra_info = lsuc_extra_info_hook(request, lsuc, osuc)
else:
lsuc_extra_info = None

result = {
'osuc': osuc,
'lsuc': lsuc,
'lsuc_extra_info': lsuc_extra_info,
}

result.update(translation_texts_for_osuc(request, osuc))

return result


@view_config(route_name='result')
def result(request):

license, = extract_params(request, ['license'])
Expand All @@ -181,7 +182,17 @@ def result(request):
osuc = OSUC.from_attrs(recipient=recipient, type=type_, state=state,
form=form, context=context)

return results_for_osuc_and_license(request, osuc, license)
if osuc is None:
raise exceptions.InvalidParameterCombination()

lsuc = osuc.get_lsuc(license)

if lsuc is None:
raise exceptions.InvalidParameterCombination()

return HTTPSeeOther(request.route_path('lsuc',
lsuc=lsuc.mnemonic,
osuc=osuc.number))


@view_config(route_name='matrix_request',
Expand Down

0 comments on commit 233e178

Please sign in to comment.