Skip to content

Commit

Permalink
Add key frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
pta2002 committed May 24, 2017
1 parent f6711ec commit e8357ae
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
31 changes: 31 additions & 0 deletions shop/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,34 @@ def clean_rating(self):
if not 1 <= data['rating'] <= 5:
raise forms.ValidationError("Rating should be between 1 and 5")
return int(data['rating'])


class KeyForm(forms.Form):
title = forms.CharField(max_length=200)
description = forms.CharField(widget=forms.Textarea(attrs={'class': 'vresize'}))
keys = forms.CharField(required=False, label="Keys to add",
widget=forms.Textarea(attrs={'class': 'vresize'}),
help_text='One key per line. '
'You won\'t be able to remove these')
is_link = forms.BooleanField(label="Are these keys links?",
help_text='Setting this will make it so that'
' the keys are clickable',
required=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.layout = Layout(
Div(
'title',
'description',
'keys',
'is_link',
css_class='panel-body'
),
Div(
Submit('submit', 'Add keys'),
css_class='panel-footer'
)
)
23 changes: 23 additions & 0 deletions shop/templates/shop/editkeys.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ <h4>Your files</h4>
{% endif %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4>Keys</h4>
</div>
<div class="panel-body">
<p>You can use this to distribute a different key to each person</p>
{% if product.digitalkeyset_set %}
<div class="row">
<div class="col-sm-10"><strong>Keyset name</strong></div>
<div class="col-sm-2"><strong>Keys remaining</strong></div>
</div>
{% for keyset in product.digitalkeyset_set.all %}
<div class="row">
<div class="col-sm-10"><a href="{% url 'shop:managekey' product.id keyset.id %}">{{ keyset.name }}</a></div>
<div class="col-sm-2">{{ keyset.get_stock }}</div>
</div>
{% endfor %}
{% else %}
<p>There are no key sets!</p>
{% endif %}
<a href="{% url 'shop:managekey' product.id %}" class="btn btn-block btn-primary">Create keyset</a>
</div>
</div>
{% endblock %}
{% block modals %}
{% for file in product.digitalfile_set.all %}
Expand Down
9 changes: 9 additions & 0 deletions shop/templates/shop/managekey.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "shop/base.html" %}
{% block title %}Manage keyset{% endblock %}
{% block body %}
<h1>{% if keyset %}Edit {{ keyset.name }}{% else %}Create keyset{% endif %}</h1>
<div class="panel panel-default">
{% load crispy_forms_tags %}
{% crispy form %}
</div>
{% endblock %}
4 changes: 3 additions & 1 deletion shop/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
name='addtocart'),
url(r'^product/(?P<id>\d*[1-9]\d*)/edit/$', views.edit_product,
name='editproduct'),
url(r'^product/(?P<id>\d*[1-9]\d*)/edit/keys/$', views.edit_keys,
url(r'^product/(?P<id>\d*[1-9]\d*)/edit/files/$', views.edit_keys,
name='editkeys'),
url(r'^product/(?P<id>\d*[1-9]\d*)/edit/files/key/'
r'(?P<keyid>\d*[1-9]\d*)?/?$', views.manage_keyset, name='managekey'),
url(r'^product/(?P<id>\d*[1-9]\d*)/edit/keys/upload/$',
views.upload_file_noapi, name='uploadfilenoapi'),
url(r'^shop/(?P<user>[A-Za-z0-9\-\_]{1,150})/$', views.shop, name='shop'),
Expand Down
43 changes: 43 additions & 0 deletions shop/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,46 @@ def view_all(request, page=0):
products = Product.objects.filter(approved=True, removed=False).order_by('-date')

return render(request, 'shop/all_products.html', {'products': products})


@login_required
def manage_keyset(request, id, keyid=None):
context = {}

if keyid is not None:
keyset = get_object_or_404(DigitalKeySet, id=keyid,
product__seller=request.user)
context['keyset'] = keyset

if request.method == 'POST':
form = KeyForm(request.POST)

if form.is_valid():
if keyid is None:
keyset = DigitalKeySet(
name=form.cleaned_data['title'],
description=form.cleaned_data['description'],
is_link=form.cleaned_data['is_link'],
product_id=id
)
keyset.save()
keys = []
for key in form.cleaned_data['keys'].strip().split('\n'):
keys.append(DigitalKey(keyset=keyset, key=key))
DigitalKey.objects.bulk_create(keys)
messages.success(request, "Successfully added %d key%s" %
(len(keys), 's' if len(keys) != 1 else ''))
return redirect('shop:managekey', id=id, keyid=keyset.id)

else:
if keyid is None:
form = KeyForm()
else:
form = KeyForm({
'title': keyset.name,
'description': keyset.description
})

context['form'] = form

return render(request, 'shop/managekey.html', context)

0 comments on commit e8357ae

Please sign in to comment.