initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class TransactionsConfig(AppConfig):
|
||||
name = 'transactions'
|
||||
@@ -0,0 +1,14 @@
|
||||
from django import forms
|
||||
from apps.transactions.models import Transaction
|
||||
#from datetimepicker.widgets import DateTimePicker
|
||||
#from django.contrib.admin.widgets import AdminDateWidget
|
||||
|
||||
class TransactionForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Transaction
|
||||
fields = "__all__"
|
||||
|
||||
# https://stackoverflow.com/a/52702275/1632368
|
||||
#date_valid = forms.DateField(widget=AdminDateWidget())
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Generated by Django 3.1.4 on 2020-12-26 10:16
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Transaction',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('crypto_currency', models.CharField(max_length=10)),
|
||||
('amount_before_fee', models.DecimalField(decimal_places=10, max_digits=24)),
|
||||
('amount_after_fee', models.DecimalField(decimal_places=10, max_digits=24)),
|
||||
('exchange_rate', models.DecimalField(decimal_places=10, max_digits=24)),
|
||||
('fiat_currency', models.CharField(max_length=10)),
|
||||
('amount', models.DecimalField(decimal_places=10, max_digits=24)),
|
||||
('date_valid', models.DateTimeField(verbose_name='date transfered')),
|
||||
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'transaction',
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,15 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
# Create your models here.
|
||||
class Transaction(models.Model):
|
||||
crypto_currency = models.CharField(max_length=10)
|
||||
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10)
|
||||
amount_after_fee = models.DecimalField(max_digits=24, decimal_places=10)
|
||||
exchange_rate = models.DecimalField(max_digits=24, decimal_places=10)
|
||||
fiat_currency = models.CharField(max_length=10)
|
||||
amount = models.DecimalField(max_digits=24, decimal_places=10)
|
||||
date_valid = models.DateTimeField('date transfered')
|
||||
class Meta:
|
||||
db_table = "transaction"
|
||||
@@ -0,0 +1,90 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="form-box">
|
||||
<form method="POST" class="post-form" action="/transactions/add">
|
||||
{% csrf_token %}
|
||||
<div class="container">
|
||||
<br>
|
||||
{% if form.errors %}
|
||||
{% for field in form %}
|
||||
{% for error in field.errors %}
|
||||
<div class="alert alert-danger">
|
||||
<strong>{{field.name}}: {{ error|escape }}</strong>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% for error in form.non_field_errors %}
|
||||
<div class="alert alert-danger">
|
||||
<strong>{{ error|escape }}</strong>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-1 col-form-label"></label>
|
||||
<div class="col-sm-4">
|
||||
<h3>Add transaction</h3>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Crypto Currency:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.crypto_currency }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Amount before fee:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.amount_before_fee }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Amount after fee:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.amount_after_fee }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Exchange Rate:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.exchange_rate }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Fiat Currency:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.fiat_currency }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.amount }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Date valid:</label>
|
||||
<div class="col-sm-4">
|
||||
{{ form.date_valid }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-1 col-form-label"></label>
|
||||
<div class="col-sm-4">
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,90 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="form-box">
|
||||
<form method="POST" class="post-form" action="/transactions/update/{{transaction.id}}">
|
||||
{% csrf_token %}
|
||||
<div class="container">
|
||||
<br>
|
||||
{% if form.errors %}
|
||||
{% for field in form %}
|
||||
{% for error in field.errors %}
|
||||
<div class="alert alert-danger">
|
||||
<strong>{{field.name}}: {{ error|escape }}</strong>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% for error in form.non_field_errors %}
|
||||
<div class="alert alert-danger">
|
||||
<strong>{{ error|escape }}</strong>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-1 col-form-label"></label>
|
||||
<div class="col-sm-4">
|
||||
<h3>Update Transaction</h3>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Crypto Currency:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" name="crypto_currency" id="id_crypto_currency" required maxlength="10" value="{{ transaction.crypto_currency }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Amount before fee:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="decimal" name="amount_before_fee" id="id_amount_before_fee" required value="{{ transaction.amount_before_fee }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Amount after fee:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="decimal" name="amount_after_fee" id="id_amount_after_fee" required value="{{ transaction.amount_after_fee }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Exchange Rate:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="decimal" name="exchange_rate" id="id_exchange_rate" required value="{{ transaction.exchange_rate }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Fiat Currency:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" name="fiat_currency" id="id_fiat_currency" required maxlength="10" value="{{ transaction.fiat_currency }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Fiat Amount:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="decimal" name="amount" id="id_amount" required value="{{ transaction.amount }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-2 col-form-label">Date valid:</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" name="date_valid" id="id_date_valid" required value="{{ transaction.date_valid|date:'Y-m-d H:i:s' }}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group row">
|
||||
<label class="col-sm-1 col-form-label"></label>
|
||||
<div class="col-sm-4">
|
||||
<button type="submit" class="btn btn-success">Update</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,41 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load humanize %}
|
||||
|
||||
{% block content %}
|
||||
<div class="form-box">
|
||||
<center><a href="/transactions/add" class="button button-primary">Add New Record</a></center>
|
||||
<br/>
|
||||
<table class="table table-striped table-bordered table-sm">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Amount</th>
|
||||
<th>Value</th>
|
||||
<th colspan="2">Rate</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for transaction in transactions %}
|
||||
<tr>
|
||||
<td>{{ transaction.date_valid }}</td>
|
||||
<td>{{ transaction.amount_before_fee|floatformat:3 }} {{ transaction.crypto_currency }}</td>
|
||||
<td>{{ transaction.amount|floatformat:2 }} {{ transaction.fiat_currency }}</td>
|
||||
<td>{% if transaction.amount < 0 %}
|
||||
{{ transaction.exchange_rate|floatformat:0 }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{% if transaction.amount > 0 %}
|
||||
{{ transaction.exchange_rate|floatformat:0 }} {{ transaction.crypto_currency }}/{{ transaction.fiat_currency }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
<a href="/transactions/edit/{{ transaction.id }}"><span class="glyphicon glyphicon-pencil" >Edit</span></a>
|
||||
<a href="/transactions/delete/{{ transaction.id }}" onclick="return confirm('Are you sure?')">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,52 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from apps.transactions.forms import TransactionForm
|
||||
from apps.transactions.models import Transaction
|
||||
|
||||
@login_required
|
||||
def add(request):
|
||||
if request.method == "POST":
|
||||
# request.POST is immutable, so make a copy
|
||||
values = request.POST.copy()
|
||||
values['owner'] = request.user.id
|
||||
form = TransactionForm(values)
|
||||
if form.is_valid():
|
||||
try:
|
||||
form.save()
|
||||
return redirect('/transactions/show')
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
form = TransactionForm()
|
||||
return render(request,'add.html',{'form':form})
|
||||
|
||||
@login_required
|
||||
def show(request):
|
||||
if 'crypto' in request.GET:
|
||||
transactions = Transaction.objects.filter(owner=request.user, crypto_currency=request.GET['crypto']).order_by('-date_valid')
|
||||
else:
|
||||
transactions = Transaction.objects.filter(owner=request.user).order_by('-date_valid')
|
||||
return render(request,"show.html",{'transactions':transactions})
|
||||
|
||||
@login_required
|
||||
def edit(request, id):
|
||||
transaction = Transaction.objects.get(id=id, owner=request.user)
|
||||
return render(request,'edit.html', {'transaction':transaction})
|
||||
|
||||
@login_required
|
||||
def update(request, id):
|
||||
transaction = Transaction.objects.get(id=id, owner=request.user)
|
||||
# request.POST is immutable, so make a copy
|
||||
values = request.POST.copy()
|
||||
values['owner'] = request.user.id
|
||||
form = TransactionForm(values, instance = transaction)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect("/transactions/show")
|
||||
return render(request, 'edit.html', {'transaction': transaction, 'form': form})
|
||||
|
||||
@login_required
|
||||
def destroy(request, id):
|
||||
transaction = Transaction.objects.get(id=id, owner=request.user.id)
|
||||
transaction.delete()
|
||||
return redirect("/transactions/show")
|
||||
Reference in New Issue
Block a user