Browse Source

Makes autocontinue flag. When autoconnect service is enabled, end anough money, then connect last service tariff

devel
Dmitry Novikov 7 years ago
parent
commit
02404c8db6
  1. 3
      abonapp/locale/ru/LC_MESSAGES/django.po
  2. 34
      abonapp/migrations/0008_auto_20181115_1206.py
  3. 36
      abonapp/models.py
  4. 36
      abonapp/templates/abonapp/service.html
  5. 83
      forward_pay.php
  6. 31
      periodic.py
  7. 17
      tariff_app/migrations/0003_auto_20181115_1206.py

3
abonapp/locale/ru/LC_MESSAGES/django.po

@ -1159,3 +1159,6 @@ msgstr "IP успешно обновлён"
msgid "IP address conflict"
msgstr "IP адрес уже есть"
msgid "Last connected service"
msgstr "Последняя подключённая услуга"

34
abonapp/migrations/0008_auto_20181115_1206.py

@ -0,0 +1,34 @@
# Generated by Django 2.1 on 2018-11-15 12:06
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def fill_last_tariff(apps, _):
Abon = apps.get_model('abonapp', 'Abon')
for abon in Abon.objects.exclude(current_tariff=None):
abon.last_connected_tariff = abon.current_tariff.tariff
abon.save(update_fields=('last_connected_tariff',))
class Migration(migrations.Migration):
dependencies = [
('tariff_app', '0003_auto_20181115_1206'),
('abonapp', '0007_auto_20181101_1545'),
]
operations = [
migrations.AddField(
model_name='abon',
name='last_connected_tariff',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, to='tariff_app.Tariff', verbose_name='Last connected service'),
),
migrations.AlterField(
model_name='abonlog',
name='author',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL),
),
migrations.RunPython(fill_last_tariff)
]

36
abonapp/models.py

@ -23,7 +23,7 @@ from tariff_app.models import Tariff, PeriodicPay
class AbonLog(models.Model):
abon = models.ForeignKey('Abon', on_delete=models.CASCADE)
amount = models.FloatField(default=0.0)
author = models.ForeignKey(UserProfile, on_delete=models.CASCADE,
author = models.ForeignKey(UserProfile, on_delete=models.SET_NULL,
related_name='+', blank=True, null=True)
comment = models.CharField(max_length=128)
date = models.DateTimeField(auto_now_add=True)
@ -51,10 +51,6 @@ class AbonTariff(models.Model):
amount = self.tariff.amount
return round(amount, 2)
# is used service now, if time start is present than it activated
def is_started(self):
return False if self.time_start is None else True
def __str__(self):
return "%s: %s" % (
self.deadline,
@ -156,10 +152,10 @@ class Abon(BaseAccount):
_('Automatically connect next service'),
default=False
)
# last_connected_tariff = models.ForeignKey(
# Tariff, verbose_name=_('Last connected service'),
# on_delete=models.CASCADE, null=True, blank=True, default=None
# )
last_connected_tariff = models.ForeignKey(
Tariff, verbose_name=_('Last connected service'),
on_delete=models.SET_NULL, null=True, blank=True, default=None
)
MARKER_FLAGS = (
('icon_donkey', _('Donkey')),
@ -230,7 +226,8 @@ class Abon(BaseAccount):
if tariff.is_admin and author is not None:
if not author.is_staff:
raise LogicError(
_('User that is no staff can not buy admin services'))
_('User that is no staff can not buy admin services')
)
if self.current_tariff is not None:
if self.current_tariff.tariff == tariff:
@ -249,11 +246,17 @@ class Abon(BaseAccount):
deadline=deadline, tariff=tariff
)
self.current_tariff = new_abtar
if self.last_connected_tariff != tariff:
self.last_connected_tariff = tariff
# charge for the service
self.ballance -= amount
self.save(update_fields=('ballance', 'current_tariff'))
self.save(update_fields=(
'ballance',
'current_tariff',
'last_connected_tariff'
))
# make log about it
AbonLog.objects.create(
@ -366,7 +369,7 @@ class Abon(BaseAccount):
def enable_service(self, tariff: Tariff, deadline=None, time_start=None):
"""
Makes a services for current user
Makes a services for current user, without money
:param tariff: Instance of service
:param deadline: Time when service is expired
:param time_start: Time when service has started
@ -381,7 +384,8 @@ class Abon(BaseAccount):
time_start=time_start
)
self.current_tariff = new_abtar
self.save(update_fields=('current_tariff',))
self.last_connected_tariff = tariff
self.save(update_fields=('current_tariff', 'last_connected_tariff'))
class PassportInfo(models.Model):
@ -464,8 +468,10 @@ class AllTimePayLogManager(models.Manager):
if r is None:
break
summ, dat = r
yield {'summ': summ,
'pay_date': datetime.strptime(dat, '%Y-%m-%d')}
yield {
'summ': summ,
'pay_date': datetime.strptime(dat, '%Y-%m-%d')
}
# Log for pay system "AllTime"

36
abonapp/templates/abonapp/service.html

@ -62,9 +62,8 @@
<h3 class="panel-title">{% trans "Subscriber's service" %}</h3>
</div>
<div class="panel-body">
{% if abon_tariff %}
<dl class="dl-horizontal">
<dl class="dl-horizontal">
{% if abon_tariff %}
<dt>{% trans 'Service' %}</dt>
<dd>
{% if abon_tariff.tariff %}
@ -93,20 +92,27 @@
<dt>{% trans 'Works until' %}</dt>
<dd>{{ abon_tariff.deadline|date:"d E Y, l H:i" }}</dd>
</dl>
{% else %}
<dt>{% trans 'Subscriber has no service' %}</dt>
<dd>
<a href="{% url 'abonapp:pick_tariff' group.pk abon.username %}">
{% trans 'Buy service' %}
</a>
</dd>
{% endif %}
{% else %}
{% trans 'Subscriber has no service' %}.
<a href="{% url 'abonapp:pick_tariff' group.pk abon.username %}">
{% trans 'Buy service' %}
</a>
{% endif %}
{% if abon.last_connected_tariff %}
<dt>{% trans 'Last connected service' %}</dt>
<dd><a href="{{ abon.last_connected_tariff.get_absolute_url }}">{{ abon.last_connected_tariff.title }}</a></dd>
{% endif %}
<dt>{% trans 'Auto continue service.' %}</dt>
<dd>
<input type="checkbox" data-url="{% url 'abonapp:set_auto_continue_service' group.pk abon.username %}" class="autosave" {{ abon.autoconnect_service|yesno:'checked,' }}>
<a href="https://github.com/nerosketch/djing/blob/devel/docs/tarifs.md" title="{% trans 'Help' %}" target="_blank" data-toggle="tooltip">?</a>
</dd>
</dl>
<p>
{% trans 'Auto continue service.' %}
<input type="checkbox" data-url="{% url 'abonapp:set_auto_continue_service' group.pk abon.username %}" class="autosave" {{ abon.autoconnect_service|yesno:'checked,' }}>
<a href="https://github.com/nerosketch/djing/blob/master/docs/tarifs.md" title="{% trans 'Help' %}" target="_blank" data-toggle="tooltip">?</a>
</p>
{% if abon_tariff.tariff.descr %}
<p>{{ abon_tariff.tariff.descr }}</p>
{% endif %}

83
forward_pay.php

@ -1,83 +0,0 @@
#!/usr/bin/env php
<?php
define("API_AUTH_SECRET", "secret");
define("SERVER_DOMAIN", 'http://localhost:8000');
define('N', '
');
function calc_hash($str)
{
$hash = bin2hex(mhash(MHASH_SHA256,$str));
return $hash;
}
function make_sign($data)
{
asort($data, SORT_STRING);
array_push($data, API_AUTH_SECRET);
$str_to_hash = join('_', $data);
return calc_hash($str_to_hash);
}
function send_to($data)
{
$sign = make_sign($data);
$data['sign'] = $sign;
$url_params = http_build_query($data);
$r = file_get_contents(SERVER_DOMAIN."/abons/api/duplicate_pay/?".$url_params);
return $r;
}
function forward_pay_request($act, $pay_account, $service_id, $trade_point, $receipt_num, $pay_id, $pay_amount)
{
require('./users_uname_pk_pairs.php');
// $user_id_pairs
if($act == 1)
{
$pay = [
"ACT" => 1,
"PAY_ACCOUNT" => $user_id_pairs[$pay_account]
];
return send_to($pay);
}else if($act == 4)
{
$pay = [
"ACT" => 4,
"PAY_ACCOUNT" => $user_id_pairs[$pay_account],
"TRADE_POINT" => $trade_point,
"RECEIPT_NUM" => $receipt_num,
"PAY_ID" => $pay_id,
"PAY_AMOUNT" => $pay_amount,
"SERVICE_ID" => $service_id
];
return send_to($pay);
}else if($act == 7)
{
$pay = [
"ACT" => 7,
"PAY_ID" => $pay_id,
"SERVICE_ID" => $service_id
];
return send_to($pay);
}
}
# Request
echo forward_pay_request(1, '1234', null, null, null, null, null);
# Add cash
echo forward_pay_request('4', '1234', 'mypaysrv', '3432', '289473', '897879-989-68669', '1');
# check cash
echo forward_pay_request(7, null, 'mypaysrv', null, null, '897879-989-68669', null);
?>

31
periodic.py

@ -38,8 +38,10 @@ def main():
AbonTariff.objects.filter(abon=None).delete()
now = timezone.now()
fields = ('id', 'tariff__title', 'abon__id')
expired_services = AbonTariff.objects.exclude(abon=None).filter(deadline__lt=now,
abon__autoconnect_service=False)
expired_services = AbonTariff.objects.exclude(abon=None).filter(
deadline__lt=now,
abon__autoconnect_service=False
)
# finishing expires services
with transaction.atomic():
@ -57,8 +59,10 @@ def main():
expired_services.delete()
# Automatically connect new service
for ex in AbonTariff.objects.filter(deadline__lt=now, abon__autoconnect_service=True).exclude(
abon=None).iterator():
for ex in AbonTariff.objects.filter(
deadline__lt=now,
abon__autoconnect_service=True
).exclude(abon=None).iterator():
abon = ex.abon
trf = ex.tariff
amount = round(trf.amount, 2)
@ -68,7 +72,8 @@ def main():
abon.ballance -= amount
ex.time_start = now
ex.deadline = None # Deadline sets automatically in signal pre_save
ex.save(update_fields=('time_start', 'deadline'))
ex.is_active = True
ex.save(update_fields=('time_start', 'deadline', 'is_active'))
abon.save(update_fields=('ballance',))
# make log about it
l = AbonLog.objects.create(
@ -91,7 +96,19 @@ def main():
)
print(l.comment)
# signals.pre_delete.connect(abontariff_pre_delete, sender=AbonTariff)
# Post connect service
# connect service when autoconnect is True, and user have enough money
for ab in Abon.objects.filter(
is_active=True,
current_tariff=None
).exclude(last_connected_tariff=None).iterator():
tariff = ab.last_connected_tariff
if tariff is None:
continue
ab.pick_tariff(
tariff, None,
"Автоматическое продление услуги '%s'" % tariff.title
)
# manage periodic pays
ppays = PeriodicPayForId.objects.filter(next_pay__lt=now) \
@ -102,7 +119,7 @@ def main():
# sync subscribers on GW
threads = tuple(NasSyncThread(nas) for nas in NASModel.objects.
annotate(usercount=Count('abon')).
filter(usercount__gt=0))
filter(usercount__gt=0, enabled=True))
for t in threads:
t.start()
for t in threads:

17
tariff_app/migrations/0003_auto_20181115_1206.py

@ -0,0 +1,17 @@
# Generated by Django 2.1 on 2018-11-15 12:06
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('tariff_app', '0002_auto_20180807_1548'),
]
operations = [
migrations.AlterModelOptions(
name='periodicpay',
options={'ordering': ('-id',), 'verbose_name': 'Periodic pay', 'verbose_name_plural': 'Periodic pays'},
),
]
Loading…
Cancel
Save