Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Python 3 and Django 3 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ First, make sure you have Django and NetworkX installed.

Change directory to `<repository_root>/buddyledger/src` (the one containing `manage.py`).

Create `buddyledger/settings.py` relative to current directory (example content):
Create `buddyledger/environment_settings.py` relative to current directory (example content):
```
ROOT_URLCONF="buddyledger.urls"
INSTALLED_APPS=(
Expand All @@ -30,7 +30,7 @@ INSTALLED_APPS=(
'buddyledger'
)

TEMPLATE_DIRS=("templates")
TEMPLATE_DIRS=("templates",)

DEBUG = True
DEFAULT_FROM_EMAIL = '[email protected]'
Expand All @@ -46,8 +46,8 @@ DATABASES = {

Now execute (example for Windows):

c:\Python33\python manage.py syncdb
c:\Python33\python manage.py getcurrency
c:\Python33\python manage.py runserver
c:\Python38\python manage.py migrate
c:\Python38\python manage.py getcurrency
c:\Python38\python manage.py runserver

The web application is available at http://localhost:8000/ .
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
networkx==1.10
Django==1.8.2
django-bootstrap3==5.4.0
networkx==2.5
Django==3.1.*
django-bootstrap4==1.1.*
2 changes: 1 addition & 1 deletion src/buddyledger/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get_expense_parts(self):
fielddict[fieldname] = value

expenseparts = dict()
for fieldname,value in fielddict.iteritems():
for fieldname,value in fielddict.items():
### get the userid from the expensepart field
if fieldname.startswith('person-expensepart-') and value == True:
userid = fieldname[19:]
Expand Down
65 changes: 41 additions & 24 deletions src/buddyledger/management/commands/getcurrency.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
from django.core.management.base import BaseCommand, CommandError
from buddyledger.models import Currency
from decimal import *
import urllib, json
import xml.etree.ElementTree as etree
from decimal import *

try:
from urllib.request import urlopen
except ImportError:
from urllib import urlopen

def fetch():
f = urlopen('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
xml = f.read()
f.close()
tree = etree.fromstring(xml)
cube = tree[2][0]
eur_rates = {}
for child in cube:
a = child.attrib
rate = float(a['rate'])
code = a['currency']
eur_rates[code] = rate

dkk_per_eur = 1 / eur_rates['DKK']

for code, rate in eur_rates.items():
if code == "DKK": continue
yield code, rate * dkk_per_eur
yield "EUR", dkk_per_eur

if __name__ == "__main__":
import pprint, sys
gen = fetch()
pprint.pprint(list(gen))
sys.exit(0)

from django.core.management.base import BaseCommand, CommandError
from buddyledger.models import Currency

class Command(BaseCommand):
help = 'Gets currency exchange rates from nationalbanken'

def handle(self, *args, **options):
f = urlopen('http://www.nationalbanken.dk/_vti_bin/DN/DataService.svc/CurrencyRatesXML?lang=da')
xml = f.read()
f.close()
tree = etree.fromstring(xml)
for child in tree[0]:
if child.attrib['rate'] != '-':
rate = float(child.attrib['rate'].replace(".", "").replace(",", "."))/100

try:
currency = Currency.objects.get(iso4217_code=child.attrib['code'])
currency.dkk_price=rate
temp = ""
except Currency.DoesNotExist:
currency = Currency(iso4217_code=child.attrib['code'],dkk_price=rate)
temp = " new"

currency.save()
self.stdout.write('Saved%s rate: 1 %s costs %s DKK' % (temp, child.attrib['code'],rate))
else:
self.stdout.write('Skipping currency %s - no price found' % child.attrib['code'])
for code, rate in fetch():
try:
currency = Currency.objects.get(iso4217_code=code)
currency.dkk_price = rate
temp = ""
except Currency.DoesNotExist:
currency = Currency(iso4217_code=code, dkk_price=rate)
temp = " new"

self.stdout.write('Saved%s rate: 1 %s costs %s DKK' % (temp, code, rate))
currency.save()


###########################################################################################
Expand All @@ -49,4 +67,3 @@ def handle(self, *args, **options):

###########################################################################################
self.stdout.write('Done getting currencies.')

64 changes: 33 additions & 31 deletions src/buddyledger/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# Generated by Django 3.0.5 on 2020-04-16 21:40

from django.db import models, migrations
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Currency',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('iso4217_code', models.CharField(max_length=3)),
('dkk_price', models.DecimalField(max_digits=20, decimal_places=2)),
('dkk_price', models.DecimalField(decimal_places=2, max_digits=20)),
],
options={
'ordering': ('iso4217_code',),
Expand All @@ -24,62 +26,62 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Expense',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('amount', models.DecimalField(max_digits=20, decimal_places=2)),
('amount_native', models.DecimalField(editable=False, max_digits=20, decimal_places=2)),
('amount', models.DecimalField(decimal_places=2, max_digits=20)),
('amount_native', models.DecimalField(decimal_places=2, editable=False, max_digits=20)),
('date', models.DateField()),
('currency', models.ForeignKey(to='buddyledger.Currency')),
('currency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='buddyledger.Currency')),
],
options={
'ordering': ('date', 'id'),
},
),
migrations.CreateModel(
name='ExpensePart',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('haspaid', models.DecimalField(null=True, max_digits=20, decimal_places=2, blank=True)),
('haspaid_native', models.DecimalField(null=True, max_digits=20, decimal_places=2, blank=True)),
('shouldpay', models.DecimalField(null=True, max_digits=20, decimal_places=2, blank=True)),
('shouldpay_native', models.DecimalField(null=True, max_digits=20, decimal_places=2, blank=True)),
('expense', models.ForeignKey(to='buddyledger.Expense')),
],
),
migrations.CreateModel(
name='Ledger',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('closed', models.BooleanField(default=False, editable=False)),
('calcmethod', models.CharField(max_length=20, editable=False)),
('currency', models.ForeignKey(to='buddyledger.Currency')),
('calcmethod', models.CharField(default='basic', editable=False, max_length=20)),
('currency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='buddyledger.Currency')),
],
),
migrations.CreateModel(
name='Person',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('ledger', models.ForeignKey(editable=False, to='buddyledger.Ledger')),
('ledger', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='buddyledger.Ledger')),
],
options={
'ordering': ('name',),
},
),
migrations.AddField(
model_name='expensepart',
name='person',
field=models.ForeignKey(to='buddyledger.Person'),
migrations.CreateModel(
name='ExpensePart',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('haspaid', models.DecimalField(blank=True, decimal_places=2, max_digits=20, null=True)),
('haspaid_native', models.DecimalField(blank=True, decimal_places=2, max_digits=20, null=True)),
('shouldpay', models.DecimalField(blank=True, decimal_places=2, max_digits=20, null=True)),
('shouldpay_native', models.DecimalField(blank=True, decimal_places=2, max_digits=20, null=True)),
('autoamount', models.BooleanField(default=False)),
('expense', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='expenseparts', to='buddyledger.Expense')),
('person', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='buddyledger.Person')),
],
options={
'unique_together': {('expense', 'person')},
},
),
migrations.AddField(
model_name='expense',
name='ledger',
field=models.ForeignKey(editable=False, to='buddyledger.Ledger'),
field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='buddyledger.Ledger'),
),
migrations.AddField(
model_name='expense',
name='people',
field=models.ManyToManyField(to='buddyledger.Person', through='buddyledger.ExpensePart'),
field=models.ManyToManyField(through='buddyledger.ExpensePart', to='buddyledger.Person'),
),
]
39 changes: 0 additions & 39 deletions src/buddyledger/migrations/0002_auto_20150815_1640.py

This file was deleted.

18 changes: 9 additions & 9 deletions src/buddyledger/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

class Ledger(models.Model):
name = models.CharField(max_length=100)
currency = models.ForeignKey('Currency')
currency = models.ForeignKey('Currency', on_delete=models.CASCADE)
closed = models.BooleanField(default=False, editable=False)
calcmethod = models.CharField(max_length=20, editable=False, default="basic")

def __unicode__(self):
def __str__(self):
return self.name


class Person(models.Model):
name = models.CharField(max_length=100)
ledger = models.ForeignKey(Ledger,editable=False)
ledger = models.ForeignKey(Ledger, editable=False, on_delete=models.CASCADE)

def __unicode__(self):
def __str__(self):
return self.name

class Meta:
Expand All @@ -24,21 +24,21 @@ class Meta:
class Expense(models.Model):
name = models.CharField(max_length=100)
amount = models.DecimalField(max_digits=20, decimal_places=2)
currency = models.ForeignKey('Currency')
currency = models.ForeignKey('Currency', on_delete=models.CASCADE)
amount_native = models.DecimalField(max_digits=20, decimal_places=2, editable=False)
ledger = models.ForeignKey('Ledger',editable=False)
ledger = models.ForeignKey('Ledger', editable=False, on_delete=models.CASCADE)
people = models.ManyToManyField('Person', through='ExpensePart')
date = models.DateField()

def __unicode__(self):
def __str__(self):
return self.name

class Meta:
ordering = ('date','id',)


class ExpensePart(models.Model):
expense = models.ForeignKey(Expense, related_name="expenseparts")
expense = models.ForeignKey(Expense, related_name="expenseparts", on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.PROTECT)
haspaid = models.DecimalField(max_digits=20, decimal_places=2,null=True,blank=True)
haspaid_native = models.DecimalField(max_digits=20, decimal_places=2,null=True,blank=True)
Expand All @@ -54,7 +54,7 @@ class Currency(models.Model):
iso4217_code = models.CharField(max_length=3)
dkk_price = models.DecimalField(max_digits=20, decimal_places=2)

def __unicode__(self):
def __str__(self):
return self.iso4217_code

class Meta:
Expand Down
13 changes: 1 addition & 12 deletions src/buddyledger/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.conf import global_settings
from environment_settings import *
from .environment_settings import *
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Application definition
Expand Down Expand Up @@ -42,17 +42,6 @@
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
],
},
},
]

Expand Down
2 changes: 1 addition & 1 deletion src/buddyledger/templates/add_expense.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h4>People</h4>
<div id="controlgroup-paymentamount-{{person.id}}" class="control-group">
<div class="controls">
<div class="input-append">
<input id="paymentamount-{{person.id}}" class="input-small" name="person-paymentamount-{{person.id}}" type="number" placeholder="0">
<input id="paymentamount-{{person.id}}" class="input-small" name="person-paymentamount-{{person.id}}" type="number" step="0.01" placeholder="0">
<span class="add-on currencylabel">{{ ledger.currency.iso4217_code }}</span>
</div>
</div>
Expand Down
Loading