Skip to content

Commit

Permalink
Merge pull request #2 from nyashaChiza/stock
Browse files Browse the repository at this point in the history
added stock app
  • Loading branch information
nyashaChiza authored Dec 28, 2023
2 parents a489ca8 + 2d06a27 commit ca424bc
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py collectstatic
python manage.py test
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
/staticfiles
MANIFEST
media/
*.pyc
Expand Down
3 changes: 2 additions & 1 deletion gds/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
#apps
'stock',

#3rd part apps
'allauth',
Expand All @@ -54,7 +55,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down
1 change: 1 addition & 0 deletions gds/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
path('admin/', admin.site.urls),
path('__debug__/', include("debug_toolbar.urls")),
path('accounts/', include('allauth.urls')),
path('stock/', include('stock.urls')),
]
Empty file added stock/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions stock/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.contrib import admin
from .models import Gas

@admin.register(Gas)
class GasAdmin(admin.ModelAdmin):
list_display = ('name', 'quantity', 'price', 'supplier', 'created', 'updated')
list_filter = ('supplier', 'created', 'updated')
search_fields = ('name', 'supplier')
date_hierarchy = 'created'
ordering = ('-created',)
fields = ('name', 'quantity', 'price', 'supplier')
readonly_fields = ('created', 'updated')
6 changes: 6 additions & 0 deletions stock/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class StockConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'stock'
26 changes: 26 additions & 0 deletions stock/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0 on 2023-12-28 20:33

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Gas',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('quantity', models.PositiveIntegerField()),
('price', models.DecimalField(decimal_places=2, max_digits=8)),
('supplier', models.CharField(blank=True, max_length=100, null=True)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
],
),
]
Empty file added stock/migrations/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions stock/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models


class Gas(models.Model):
name = models.CharField(max_length=100)
quantity = models.PositiveIntegerField()
price = models.DecimalField(max_digits=8, decimal_places=2)
supplier = models.CharField(max_length=100, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

def __str__(self):
return self.name
50 changes: 50 additions & 0 deletions stock/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.test import TestCase
from django.urls import reverse
from .models import Gas
from .views import GasListView, GasCreateView

class GasModelTest(TestCase):
def setUp(self):
self.gas = Gas.objects.create(
name='Gas A',
quantity=10,
price=2.50,
supplier='Supplier A'
)

def test_gas_model(self):
self.assertEqual(self.gas.name, 'Gas A')
self.assertEqual(self.gas.quantity, 10)
self.assertEqual(self.gas.price, 2.50)
self.assertEqual(self.gas.supplier, 'Supplier A')
self.assertIsNotNone(self.gas.created)
self.assertIsNotNone(self.gas.updated)

class GasListViewTest(TestCase):
def test_gas_list_view(self):
url = reverse('stock:gas_list')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

class GasCreateViewTest(TestCase):
def test_gas_create_view(self):
url = reverse('stock:gas_create')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_gas_create_form_submission(self):
url = reverse('stock:gas_create')
data = {
'name': 'Gas B',
'quantity': 5,
'price': 3.00,
'supplier': 'Supplier B'
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(Gas.objects.count(), 1)
gas = Gas.objects.first()
self.assertEqual(gas.name, 'Gas B')
self.assertEqual(gas.quantity, 5)
self.assertEqual(gas.price, 3.00)
self.assertEqual(gas.supplier, 'Supplier B')
12 changes: 12 additions & 0 deletions stock/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path

from . import views

app_name = 'stock'

urlpatterns = [
path('', views.GasListView.as_view(), name='gas_list'),
path('add/', views.GasCreateView.as_view(), name='gas_create'),
path('<int:pk>/update/', views.GasUpdateView.as_view(), name='gas_update'),
path('<int:pk>/delete/', views.GasDeleteView.as_view(), name='gas_delete'),
]
28 changes: 28 additions & 0 deletions stock/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.shortcuts import render

# Create your views here.
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Gas

class GasListView(ListView):
model = Gas
template_name = 'stock/index.html'
context_object_name = 'gas_items'

class GasCreateView(CreateView):
model = Gas
template_name = 'stock/create.html'
fields = ['name', 'quantity', 'price', 'supplier']
success_url = reverse_lazy('stock:gas_list')

class GasUpdateView(UpdateView):
model = Gas
template_name = 'stock/update.html'
fields = ['name', 'quantity', 'price', 'supplier']
success_url = reverse_lazy('stock:gas_list')

class GasDeleteView(DeleteView):
model = Gas
template_name = 'stock/delete.html'
success_url = reverse_lazy('stock:gas_list')
Empty file added templates/stock/create.html
Empty file.
Empty file added templates/stock/index.html
Empty file.
Empty file added templates/stock/update.html
Empty file.

0 comments on commit ca424bc

Please sign in to comment.