Skip to content

Commit

Permalink
Frontend added with login completed crud apis completed and some ui t…
Browse files Browse the repository at this point in the history
…hings like tables and form to add new spending completed
  • Loading branch information
supsa-ak committed Dec 19, 2021
1 parent 2bc7068 commit df96989
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 31 deletions.
3 changes: 2 additions & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib import admin
from .models import Transaction

# Register your models here.
admin.site.register(Transaction)
29 changes: 29 additions & 0 deletions api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.0 on 2021-12-19 18:34

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


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='Transaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('name', models.CharField(max_length=500)),
('amount', models.IntegerField()),
('category', models.CharField(choices=[('ren', 'Rent'), ('tra', 'Travel'), ('inv', 'Investment'), ('sho', 'Shopping'), ('lea', 'Learning'), ('fee', 'Fees'), ('oth', 'Other')], default='oth', max_length=3)),
('note', models.TextField()),
('username', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.user')),
],
),
]
19 changes: 19 additions & 0 deletions api/migrations/0002_alter_transaction_created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0 on 2021-12-19 18:43

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='transaction',
name='created',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.0 on 2021-12-20 06:34

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


class Migration(migrations.Migration):

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('api', '0002_alter_transaction_created'),
]

operations = [
migrations.RemoveField(
model_name='transaction',
name='username',
),
migrations.AddField(
model_name='transaction',
name='user',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='auth.user'),
),
]
22 changes: 22 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
from django.utils.timezone import now
from django.db import models
from django.contrib.auth.models import User

class Transaction(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
created = models.DateTimeField(default=now, editable=True)
name = models.CharField(max_length=500)
amount = models.IntegerField()
categories = [
('ren', 'Rent'),
('tra', 'Travel'),
('inv', 'Investment'),
('sho', 'Shopping'),
('lea', 'Learning'),
('fee', 'Fees'),
('oth', 'Other'),
]
category = models.CharField(max_length=3, default='oth', choices=categories)
note = models.TextField()

def __str__(self):
return (str(self.user) + ' ' + str(self.amount))
17 changes: 17 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import serializers
from .models import Transaction

class TransactionSearializer(serializers.ModelSerializer):
class Meta:
model = Transaction
fields = ('id', 'user', 'created', 'name', 'amount', 'category', 'note')

# class CreateTransactionSerializer(serializers.ModelSerializer):
# class Meta:
# model = Transaction
# fields = ('user', 'name', 'amount', 'category', 'note')

# class UpdateTransactionSerializer(serializers.ModelSerializer):
# class Meta:
# model = Transaction
# fields = ('id', 'name', 'amount', 'category', 'note')
7 changes: 5 additions & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.urls import path

from .views import *

urlpatterns = [
# path('', home),
path('', TransactionView.as_view()),
path('create-transaction', CreateTransactionView.as_view()),
path('update-transaction', UpdateTransactionView.as_view()),
path('delete-transaction', DeleteTransactionView.as_view()),
]
64 changes: 64 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from rest_framework import generics, status
from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from .serializers import *
from .models import Transaction
from rest_framework.views import APIView
from rest_framework.response import Response
import json

class TransactionView(generics.ListAPIView):
def get(self, request, format=None):
serializer_class = TransactionSearializer
queryset = Transaction.objects.filter(user=request.user)
dat = []
for i in queryset:
dat.append(TransactionSearializer(i).data)
return Response({'data':dat}, status=status.HTTP_200_OK)

class CreateTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:

user = request.user
name = request.data.get('name')
amount = request.data.get('amount')
category = request.data.get('category')
note = request.data.get('note')

transaction = Transaction(user=user, name=name, amount=amount, category=category, note=note)
transaction.save()

return Response(TransactionSearializer(transaction).data, status=status.HTTP_201_CREATED)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)

class UpdateTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:
id = request.data.get('id')

queryset = Transaction.objects.filter(id=id)
transaction = queryset[0]
transaction.name = request.data.get('name')
transaction.amount = request.data.get('amount')
transaction.category = request.data.get('category')
transaction.note = request.data.get('note')
transaction.save(update_fields=['name','amount','category','note'])

return Response(TransactionSearializer(transaction).data, status=status.HTTP_200_OK)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)


class DeleteTransactionView(APIView):
def post(self, request, format=None):
if request.user.is_authenticated:
id = request.data.get('id')

queryset = Transaction.objects.filter(id=id)
transaction = queryset[0]
transaction.delete()

return Response({'message':'Deleted Successfully'}, status=status.HTTP_200_OK)
return Response({'message':'Login Required'}, status=status.HTTP_404_NOT_FOUND)
Binary file modified db.sqlite3
Binary file not shown.
2 changes: 0 additions & 2 deletions frontend/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.db import models

# Create your models here.
Empty file.
16 changes: 0 additions & 16 deletions frontend/templates/frontend/home.html

This file was deleted.

162 changes: 162 additions & 0 deletions frontend/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BudMan - BudMan is a web app that helps you track and adjust your spending so that you are in control of
money.</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<!-- Font Awesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet" />
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<!-- MDB -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.10.1/mdb.min.css" rel="stylesheet" />

</head>

<body>
{% if user.is_authenticated %}
<h3 style="display: inline;">Logged in as </h3>
<h3 style="color: green; display: inline;">{{request.user}}</h3>
<a href="/logout">Logout</a>
<h1>Spendings</h1>

<div class="container">

<button class="btn btn-primary " type="button" data-mdb-toggle="dropdown" aria-expanded="false" id="new"
onclick=toggleButtonNew()>
New
</button>
<div class="container">
<form class="px-4 py-3" id="newForm" style="display:none; width: 620px;">
<h5 class="primary">Add New Spending</h5>
<!-- Text input -->
<div class="form-outline mb-4">
<input type="text" id="form6Example3" class="form-control" />
<label class="form-label" for="form6Example3">Name</label>
</div>
<!-- Number input -->
<div class="form-outline mb-4">
<input type="number" id="form6Example6" class="form-control" />
<label class="form-label" for="form6Example6">Amount</label>
</div>

<select class="form-select" aria-label="Default select example">
<option disabled selected>Categories</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<br>
<div class="form-outline mb-4">
<textarea class="form-control" id="form4Example3" rows="4"></textarea>
<label class="form-label" for="form4Example3">Note</label>
</div>

<!-- Submit button -->
<br>
<button type="submit" onclick="toggleButtonNewForm()"
class="btn btn-primary btn-block mb-4">Create</button>
<button type="button" onclick="toggleButtonNewForm()" class="btn btn-danger btn-sm px-3">
<i class="fas fa-times"></i>
</button>
</form>

</div>
</div>
<table class="table align-middle">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Created</th>
<th scope="col">Amount</th>
<th scope="col">category</th>
<th scope="col">Note</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Purchase 1</th>
<td>Sit</td>
<td>Amet</td>
<td>Amet</td>
<td>Amet</td>
<td>
<button type="button" class="btn btn-success btn-sm px-3">
<i class="fas fa-edit"></i>
</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm px-3">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>

</tbody>
</table>


{% else %}
<h1>BudMan - BudMan is a web app that helps you track and adjust your spending so that you are in control of money.
</h1>
<a href="/login">Login</a>

<!-- stuff realted to promotion of website -->

{% endif %}


<script>
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function updateTransaction() {
fetch("api/update-transaction", {
method: "POST",
body: JSON.stringify({
id: 1,
name: "one",
amount: 888,
category: "fee",
note: "lkjal;ksdf"
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
'X-CSRFToken': csrftoken
}
})
.then(response => response.json())
}

function toggleButtonNew() {
document.getElementById('new').style.display = "none";
document.getElementById('newForm').style.display = "block";
}
function toggleButtonNewForm() {
document.getElementById('new').style.display = "block";
document.getElementById('newForm').style.display = "none";
}
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.10.1/mdb.min.js"></script>
</body>

</html>
Loading

0 comments on commit df96989

Please sign in to comment.