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

Product/crud added. #14

Open
wants to merge 5 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
8 changes: 7 additions & 1 deletion management/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from django import forms
from management.models import Company
from management.models import Company, Product


class CompanyForm(forms.ModelForm):
class Meta:
model = Company
fields = '__all__'


class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
14 changes: 7 additions & 7 deletions management/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.0.2 on 2020-02-09 18:08
# Generated by Django 3.0.2 on 2020-08-26 09:17

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -16,23 +16,23 @@ class Migration(migrations.Migration):
name='Company',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('company_type', models.CharField(choices=[('V', 'vendor'), ('C', 'client')], max_length=1)),
('name', models.CharField(max_length=30)),
('address', models.CharField(max_length=60)),
('contact', models.CharField(max_length=20)),
('date_created', models.DateField()),
('client_type', models.CharField(max_length=20)),
('contact', models.IntegerField()),
('date_created', models.DateField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=30)),
('date_created', models.DateField()),
('date_created', models.DateField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='ImportExportEvents',
name='ImportExportEvent',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('expense_description', models.CharField(max_length=200)),
Expand All @@ -44,7 +44,7 @@ class Migration(migrations.Migration):
('total_imported', models.IntegerField()),
('cost', models.IntegerField()),
('remaining_products', models.IntegerField()),
('date_created', models.DateField()),
('date_created', models.DateField(auto_now_add=True)),
('company_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='management.Company')),
('product_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='management.Product')),
],
Expand Down
30 changes: 30 additions & 0 deletions management/migrations/0002_auto_20201026_1444.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.0.2 on 2020-10-26 14:44

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


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='product',
name='description',
field=models.TextField(default=django.utils.timezone.now, max_length=220),
preserve_default=False,
),
migrations.AlterField(
model_name='company',
name='company_type',
field=models.CharField(choices=[('v', 'Vendor'), ('c', 'Client')], max_length=1),
),
migrations.AlterField(
model_name='company',
name='contact',
field=models.CharField(max_length=30),
),
]
4 changes: 4 additions & 0 deletions management/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class Product(models.Model):
def __str__(self):
return self.title

def get_absolute_url(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause code duplication you can create a helper method in a utils file. which takes the url as argument and returns the absolute url.
Now you will have to create method for each model.
If this is required by the model view we can keep this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the concept of get_absolute_url is understandable for me. I don't know how can I reduce code in utils.py or utils/absolute_urls.py. Could you refer some readings?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its quite simple if its used by many classes it should be a seperate function. If its a functionality which contains multiple functions it should have its own class. If its core functionality it should exist in core files or should have its own core file.
If its a utility method and we can work without it. This should be moved to utility.py.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a two liner its fine to have it in all classes but comment is regarding the practice. Because having similar code repeated in all classes causes code duplication. Many ways to handle that. you can create your own parent model class which inherits from models.Model and have this method and then all other classes are inherited by that. or another way which is preferred here bcz its small functionality is to create a utility method.

Copy link
Collaborator Author

@quratulain25 quratulain25 Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am using builtin views like 'UpdateView', 'ListView' etc. These need an absolute urls that needs to be defined in Model. Apart from this, each model has different absolute url like Product model's absolute url leads to Product's list page while Company's functions leads to Company's list page.

return reverse_lazy('management:list_products')

title = models.CharField(max_length=30)
description = models.TextField(max_length=220)
date_created = models.DateField(auto_now_add=True, editable=True)


Expand Down
28 changes: 20 additions & 8 deletions management/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
from management import views

app_name = 'management'
urlpatterns = [
path('', views.dashboard_view, name='dashboard'),
path('list_companies', views.companies_list_view, name='list_companies'),
path('create_company', views.company_registration_view, name='create_company'),
path('edit_company/<int:pk>/', views.company_edit_view, name='edit_company'),
path('view_company/<int:pk>/', views.company_profile_view, name='view_company'),
path('delete_company/<int:pk>/', views.company_delete_view, name='delete_company'),
] + static(settings.STATIC_URL)

dashboard_url = [path('', views.dashboard_view, name='dashboard')]

company_urls = [
path('list_companies', views.companies_list_view, name='list_companies'),
path('create_company', views.company_registration_view, name='create_company'),
path('edit_company/<int:pk>/', views.company_edit_view, name='edit_company'),
path('view_company/<int:pk>/', views.company_profile_view, name='view_company'),
path('delete_company/<int:pk>/', views.company_delete_view, name='delete_company'),
]

product_urls = [
path('list_products', views.products_list_view, name='list_products'),
path('create_product', views.product_registration_view, name='create_product'),
path('edit_product/<int:pk>/', views.product_edit_view, name='edit_product'),
path('view_product/<int:pk>/', views.product_profile_view, name='view_product'),
path('delete_product/<int:pk>/', views.product_delete_view, name='delete_product'),
]

urlpatterns = dashboard_url + company_urls + product_urls + static(settings.STATIC_URL)
3 changes: 3 additions & 0 deletions management/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from dashboard import *
from company import *
from product import *
18 changes: 3 additions & 15 deletions management/views.py → management/views/company.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.views.generic import (
TemplateView, DetailView,
DetailView,
UpdateView, ListView,
CreateView, DeleteView
)
Expand All @@ -10,25 +10,13 @@
from management.models import Company


class DashboardView(TemplateView):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Move.

template_name = 'home_page.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'LBM'
return context


dashboard_view = login_required(DashboardView.as_view())


class CompanyProfileView(DetailView):
model = Company
template_name = 'company_management/view_company_profile.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'LBM'
context['title'] = 'LBM-Company List'
return context


Expand Down
16 changes: 16 additions & 0 deletions management/views/dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib.auth.decorators import login_required
from django.views.generic import (
TemplateView
)


class DashboardView(TemplateView):
template_name = 'home_page.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'LBM-Dashboard'
return context


dashboard_view = login_required(DashboardView.as_view())
61 changes: 61 additions & 0 deletions management/views/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.views.generic import (
DetailView,
UpdateView, ListView,
CreateView, DeleteView
)

from management.forms import ProductForm
from management.models import Product


class ProductProfileView(DetailView):
model = Product
template_name = 'product_management/view_product_profile.html'

def get_context_data(self, **kwargs):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do similar thing using Django Context Manager. We might do that in future.

context = super().get_context_data(**kwargs)
context['title'] = 'LBM-Product List'
return context


product_profile_view = login_required(ProductProfileView.as_view())


class ProductsListView(ListView):
model = Product
template_name = 'product_management/list_products.html'
context_object_name = 'products'


products_list_view = login_required(ProductsListView.as_view())


class ProductRegistrationView(CreateView):
model = Product
form_class = ProductForm
context_object_name = 'product'
template_name = 'product_management/create_product_profile.html'


product_registration_view = login_required(ProductRegistrationView.as_view())


class ProductEditView(UpdateView):
model = Product
form_class = ProductForm
context_object_name = 'product'
template_name = 'product_management/edit_product_profile.html'


product_edit_view = login_required(ProductEditView.as_view())


class ProductDeleteView(DeleteView):
model = Product
template_name = 'company_management/confirm_delete.html'
success_url = reverse_lazy('management:list_products')


product_delete_view = login_required(ProductDeleteView.as_view())
2 changes: 1 addition & 1 deletion templates/company_management/confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h6 class="element-header">
</h6>
<div class="element-box">
<h5 class="form-header">
Are you sure you want to delete "{{ company.name }}" ?
Are you sure you want to delete ?
</h5>
</br>
<div class="element-box-content">
Expand Down
2 changes: 1 addition & 1 deletion templates/company_management/view_company_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>
</div>
<h1 class="up-header">
John Mayers
{{ company.name }}
</h1>
<h5 class="up-sub-header">
Product Designer at Facebook
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" Product Designer at Facebook" ???

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default information. I haven't customised this page according to our needs, yet.

Expand Down
33 changes: 25 additions & 8 deletions templates/main_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1 class="menu-page-header">
Page Header
</h1>
<ul class="main-menu">
<!-- Layouts -->
<li class="sub-header">
<span>Layouts</span>
</li>
Expand All @@ -25,14 +26,8 @@ <h1 class="menu-page-header">
<span>Dashboard</span>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this redirect to dashboard as you remove the anchor ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does redirect and is under anchor tag.

            <a href="{% url 'management:dashboard' %}">
                <div class="icon-w">
                    <div class="os-icon os-icon-layout"></div>
                </div>
                <span>Dashboard</span>
            </a>

</a>
</li>
{# <li>#}
{# <a href="{% url 'management:dashboard' %}">#}
{# <div class="icon-w">#}
{# <div class="os-icon os-icon-layers"></div>#}
{# </div>#}
{# <span>Menu Styles</span>#}
{# </a>#}
{# </li>#}
<!-- Layouts -->
<!-- Company -->
<li class="sub-header">
<span> Company </span>
</li>
Expand All @@ -52,5 +47,27 @@ <h1 class="menu-page-header">
<span> Companies List </span>
</a>
</li>
<!-- Company -->
<!-- Product -->
<li class="sub-header">
<span> Product </span>
</li>
<li>
<a href="{% url 'management:create_product' %}">
<div class="icon-w">
<div class="os-icon os-icon-layers"></div>
</div>
<span> Create Product Profile </span>
</a>
</li>
<li>
<a href="{% url 'management:list_products' %}">
<div class="icon-w">
<div class="os-icon os-icon-file-text"></div>
</div>
<span> Products List </span>
</a>
</li>
<!-- Product -->
</ul>
</div>
53 changes: 53 additions & 0 deletions templates/product_management/create_product_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="content-i">
<div class="content-box">
<div class="element-wrapper">
<div class="element-box">
<form method="post">
{% csrf_token %}
<div class="steps-w">
<div class="step-triggers">
<a class="step-trigger active" href="#stepContent1">
Create Product
</a>
</div>
<div class="step-contents">
<div class="step-content active" id="stepContent1">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for=""> Name </label>
<input class="form-control" name="title"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to update html files as well to shows description field.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already been added in last commit.

`

                                    <div class="col-sm-6">
                                        <div class="form-group">
                                            <label> Description </label>
                                            <textarea class="form-control" rows="5" name="description"
                                                      placeholder="Anything you would like to add."> {{ product.description }} </textarea>
                                        </div>
                                    </div>
                                </div>`

data-error="Please input product's Name"
placeholder="Product's Name"
minlength="3" maxlength="60"
required="required" type="text">
<div class="help-block form-text with-errors form-control-feedback"></div>
</div>
</div>
</div>

<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label> Description </label>
<textarea class="form-control" rows="5" name="description"
placeholder="Anything you would like to add."></textarea>
</div>
</div>
</div>

<div class="form-buttons-w text-right">
<button class="btn btn-primary">Submit Form</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
Loading