Skip to content

Commit

Permalink
Merge pull request #21 from nuxis/feature/add-users
Browse files Browse the repository at this point in the history
Feature/add users
  • Loading branch information
haavardlian authored Sep 30, 2018
2 parents 355e299 + ec64c05 commit 05a0ca4
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
env/
venv/
collected_static/
prod.py
.tox/
Expand Down
2 changes: 2 additions & 0 deletions p0sx/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@

MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media')
MEDIA_URL = '/media/'

LOGIN_REDIRECT_URL = '/littleadmin/scan_user_card'
20 changes: 17 additions & 3 deletions p0sx/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
from django.contrib import admin
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import RedirectView
from django.contrib.auth import views as auth_views

from pos.views.littleadmin import check_credit, credit_edit, credit_overview, crew_report, sale_overview
from pos.views.littleadmin import (add_user,
check_credit,
credit_edit,
credit_overview,
crew_report,
edit_user_credit,
sale_overview,
scan_user_card)
from pos.views.shift import AllShiftsViewSet, CurrentShiftViewSet, NewShiftViewSet, ShiftViewSet
from pos.views.stock import (CategoryViewSet,
CreditCheckViewSet,
Expand All @@ -27,9 +35,12 @@
url(r'^$', RedirectView.as_view(url=reverse_lazy('littleadmin:overview'))),
url(r'check/', check_credit, name='check'),
url(r'overview/', credit_overview, name='overview'),
url(r'edit/(?P<card>\w+)', credit_edit, name='edit'),
url(r'edit_crew_credit/(?P<card>\w+)', credit_edit, name='edit_crew_credit'),
url(r'sale/', include(sale_url, namespace='sale')),
url(r'crew_report/', crew_report, name='crew_report')
url(r'crew_report/', crew_report, name='crew_report'),
url(r'scan_user_card', scan_user_card, name='scan_user_card'),
url(r'edit_user_credit/(?P<card>\w+)', edit_user_credit, name='edit_user_credit'),
url(r'add_user/(?P<card>\w+)', add_user, name='add_user')
]

# Routers provide an easy way of automatically determining the URL conf.
Expand All @@ -48,8 +59,11 @@
router.register(r'purchases', PurchaseViewSet, 'purchase')
router.register(r'credit', CreditCheckViewSet, 'credit')
router.register(r'discounts', DiscountViewSet, 'discount')

urlpatterns = [
url(r'^$', RedirectView.as_view(url=reverse_lazy('admin:index'))),
url(r'^login/$', auth_views.login, {'template_name': 'pos/login.djhtml'}, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/login'}, name='logout'),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'littleadmin/', include(littleadmin_url, namespace='littleadmin'))
Expand Down
20 changes: 19 additions & 1 deletion pos/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,29 @@


class CheckCreditForm(forms.Form):
card = forms.CharField(max_length=100, widget=forms.PasswordInput())
card = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'autofocus': 'autofocus'}))


class AddCreditForm(forms.Form):
credit = forms.CharField(widget=forms.NumberInput(attrs={'autofocus': 'autofocus'}))


class ChangeCreditForm(forms.ModelForm):

class Meta:
model = User
fields = ['credit']
widgets = {
'credit': forms.NumberInput(attrs={'autofocus': 'autofocus'})
}


class AddUserForm(forms.ModelForm):

class Meta:
model = User
fields = ['first_name', 'last_name', 'phone', 'email', 'credit', 'card']
widgets = {
'card': forms.PasswordInput(render_value=True),
'first_name': forms.TextInput(attrs={'autofocus': 'autofocus'})
}
3 changes: 2 additions & 1 deletion pos/management/commands/import-ge-crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def handle(self, *args, **options):
crew=data['crew'],
role=data['role'],
email=data['email'],
credit=0
credit=0,
is_crew=True
)
crew.save()
# print('Added new user {} {}'.format(data['first_name'], data['last_name']))
23 changes: 23 additions & 0 deletions pos/migrations/0006_auto_20180925_2035.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-09-25 18:35
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('pos', '0005_auto_20170902_1225'),
]

operations = [
migrations.AlterModelOptions(
name='category',
options={'verbose_name_plural': 'Categories'},
),
migrations.AlterModelOptions(
name='user',
options={'permissions': (("update_credit", "Can update the credit limit on a user"),)},
),
]
20 changes: 20 additions & 0 deletions pos/migrations/0007_user_is_crew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-09-25 18:49
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pos', '0006_auto_20180925_2035'),
]

operations = [
migrations.AddField(
model_name='user',
name='is_crew',
field=models.BooleanField(default=False),
),
]
7 changes: 5 additions & 2 deletions pos/models/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ class Order(models.Model):
def __str__(self):
return str(self.user) + ' ' + self.date.strftime('%Y-%m-%d %H:%M:%S')

@property
def info(self):
return f"{self.sum} {self.date:%Y-%m-%d %H:%M:%S}"

@classmethod
def create(cls, user, cashier, authenticated_user, payment_method, message):
order = cls(user=user,
cashier=cashier,
authenticated_user=authenticated_user,
payment_method=payment_method,
message=message
)
message=message)

return order

Expand Down
17 changes: 17 additions & 0 deletions pos/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User(models.Model):
role = models.CharField(max_length=255)
email = models.EmailField()
is_cashier = models.BooleanField(default=False)
is_crew = models.BooleanField(default=False)

@property
def used(self):
Expand All @@ -25,9 +26,25 @@ def used(self):
def left(self):
return self.credit - self.used

@classmethod
def create(cls, card, credit, first_name, last_name, phone, email):
user = cls(card=card,
credit=credit,
first_name=first_name,
last_name=last_name,
phone=phone,
email=email)

return user

def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)

class Meta:
permissions = (
("update_credit", "Can update the credit limit on a user"),
)


class UserSession(models.Model):
start = models.DateTimeField(auto_now_add=True)
Expand Down
101 changes: 94 additions & 7 deletions pos/views/littleadmin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import Case, IntegerField, Sum, When
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse_lazy
from django.contrib import messages

from ..forms import ChangeCreditForm, CheckCreditForm
from ..forms import AddCreditForm, AddUserForm, ChangeCreditForm, CheckCreditForm
from ..models.shift import Shift
from ..models.stock import Item, OrderLine
from ..models.stock import Item, OrderLine, Order
from ..models.user import User
from ..serializers.shift import ShiftSerializer

Expand All @@ -17,14 +18,16 @@ def check_credit(request):

if form.is_valid():
card = form.cleaned_data['card']

user = User.objects.filter(card=card)

if not user:
return HttpResponseRedirect(reverse_lazy('littleadmin:check'))

orders = Order.objects.filter(user_id=user.id).order_by('date')[0:3]

return render(request, 'pos/credit_check.djhtml', {
'form': CheckCreditForm(),
'orders': [o.info for o in orders],
'table': True,
'used': user[0].used,
'credit': user[0].credit,
Expand All @@ -43,7 +46,6 @@ def check_credit(request):
def credit_overview(request):
bought = OrderLine.objects.all().exclude(order__user__isnull=True).values('order__user').annotate(used=Sum('price'))
users = User.objects.all().values()
print(bought)
for user in users:
for b in bought:
user['used'] = 0
Expand Down Expand Up @@ -131,5 +133,90 @@ def sale_overview(request):
total['total'] += item['total']

shifts = ShiftSerializer(Shift.objects.all(), many=True)
print(shifts.data)
return render(request, 'pos/sale_overview.djhtml', {'overview': overview, 'shifts': shifts.data, 'total': total})


@permission_required("pos.update_credit")
def scan_user_card(request):
if request.POST:
form = CheckCreditForm(request.POST)

if form.is_valid():
card = form.cleaned_data['card']

user = User.objects.filter(card=card)

if not user:
return redirect('littleadmin:add_user', card=card)

return redirect('littleadmin:edit_user_credit', card=card)
else:
return HttpResponseRedirect(reverse_lazy('littleadmin:scan_user_card'))
else:
return render(request, 'pos/scan_card.djhtml', {
'form': CheckCreditForm(),
'table': False,
})


@permission_required('pos.update_credit')
def edit_user_credit(request, card=None):
if request.POST:
form = AddCreditForm(request.POST)
if form.is_valid():
credit = form.cleaned_data['credit']
user = get_object_or_404(User, card=card)

if user.is_crew:
messages.error(request, "You cannot change the credit of Crew")
return redirect('littleadmin:scan_user_card')

user.credit = user.credit + credit
user.save()
messages.success(request, "Credit updated successfully")
return redirect('littleadmin:scan_user_card')
else:
user = get_object_or_404(User, card=card)

if user.is_crew:
messages.error(request, "You cannot change the credit of Crew")
return redirect('littleadmin:scan_user_card')

form = AddCreditForm()

return render(request, 'pos/add_credit.djhtml', {'form': form, 'target': user})


@permission_required('pos.update_credit')
def add_user(request, card=None):
if request.POST:
form = AddUserForm(request.POST)

if form.is_valid():
card = form.cleaned_data['card']
credit = form.cleaned_data['credit']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
phone = form.cleaned_data['phone']
email = form.cleaned_data['email']

user = User.create(card, credit, first_name, last_name, phone, email)

user.save()
messages.success(request, "User added successfully")
return redirect('littleadmin:scan_user_card')
else:
messages.error(request, "Failed to add user")
return HttpResponseRedirect(reverse_lazy('littleadmin:add_user'))
else:
form = AddUserForm(initial={'card': card})
return render(request, 'pos/add_user.djhtml', {
'form': form
})







30 changes: 17 additions & 13 deletions templates/base.djhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
<html>

<head>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<!-- Compiled and minified JavaScript -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

{% block script%}
{% endblock script %}
Expand All @@ -30,34 +29,39 @@
</head>

<body>
{% if user.is_authenticated %}
{% if messages %}
<script>
{% for message in messages %}
M.toast({html: '{{message}}'});
{% endfor %}
</script>
{% endif %}
<nav>
<div class="nav-wrapper">
<div class="nav-wrapper cyan">
<a href="#" class="brand-logo">p0sX a little admin</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
{% if user.is_staff %}
<li><a href="{% url 'littleadmin:check' %}">Check credit</a></li>
<li><a href="{% url 'littleadmin:overview' %}">Credit overview</a></li>
<li><a href="{% url 'littleadmin:sale:overview' %}">Sale overview</a></li>
<li><a href="{% url 'littleadmin:crew_report' %}">Crew report</a></li>
{% elif user.is_authenticated %}
<li><a href="{% url 'logout' %}">Logout</a></li>
{% endif %}
</ul>
</div>
</nav>
{% endif %}
<main>
<div class="container">
{% block content %}{% endblock content %}
</div>
</main>
<footer class="page-footer">
<footer class="page-footer cyan">
<div class="footer-copyright">
<div class="container">
© <script type="text/javascript">
document.write(new Date().getFullYear());
</script> Nuxis, Version: {% version %}
© {% now "Y" %} Nuxis, Version: {% version %}
</div>
</div>

</footer>
</body>

</html>
Loading

0 comments on commit 05a0ca4

Please sign in to comment.