Skip to content
Snippets Groups Projects
item.py 7.9 KiB
Newer Older
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormView
import transport
from ..forms.item import AddEditItemForm
from ..constants import ITEM_TYPE_CATEGORY
class ItemTypeNotFound(Exception):
    """ Exception raised internally when an item type is not found """
    pass


class ItemNotFound(Exception):
    """ Exception raised internally when an item is not found """
    pass


class AddEditItemView(FormView):
    template_name = "hid/add_edit_item.html"
    form_class = AddEditItemForm

    def _initialize_item(self, item_id, item_type):
        """ Initialize the view's item from the given item id or item_type
            self.item_type to the item type (either from the given type
                of from the given item)
            self.item to the item object (or None)
            self.item_terms to a dictionary of taxonomy to list of terms
            item_id (int): Item id to initialize from
            item_type (str): Item type to initialize item_type from.
                This is used only if item_id is None.

        Raises:
            ItemNotFound: If the item was not found
            ItemTypeNotFound: If the item type was not found
        """
        self.item = None
        self.item_type = None
        self.item_terms = {}
        if item_id:
            try:
                self.item = transport.items.get(item_id)
            except transport.exceptions.TransportException:
                raise ItemNotFound()
            self.item_terms = {}
            for term in self.item['terms']:
                taxonomy = term['taxonomy']
                if taxonomy == 'item-types':
                    self.item_type = term
                if taxonomy not in self.item_terms:
                    self.item_terms[taxonomy] = []
                self.item_terms[taxonomy].append(term)
        elif item_type:
            matches = transport.terms.list(
                taxonomy='item-types',
                name=item_type
            )
            if len(matches) == 0:
                raise ItemTypeNotFound()
            else:
                self.item_type = matches[0]

    def get(self, request, *args, **kwargs):
        """ get request handler

        If the URL defines an item_id, we load the corresponding item
        to make it available for forms.
        """
            self._initialize_item(
                kwargs.get('item_id'), kwargs.get('item_type')
            )
        except ItemNotFound:
            return self._response(
                self.request.GET.get('next', '/'),
                messages.ERROR,
                (_('Item with id %s could not be found') %
                 str(kwargs.get('item_id')))
            )
        except ItemTypeNotFound:
            return self._response(
                self.request.GET.get('next', '/'),
                messages.ERROR,
                (_('Item type %s could not be found') %
                 str(kwargs.get('item_type')))
            )
        return super(AddEditItemView, self).get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        """ post request handler

        If the URL defines an item_id, we load the corresponding item
        to make it available for forms.

        We handle cancel and delete here, as the form doesn't it to be
        valid for those.
        """
            self._initialize_item(
                kwargs.get('item_id'), kwargs.get('item_type')
            )
        except ItemNotFound:
            return self._response(
                self.request.GET.get('next', '/'),
                messages.ERROR,
                (_('Item with id %s could not be found') %
                 str(kwargs.get('item_id')))
            )
        except ItemTypeNotFound:
            return self._response(
                self.request.GET.get('next', '/'),
                messages.ERROR,
                (_('Item type %s could not be found') %
                 str(kwargs.get('item_type')))
            )
        if 'cancel' in self.request.POST['action']:
            return self._response(
                self.request.POST['next'],
                messages.INFO,
                _('No action performed')
            )
        if 'delete' in self.request.POST['action']:
            return self._response(
                self.request.POST['next'],
                messages.ERROR,
                _('Delete item not implemented')
            )

        return super(AddEditItemView, self).post(request, *args, **kwargs)

    def get_initial(self):
        """ Return the form object's initial values for the current item """
        if self.item is None:
            initial = {
                'id': 0,
                'timestamp': datetime.now(),
                'next': self.request.GET.get('next', self.request.path)
            }
        else:
            initial = {
                'id': self.item['id'],
                'body': self.item['body'],
                'timestamp': self.item['timestamp'],
                'next': self.request.GET.get('next', self.request.path)
            }
        taxonomy = ITEM_TYPE_CATEGORY.get(self.item_type['name'])
        if (taxonomy and taxonomy in self.item_terms
                and len(self.item_terms[taxonomy]) > 0):
            initial['category'] = self.item_terms[taxonomy][0]['name']

        return initial

    def get_form(self, form_class):
        """ Return the form object to be used """
        return form_class(self.item_type['name'], **self.get_form_kwargs())
        """ Get the form's context data

        We invoke FormView's get_context_data and add the current
        item.
        """
        context = super(AddEditItemView, self).get_context_data(**kwargs)

        context['item_type_label'] = self.item_type['long_name']

        # Add the width of the option row to the context
        option_row_widget_count = 1  # We always have 'created'
        if 'category' in kwargs['form'].fields:
            option_row_widget_count += 1
        if 'region' in kwargs['form'].fields:
            option_row_widget_count += 1
        context['option_row_width'] = 12 / option_row_widget_count

        return context

    def form_valid(self, form):
        """ Form submit handler """
        # if self.item_type:
        #     item_type = self.item_type['long_name']
        # else:
        #     item_type = 'Item'
        # msg = _("%s %d successfully updated.") % (
        #     item_type,
        #     int(form.cleaned_data['id'])
        # )

        # return self._response(
        #    form.cleaned_data['next'],
        #    messages.SUCCESS,
        #    msg
        # )
        return self._response(
            form.cleaned_data['next'],
            messages.ERROR,
            _('Update item not implemented')
        )

    def form_invalid(self, form):
        """ Form invalid handler """
        messages.add_message(
            self.request,
            messages.ERROR,
            _("The form could not be submitted."
              "Please correct the errors and submit it again.")
        )
        return super(AddEditItemView, self).form_invalid(form)

    def _response(self, url, message_type, message):
        """ Log a message and return an HTTP Response

        Args:
            url (str): URL to redirect to
            message_type (str): Message type to log (from message.INFO, etc.)
            message (str): Message to log
        Returns:
            HttpResponseRedirect: Response object
        """
        messages.add_message(self.request, message_type, message)
        return HttpResponseRedirect(url)