diff --git a/abonapp/locale/ru/LC_MESSAGES/django.po b/abonapp/locale/ru/LC_MESSAGES/django.po index f30a5f6..b0cf066 100644 --- a/abonapp/locale/ru/LC_MESSAGES/django.po +++ b/abonapp/locale/ru/LC_MESSAGES/django.po @@ -1159,3 +1159,6 @@ msgstr "IP успешно обновлён" msgid "IP address conflict" msgstr "IP адрес уже есть" + +msgid "Last connected service" +msgstr "Последняя подключённая услуга" diff --git a/abonapp/migrations/0008_auto_20181115_1206.py b/abonapp/migrations/0008_auto_20181115_1206.py new file mode 100644 index 0000000..8fb29e2 --- /dev/null +++ b/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) + ] diff --git a/abonapp/models.py b/abonapp/models.py index 79bfadc..7878895 100644 --- a/abonapp/models.py +++ b/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" diff --git a/abonapp/templates/abonapp/service.html b/abonapp/templates/abonapp/service.html index a1d5853..1a3bc6d 100644 --- a/abonapp/templates/abonapp/service.html +++ b/abonapp/templates/abonapp/service.html @@ -62,9 +62,8 @@
- {% trans 'Auto continue service.' %} - - ? -
{% if abon_tariff.tariff.descr %}{{ abon_tariff.tariff.descr }}
{% endif %} diff --git a/forward_pay.php b/forward_pay.php deleted file mode 100755 index 90bc3c6..0000000 --- a/forward_pay.php +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env php - 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); - -?> diff --git a/periodic.py b/periodic.py index b1963d0..f239c2c 100755 --- a/periodic.py +++ b/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: diff --git a/tariff_app/migrations/0003_auto_20181115_1206.py b/tariff_app/migrations/0003_auto_20181115_1206.py new file mode 100644 index 0000000..75882af --- /dev/null +++ b/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'}, + ), + ]