diff --git a/pacmanager/core/admin.py b/pacmanager/core/admin.py index c0a7f84..bf2da82 100644 --- a/pacmanager/core/admin.py +++ b/pacmanager/core/admin.py @@ -2,7 +2,7 @@ from django.contrib import admin from .models import Setting, Character, Corporation, Key, Transaction, MonthTotal, APICache class SettingAdmin(admin.ModelAdmin): - pass + list_display = ('key', 'value') class CharacterAdmin(admin.ModelAdmin): list_display = ('id', 'name') @@ -33,6 +33,7 @@ class MonthTotalAdmin(admin.ModelAdmin): class APICacheAdmin(admin.ModelAdmin): list_display = ('key', 'cache_until') + admin.site.register(Setting, SettingAdmin) admin.site.register(Character, CharacterAdmin) admin.site.register(Corporation, CorporationAdmin) diff --git a/pacmanager/core/management/__init__.py b/pacmanager/core/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pacmanager/core/management/commands/__init__.py b/pacmanager/core/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pacmanager/core/management/commands/updatecorps.py b/pacmanager/core/management/commands/updatecorps.py new file mode 100644 index 0000000..444429a --- /dev/null +++ b/pacmanager/core/management/commands/updatecorps.py @@ -0,0 +1,11 @@ +import logging +from django.core.management.base import BaseCommand, CommandError +from core.tasks import process_corps + +class Command(BaseCommand): + args = '' + help = 'Process all corporation wallet journals' + + def handle(self, *args, **options): + logging.basicConfig(level=logging.INFO) + process_corps() diff --git a/pacmanager/core/management/commands/updatepayments.py b/pacmanager/core/management/commands/updatepayments.py new file mode 100644 index 0000000..974c241 --- /dev/null +++ b/pacmanager/core/management/commands/updatepayments.py @@ -0,0 +1,11 @@ +import logging +from django.core.management.base import BaseCommand, CommandError +from core.tasks import process_pac_wallet + +class Command(BaseCommand): + args = '' + help = 'Process the payments wallet for new payments' + + def handle(self, *args, **options): + logging.basicConfig(level=logging.INFO) + process_pac_wallet() diff --git a/pacmanager/core/models.py b/pacmanager/core/models.py index 9151d8f..2efd09b 100644 --- a/pacmanager/core/models.py +++ b/pacmanager/core/models.py @@ -16,6 +16,9 @@ class Setting(models.Model): key = models.CharField(max_length=32) value = models.CharField(max_length=200) + def __unicode__(self): + return self.key + class Character(models.Model): name = models.CharField(max_length=255) @@ -33,7 +36,7 @@ class Corporation(models.Model): balance = models.DecimalField('Current Balance', max_digits=25, decimal_places=2, default=0) last_transaction = models.BigIntegerField('Last Transaction ID', default=0) - payment_id = models.CharField('Payment ID', max_length=36) + payment_id = models.CharField('Payment ID', max_length=36, default=uuid4) objects = CorporationManager() @@ -42,11 +45,6 @@ class Corporation(models.Model): return u'%s' % self.name return u'%s' % self.pk - def save(self, *args, **kwargs): - if self.payment_id is None or self.payment_id == '': - self.payment_id = uuid4() - return super(Corporation, self).save(*args, **kwargs) - class MonthTotal(models.Model): """Holds the monthly fee totals for a corporation""" @@ -176,4 +174,7 @@ class APICache(models.Model): if obj.cache_until >= datetime.utcnow().replace(tzinfo=utc): return obj.document return None + + def __unicode__(self): + return self.key diff --git a/pacmanager/core/tasks.py b/pacmanager/core/tasks.py index 8cba571..75f1e87 100644 --- a/pacmanager/core/tasks.py +++ b/pacmanager/core/tasks.py @@ -6,7 +6,7 @@ from django.utils.timezone import utc from eveapi import EVEAPIConnection, Error, Rowset from .conf import managerconf -from .models import Corporation, APICache, MonthTotal +from .models import Corporation, APICache, MonthTotal, Transaction def import_wallet_journal(corporation_id): corp = Corporation.objects.get(pk=corporation_id) @@ -18,7 +18,7 @@ def import_wallet_journal(corporation_id): def get_records(corp=None, fromID=None, rowCount=2560): if fromID and fromID <= corp.last_transaction: return None - print "get_records: ", corp, fromID + #print "get_records: ", corp, fromID try: res = auth.corp.WalletJournal(rowCount=rowCount, fromID=fromID) except Error, e: @@ -35,12 +35,12 @@ def import_wallet_journal(corporation_id): # Process Rows rows = get_records(corp).SortedBy('refID', reverse=True) - print "Total rows: ", len(rows) + logging.info("Total rows: %s" % len(rows)) totals = {} for record in rows: if int(record.refID) > corp.last_transaction: if int(record.refTypeID) in getattr(settings, 'PAC_TAX_REFIDS', [85, 99]): - print record.refID, int(record.refTypeID), record.amount + #print record.refID, int(record.refTypeID), record.amount dt = datetime.fromtimestamp(record.date).replace(tzinfo=utc) if not totals.has_key('%s-%s' % (dt.year, dt.month)): totals['%s-%s' % (dt.year, dt.month)], created = MonthTotal.objects.get_or_create(corporation=corp, year=dt.year, month=dt.month) @@ -67,15 +67,33 @@ def process_pac_wallet(): for corp in Corporation.objects.all(): paymentid[corp.payment_id] = corp + if not 'payments.keyid' in managerconf or not 'payments.vcode' in managerconf: + logging.error('No payments Key ID / vCode set!') + return + + api = EVEAPIConnection(cacheHandler=APICache.DjangoCacheHandler()) auth = api.auth(keyID=managerconf['payments.keyid'], vCode=managerconf['payments.vcode']) try: - res = auth.corp.WalletJournal(rowCount=2560) + entries = auth.corp.WalletJournal(rowCount=2560).entries.SortedBy('refID', reverse=True) except Error, e: - print e + logging.error('Error importing Payments wallet: %s' % e) + return - for record in res.entries: - if int(record.refID) > managerconf['payments.last_id'] and record.reason.replace('DESC:', '') in paymentid.keys(): - continue + if not 'payments.last_id' in managerconf: + managerconf['payments.last_id'] = 0 + + logging.info('Last processed Ref ID: %s' % managerconf['payments.last_id']) + for record in entries: + if int(record.refID) > int(managerconf['payments.last_id']): + res = record.reason.replace('DESC: ', '').strip() + if paymentid.has_key(res): + corp = paymentid[res] + logging.info('Payment Found, %s, Ref ID %s, Amount: %s ISK' % (corp.name, record.refID, record.amount)) + # Payment found + trans = Transaction.objects.create(corporation=corp, value=record.amount, comment="Wallet Ref ID %s" % record.refID, type=Transaction.TRANSACTION_TYPE_PAYMENT) + corp.balance += Decimal(str(record.amount)) + corp.save() else: break + managerconf['payments.last_id'] = entries[0].refID diff --git a/pacmanager/core/templates/core/corporation_detail.html b/pacmanager/core/templates/core/corporation_detail.html index e28b442..c4e114e 100644 --- a/pacmanager/core/templates/core/corporation_detail.html +++ b/pacmanager/core/templates/core/corporation_detail.html @@ -24,11 +24,12 @@ span.negative { color: red; } {% endfor %} {% else %} -

No wallet balance information is available at this time.

+

No wallet balance information is available at this time.

{% endif %} -

 

- +

Balance Payment

+

To make a payment towards this corporation's fees, send money to {{ payment_corp }} with {{ object.payment_id }} as the "Reason" field.

+

 

{% if object.totals.count %}

Tax Period Overview

Total transactions by taxable periods and related fees

diff --git a/pacmanager/core/templates/core/corporation_list.html b/pacmanager/core/templates/core/corporation_list.html index 2c49cf3..1818bb1 100644 --- a/pacmanager/core/templates/core/corporation_list.html +++ b/pacmanager/core/templates/core/corporation_list.html @@ -29,6 +29,6 @@

{% else %} -

No corporations are currently available, consider adding a API key.

+

No corporations are currently available to you{% if perms.add_key %}, consider adding a API key{% endif %}.

{% endif %} {% endblock %} diff --git a/pacmanager/core/templates/core/key_list.html b/pacmanager/core/templates/core/key_list.html index 167eec3..8c55566 100644 --- a/pacmanager/core/templates/core/key_list.html +++ b/pacmanager/core/templates/core/key_list.html @@ -23,7 +23,9 @@

No keys are currently stored.

{% endif %} +{% if perms.add_key %}

Add Key

+{% endif %} {% endblock %} {% block footer %} diff --git a/pacmanager/core/views.py b/pacmanager/core/views.py index 493b9ed..2d7d656 100644 --- a/pacmanager/core/views.py +++ b/pacmanager/core/views.py @@ -16,6 +16,7 @@ from braces.views import LoginRequiredMixin, PermissionRequiredMixin from .tasks import import_wallet_journal from .forms import KeyForm, CorporationContactForm, ManualAdjustmentForm from .models import Corporation, Key, APICache, Transaction, MonthTotal +from .conf import managerconf class KeyListView(LoginRequiredMixin, ListView): @@ -128,6 +129,7 @@ class CorporationDetailView(LoginRequiredMixin, DetailView): ctx.update({ 'balances': balances, 'last_update': last_update, + 'payment_corp': managerconf.get('payment.corpname', 'Upvote'), }) return ctx diff --git a/pacmanager/pacmanager/templates/base.html b/pacmanager/pacmanager/templates/base.html index b4eda7e..4cd5359 100644 --- a/pacmanager/pacmanager/templates/base.html +++ b/pacmanager/pacmanager/templates/base.html @@ -10,6 +10,7 @@