Fixed #87 -- Added deletion of products

This commit is contained in:
Raphael Michel
2015-08-13 18:27:19 +02:00
parent 56961337ae
commit 478d6af248
7 changed files with 84 additions and 3 deletions

View File

@@ -1748,7 +1748,8 @@ class OrderPosition(ObjectWithAnswers, Versionable):
)
item = VersionedForeignKey(
Item,
verbose_name=_("Item")
verbose_name=_("Item"),
related_name='positions'
)
variation = VersionedForeignKey(
ItemVariation,

View File

@@ -0,0 +1,33 @@
{% extends "pretixcontrol/items/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% block title %}{% trans "Delete product" %}{% endblock %}
{% block inside %}
<h1>{% trans "Delete product" %}</h1>
{% if not possible and not item.active %}
<p>{% blocktrans %}You cannot delete the product <strong>{{ item }}</strong> because it already has been ordered.{% endblocktrans %}</p>
<div class="form-group submit-group">
<a href="{% url "control:event.items" organizer=request.event.organizer.slug event=request.event.slug %}" class="btn btn-default btn-cancel">
{% trans "Cancel" %}
</a>
<div class="clearfix"></div>
</div>
{% else %}
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
{% if possible %}
<p>{% blocktrans %}Are you sure you want to delete the product <strong>{{ item }}</strong>?{% endblocktrans %}</p>
{% else %}
<p>{% blocktrans %}You cannot delete the product <strong>{{ item }}</strong> because it already has been ordered, but you can deactive it.{% endblocktrans %}</p>
{% endif %}
<div class="form-group submit-group">
<a href="{% url "control:event.items" organizer=request.event.organizer.slug event=request.event.slug %}" class="btn btn-default btn-cancel">
{% trans "Cancel" %}
</a>
<button type="submit" class="btn btn-danger btn-save">
{% if possible %}{% trans "Delete" %}{% else %}{% trans "Deactivate" %}{% endif %}
</button>
</div>
</form>
{% endif %}
{% endblock %}

View File

@@ -21,6 +21,7 @@
<th>{% trans "Product name" %}</th>
<th>{% trans "Category" %}</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@@ -35,6 +36,7 @@
<a href="{% url "control:event.items.up" organizer=request.event.organizer.slug event=request.event.slug item=i.identity %}" class="btn btn-default btn-sm {% if forloop.counter0 == 0 %}disabled{% endif %}"><i class="fa fa-arrow-up"></i></a>
<a href="{% url "control:event.items.down" organizer=request.event.organizer.slug event=request.event.slug item=i.identity %}" class="btn btn-default btn-sm {% if forloop.revcounter0 == 0 %}disabled{% endif %}"><i class="fa fa-arrow-down"></i></a>
</td>
<td class="text-right"><a href="{% url "control:event.items.delete" organizer=request.event.organizer.slug event=request.event.slug item=i.identity %}" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a></td>
</tr>
{% endfor %}
{% endfor %}

View File

@@ -14,7 +14,7 @@
{% else %}
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
<p>{% blocktrans %}Are you sure you want to the property <strong>{{ property }}</strong>?{% endblocktrans %}</p>
<p>{% blocktrans %}Are you sure you want to delete the property <strong>{{ property }}</strong>?{% endblocktrans %}</p>
<div class="form-group submit-group">
<a href="{% url "control:event.items.properties" organizer=request.event.organizer.slug event=request.event.slug %}" class="btn btn-default btn-cancel">
{% trans "Cancel" %}

View File

@@ -6,7 +6,7 @@
<h1>{% trans "Delete question" %}</h1>
<form action="" method="post" class="form-horizontal">
{% csrf_token %}
<p>{% blocktrans %}Are you sure you want to the question <strong>{{ question }}</strong>?{% endblocktrans %}</p>
<p>{% blocktrans %}Are you sure you want to delete the question <strong>{{ question }}</strong>?{% endblocktrans %}</p>
{% if dependent|length > 0 %}
<p>{% blocktrans %}All answers to the question given by the buyers of the following products will be <strong>lost</strong>.{% endblocktrans %}</p>
{% for item in dependent %}

View File

@@ -32,6 +32,7 @@ urlpatterns = [
name='event.item.restrictions'),
url(r'^items/(?P<item>[0-9a-f-]+)/up$', item.item_move_up, name='event.items.up'),
url(r'^items/(?P<item>[0-9a-f-]+)/down$', item.item_move_down, name='event.items.down'),
url(r'^items/(?P<item>[0-9a-f-]+)/delete$', item.ItemDelete.as_view(), name='event.items.delete'),
url(r'^categories/$', item.CategoryList.as_view(), name='event.items.categories'),
url(r'^categories/(?P<category>[0-9a-f-]+)/delete$', item.CategoryDelete.as_view(),
name='event.items.categories.delete'),

View File

@@ -894,3 +894,47 @@ class ItemRestrictions(ItemDetailMixin, EventPermissionRequiredMixin, TemplateVi
'event': self.request.event.slug,
'item': self.object.identity
})
class ItemDelete(EventPermissionRequiredMixin, DeleteView):
model = Item
template_name = 'pretixcontrol/item/delete.html'
permission = 'can_change_items'
context_object_name = 'item'
def get_context_data(self, *args, **kwargs) -> dict:
context = super().get_context_data(*args, **kwargs)
context['possible'] = self.is_allowed()
return context
def is_allowed(self) -> bool:
return not self.get_object().positions.current.exists()
def get_object(self, queryset=None) -> Property:
if not hasattr(self, 'object') or not self.object:
try:
self.object = self.request.event.items.current.get(
identity=self.kwargs['item']
)
except Property.DoesNotExist:
raise Http404(_("The requested product does not exist."))
return self.object
def delete(self, request, *args, **kwargs):
success_url = self.get_success_url()
if self.is_allowed():
self.get_object().delete()
messages.success(request, _('The selected product has been deleted.'))
return HttpResponseRedirect(success_url)
else:
o = self.get_object()
o.active = False
o.save()
messages.success(request, _('The selected product has been deactivated.'))
return HttpResponseRedirect(success_url)
def get_success_url(self) -> str:
return reverse('control:event.items', kwargs={
'organizer': self.request.event.organizer.slug,
'event': self.request.event.slug,
})