parent
7c39828a4f
commit
833491f8c1
17 changed files with 348 additions and 66 deletions
@ -0,0 +1,20 @@ |
||||
# -*- coding: utf-8 -*- |
||||
# Generated by Django 1.9.7 on 2016-08-09 16:56 |
||||
from __future__ import unicode_literals |
||||
|
||||
from django.db import migrations, models |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('common', '0004_auto_20160808_1557'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AddField( |
||||
model_name='settings', |
||||
name='accountant_send_email', |
||||
field=models.EmailField(default='muhtarzubanchi05@gmail.com', max_length=100), |
||||
), |
||||
] |
||||
@ -1,5 +1,15 @@ |
||||
from django.contrib import admin |
||||
|
||||
from .models import InvoiceHistory |
||||
from .models import InvoiceHistory, WithDraw |
||||
|
||||
admin.site.register(InvoiceHistory) |
||||
|
||||
class InvoiceHistoryAdmin(admin.ModelAdmin): |
||||
list_display = ('comment', 'sum', 'user','balance',) |
||||
|
||||
|
||||
class WithDrawAdmin(admin.ModelAdmin): |
||||
list_display = ('sum','created','user',) |
||||
|
||||
|
||||
admin.site.register(InvoiceHistory, InvoiceHistoryAdmin) |
||||
admin.site.register(WithDraw, WithDrawAdmin) |
||||
|
||||
@ -0,0 +1,13 @@ |
||||
from django import forms |
||||
from .models import WithDraw |
||||
|
||||
|
||||
class WithDrawForm(forms.ModelForm): |
||||
|
||||
class Meta: |
||||
model = WithDraw |
||||
fields = ( |
||||
'sum', |
||||
'yandex_card', |
||||
'user', |
||||
) |
||||
@ -0,0 +1,20 @@ |
||||
# -*- coding: utf-8 -*- |
||||
# Generated by Django 1.9.7 on 2016-08-09 09:22 |
||||
from __future__ import unicode_literals |
||||
|
||||
from django.db import migrations, models |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('wallets', '0003_auto_20160729_1209'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AddField( |
||||
model_name='invoicehistory', |
||||
name='balance', |
||||
field=models.DecimalField(decimal_places=0, default=0, max_digits=10), |
||||
), |
||||
] |
||||
@ -0,0 +1,43 @@ |
||||
# -*- coding: utf-8 -*- |
||||
# Generated by Django 1.9.7 on 2016-08-09 14:27 |
||||
from __future__ import unicode_literals |
||||
|
||||
from django.conf import settings |
||||
from django.db import migrations, models |
||||
import django.db.models.deletion |
||||
import django.utils.timezone |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), |
||||
('wallets', '0004_invoicehistory_balance'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.CreateModel( |
||||
name='WithDraw', |
||||
fields=[ |
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
('sum', models.DecimalField(decimal_places=0, max_digits=10)), |
||||
('created', models.DateTimeField(default=django.utils.timezone.now)), |
||||
('yandex_card', models.CharField(max_length=30)), |
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='with_draw', to=settings.AUTH_USER_MODEL)), |
||||
], |
||||
options={ |
||||
'verbose_name_plural': 'Заявки на вывод средств', |
||||
'ordering': ('-created',), |
||||
'verbose_name': 'Заявка на вывод средств', |
||||
}, |
||||
), |
||||
migrations.AlterModelOptions( |
||||
name='invoicehistory', |
||||
options={'ordering': ('-created',), 'verbose_name': 'Счет(История)', 'verbose_name_plural': 'Счет(История)'}, |
||||
), |
||||
migrations.AlterField( |
||||
model_name='invoicehistory', |
||||
name='type', |
||||
field=models.CharField(blank=True, max_length=20, null=True), |
||||
), |
||||
] |
||||
@ -0,0 +1,20 @@ |
||||
from django.db.models.signals import post_save |
||||
from django.dispatch import receiver |
||||
from django.core.mail import send_mail, EmailMultiAlternatives |
||||
from django.template.loader import get_template, render_to_string |
||||
|
||||
from .models import WithDraw |
||||
|
||||
|
||||
@receiver(post_save, sender=WithDraw) |
||||
def send_for_accountant(sender, instance, created, **kwargs): |
||||
|
||||
if created: |
||||
ctx_dict = { |
||||
'username': instance.user.username, |
||||
} |
||||
subject, from_email, to = 'Заявка на распечатку', 'mukhtar@mukhtar', 'muhtarzubanchi05@gmail.com' |
||||
text_content = render_to_string('send_for_accountant.txt', ctx_dict) |
||||
html_content = get_template('send_for_accountant.html').render(ctx_dict) |
||||
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) |
||||
msg.send() |
||||
@ -0,0 +1 @@ |
||||
Заявка на вывод средств от {{ username }}! |
||||
@ -0,0 +1,2 @@ |
||||
Заявка на распечатку от {{ username }}! |
||||
|
||||
@ -1,9 +1,10 @@ |
||||
from django.conf import urls |
||||
from django.conf.urls import include |
||||
from .views import ScoreDetailView |
||||
from .views import ScoreDetailView, WithDrawCreate |
||||
|
||||
app_name = 'wallets' |
||||
|
||||
urlpatterns = [ |
||||
urls.url(r'^score/(?P<pk>\d+)/$', ScoreDetailView.as_view(), name='score-detail'), |
||||
urls.url(r'^withdraw/create/$', WithDrawCreate.as_view(), name='withdraw-create'), |
||||
|
||||
] |
||||
|
||||
@ -1,9 +1,69 @@ |
||||
from django.shortcuts import render |
||||
from django.views.generic import DetailView |
||||
from django.http import JsonResponse |
||||
from django.contrib import messages |
||||
from django.db.models import Sum |
||||
from django.views.generic import DetailView, CreateView |
||||
from users.models import User |
||||
from users.mixins import CheckForUserMixin |
||||
from .models import InvoiceHistory, WithDraw |
||||
from .forms import WithDrawForm |
||||
|
||||
|
||||
class ScoreDetailView(DetailView): |
||||
class ScoreDetailView(CheckForUserMixin, DetailView): |
||||
model = User |
||||
template_name = 'score-detail.html' |
||||
context_object_name = 'user_score' |
||||
|
||||
def get_context_data(self, **kwargs): |
||||
context = super().get_context_data(**kwargs) |
||||
user_score_balance = InvoiceHistory.objects.filter(user=self.get_object()).aggregate(Sum('sum')) |
||||
context['user_score_balance'] = user_score_balance['sum__sum'] or 0 |
||||
context['form'] = WithDrawForm |
||||
return context |
||||
|
||||
|
||||
class AjaxableResponseMixin(object): |
||||
def form_invalid(self, form): |
||||
response = super().form_invalid(form) |
||||
if self.request.is_ajax(): |
||||
return JsonResponse(form.errors, status=400) |
||||
else: |
||||
return response |
||||
|
||||
def form_valid(self, form): |
||||
# import code; code.interact(local=dict(globals(), **locals())) |
||||
response = super(AjaxableResponseMixin, self).form_valid(form) |
||||
if self.request.is_ajax(): |
||||
messages.info(self.request, 'Ваша заявка на вывод средств принята!') |
||||
data = { |
||||
'pk': self.object.pk, |
||||
'status': 'ok', |
||||
} |
||||
return JsonResponse(data) |
||||
else: |
||||
return response |
||||
|
||||
|
||||
# class WithDrawCreate(AjaxableResponseMixin, CreateView): |
||||
# model = WithDraw |
||||
# form_class = WithDrawForm |
||||
# success_url = '/' |
||||
|
||||
class WithDrawCreate(CreateView): |
||||
model = WithDraw |
||||
form_class = WithDrawForm |
||||
|
||||
def form_valid(self, form): |
||||
if self.request.is_ajax(): |
||||
self.object = form.save() |
||||
messages.info(self.request, 'Ваша заявка на вывод средств принята!') |
||||
data = { |
||||
'pk': self.object.pk, |
||||
'status': 'ok', |
||||
} |
||||
return JsonResponse(data) |
||||
return super().form_valid(form) |
||||
|
||||
def form_invalid(self, form): |
||||
if self.request.is_ajax(): |
||||
return JsonResponse(form.errors, status=400) |
||||
return super().form_invalid(form) |
||||
|
||||
Loading…
Reference in new issue