2017-03-22 14:35:40 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2013-07-19 17:57:53 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2019-01-21 17:11:12 -06:00
|
|
|
# Copyright © 2010-2019 Lance Edgar
|
2013-07-19 17:57:53 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2013-07-19 17:57:53 -05:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2013-07-19 17:57:53 -05:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-19 17:57:53 -05:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
2015-08-15 17:00:01 -05:00
|
|
|
Base View Class
|
2013-07-19 17:57:53 -05:00
|
|
|
"""
|
|
|
|
|
2016-06-14 22:19:16 -05:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2015-08-15 17:00:01 -05:00
|
|
|
|
2017-03-22 14:35:40 -05:00
|
|
|
import os
|
|
|
|
|
2018-02-12 14:41:40 -06:00
|
|
|
import six
|
|
|
|
|
2015-08-15 17:00:01 -05:00
|
|
|
from rattail.db import model
|
2017-08-04 16:11:45 -05:00
|
|
|
from rattail.util import progress_loop
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2016-05-10 13:08:32 -05:00
|
|
|
from pyramid import httpexceptions
|
2016-08-09 19:46:49 -05:00
|
|
|
from pyramid.renderers import render_to_response
|
2017-03-22 14:35:40 -05:00
|
|
|
from pyramid.response import FileResponse
|
2016-05-10 13:08:32 -05:00
|
|
|
|
2015-08-15 17:00:01 -05:00
|
|
|
from tailbone.db import Session
|
2017-03-27 21:37:45 -05:00
|
|
|
from tailbone.auth import logout_user
|
2013-09-01 17:31:50 -05:00
|
|
|
|
|
|
|
|
2013-07-19 17:57:53 -05:00
|
|
|
class View(object):
|
|
|
|
"""
|
2015-08-15 17:00:01 -05:00
|
|
|
Base class for all class-based views.
|
2013-07-19 17:57:53 -05:00
|
|
|
"""
|
|
|
|
|
2018-11-03 17:13:08 -05:00
|
|
|
def __init__(self, request, context=None):
|
2013-07-19 17:57:53 -05:00
|
|
|
self.request = request
|
2017-03-27 22:44:51 -05:00
|
|
|
|
|
|
|
# if user becomes inactive while logged in, log them out
|
2017-08-02 13:18:19 -05:00
|
|
|
if getattr(request, 'user', None):
|
|
|
|
# TODO: why is the user sometimes not attached to session?
|
|
|
|
# (this has only been seen on mobile, when creating a new ordering batch)
|
|
|
|
if request.user not in Session():
|
|
|
|
request.user = Session.merge(request.user)
|
|
|
|
if not request.user.active:
|
|
|
|
headers = logout_user(request)
|
|
|
|
raise self.redirect(request.route_url('home'))
|
2017-03-27 22:44:51 -05:00
|
|
|
|
2016-11-19 18:56:09 -06:00
|
|
|
config = self.rattail_config
|
|
|
|
if config:
|
|
|
|
self.enum = config.get_enum()
|
2015-03-17 14:49:20 -05:00
|
|
|
|
2015-07-20 09:51:29 -05:00
|
|
|
@property
|
|
|
|
def rattail_config(self):
|
|
|
|
"""
|
|
|
|
Reference to the effective Rattail config object.
|
|
|
|
"""
|
2016-11-19 18:56:09 -06:00
|
|
|
return getattr(self.request, 'rattail_config', None)
|
2015-07-20 09:51:29 -05:00
|
|
|
|
2017-03-21 13:18:24 -05:00
|
|
|
def notfound(self):
|
|
|
|
return httpexceptions.HTTPNotFound()
|
|
|
|
|
2015-08-15 17:00:01 -05:00
|
|
|
def late_login_user(self):
|
|
|
|
"""
|
|
|
|
Returns the :class:`rattail:rattail.db.model.User` instance
|
|
|
|
corresponding to the "late login" form data (if any), or ``None``.
|
|
|
|
"""
|
|
|
|
if self.request.method == 'POST':
|
|
|
|
uuid = self.request.POST.get('late-login-user')
|
|
|
|
if uuid:
|
|
|
|
return Session.query(model.User).get(uuid)
|
|
|
|
|
2016-06-14 22:19:16 -05:00
|
|
|
def redirect(self, url, **kwargs):
|
2016-05-10 13:08:32 -05:00
|
|
|
"""
|
|
|
|
Convenience method to return a HTTP 302 response.
|
|
|
|
"""
|
2016-06-14 22:19:16 -05:00
|
|
|
return httpexceptions.HTTPFound(location=url, **kwargs)
|
2016-05-10 13:08:32 -05:00
|
|
|
|
2017-08-04 16:11:45 -05:00
|
|
|
def progress_loop(self, func, items, factory, *args, **kwargs):
|
|
|
|
return progress_loop(func, items, factory, *args, **kwargs)
|
|
|
|
|
2017-08-09 11:44:31 -05:00
|
|
|
# TODO: this signature seems wonky
|
|
|
|
def render_progress(self, progress, kwargs, template=None):
|
2016-08-09 19:46:49 -05:00
|
|
|
"""
|
|
|
|
Render the progress page, with given kwargs as context.
|
|
|
|
"""
|
2017-08-09 11:44:31 -05:00
|
|
|
if not template:
|
|
|
|
template = '/progress.mako'
|
2017-08-07 18:19:29 -05:00
|
|
|
kwargs['progress'] = progress
|
2018-06-07 12:40:25 -05:00
|
|
|
kwargs.setdefault('can_cancel', True)
|
2017-08-09 11:44:31 -05:00
|
|
|
return render_to_response(template, kwargs, request=self.request)
|
2016-08-09 19:46:49 -05:00
|
|
|
|
2017-03-22 14:35:40 -05:00
|
|
|
def file_response(self, path):
|
|
|
|
"""
|
|
|
|
Returns a generic FileResponse from the given path
|
|
|
|
"""
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return self.notfound()
|
|
|
|
response = FileResponse(path, request=self.request)
|
2019-01-21 17:11:12 -06:00
|
|
|
response.content_length = os.path.getsize(path)
|
|
|
|
filename = os.path.basename(path)
|
|
|
|
if six.PY2:
|
|
|
|
filename = filename.encode('ascii', 'replace')
|
|
|
|
response.content_disposition = str('attachment; filename="{}"'.format(filename))
|
2017-03-22 14:35:40 -05:00
|
|
|
return response
|