Skip to content
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
7 changes: 6 additions & 1 deletion lantern/django/django_orm/src/apps/cars/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Register your models here.
from django.utils.safestring import mark_safe

from apps.cars.models import Color, CarModel, CarBrand
from apps.cars.models import Color, CarModel, CarBrand, Car


@admin.register(Color)
Expand All @@ -24,3 +24,8 @@ def _image(self, obj):
if obj.logo:
return mark_safe(f'<img src="{obj.logo.url}" style="height: 50px">')
return '----'


@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
list_display = ('number', 'dealer', 'model', 'color', 'fuel_type',)
7 changes: 0 additions & 7 deletions lantern/django/django_orm/src/apps/cars/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ def delete(self, using=None, keep_parents=False):
self.status = self.STATUS_ARCHIVED
self.save()

@property
def title(self):
return f'{self.model.brand} {self.extra_title or ""}' # do not show None

def __str__(self):
return self.title

class Meta:
verbose_name = _('Car')
verbose_name_plural = _('Cars')
Expand Down
46 changes: 46 additions & 0 deletions lantern/django/django_orm/src/apps/cars/templates/cars_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% extends "base.html" %}

{% block content %}
<table class="table table-bordered">
<thead>
<tr>
<th>Model</th>
<th>Brand</th>
<th>Price</th>
<th>Dealer Name</th>
</tr>
</thead>
<tbody>
{% for car in cars %}
<tr>
<td>{{ car.model }}</td>
<td>{{ car.model.brand }}</td>
<td>{{ car.price }}</td>
<td>{{ car.dealer.first_name }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
{% endif %}
{% endblock %}
46 changes: 46 additions & 0 deletions lantern/django/django_orm/src/apps/cars/templates/dealers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% extends "base.html" %}

{% block content %}
<table class="table table-bordered">
<thead>
<tr>
<th>Dealer username</th>
<th>Dealer Full Name</th>
<th>Dealer's country</th>
<th>Dealer's city</th>
</tr>
</thead>
<tbody>
{% for dealer in dealers %}
<tr>
<td>{{ dealer.username }}</td>
<td>{{ dealer.full_name }}</td>
<td>{{ dealer.city.country }}</td>
<td>{{ dealer.city}}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
{% endif %}
{% endblock %}
Copy link
Collaborator

Choose a reason for hiding this comment

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

endline

13 changes: 13 additions & 0 deletions lantern/django/django_orm/src/apps/cars/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib.auth.decorators import login_required
from django.urls import path

from apps.cars.views import CarsListView, DealersCarListView
from apps.photos.views import PhotoView

app_name = 'cars'

urlpatterns = [
path('cars_list/', CarsListView.as_view(), name='cars_list'),
path('cars_photo/', login_required(PhotoView.as_view()), name='cars_photo_url'),
path('dealers_list/', DealersCarListView.as_view(), name='dealers_list_url'),
]
26 changes: 24 additions & 2 deletions lantern/django/django_orm/src/apps/cars/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
from django.shortcuts import render
from django.views.generic import ListView

# Create your views here.
from apps.cars.models import Car
from apps.dealers.models import Dealer


class CarsListView(ListView):
model = Car
template_name = 'cars_list.html'
context_object_name = 'cars'
paginate_by = 10
queryset = Car.objects.all()


class DealersCarListView(ListView):
model = Dealer
template_name = 'dealers.html'
context_object_name = 'dealers'
paginate_by = 10

def get_queryset(self):
if self.kwargs:
return Dealer.objects.all().filter(user_ptr_id=int(self.kwargs.get('pk')))
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

you do not need this else

return Dealer.objects.all()
17 changes: 16 additions & 1 deletion lantern/django/django_orm/src/apps/dealers/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
from django.contrib import admin

# Register your models here.
from apps.dealers.models import Dealer, City, Country


@admin.register(Dealer)
class CarModelAdmin(admin.ModelAdmin):
list_display = ('username', 'title')


@admin.register(City)
class CarModelAdmin(admin.ModelAdmin):
list_display = ('name', 'country')


@admin.register(Country)
class CarModelAdmin(admin.ModelAdmin):
list_display = ('name', 'code')
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.0.6 on 2020-06-16 18:37

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('dealers', '0002_delete_newsletter'),
]

operations = [
migrations.AlterModelOptions(
name='dealer',
options={'verbose_name': 'Dealer', 'verbose_name_plural': 'Dealers'},
),
]
10 changes: 10 additions & 0 deletions lantern/django/django_orm/src/apps/dealers/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
Copy link
Collaborator

Choose a reason for hiding this comment

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

try to not use as _
it may be confused



class Country(models.Model):
Expand All @@ -22,4 +23,13 @@ class Dealer(User):
title = models.CharField(max_length=255)
city = models.ForeignKey(City, on_delete=models.DO_NOTHING)

@property
def full_name(self):
return f'{self.first_name} {self.last_name}'

def __str__(self):
return self.full_name

class Meta:
verbose_name = _('Dealer')
verbose_name_plural = _('Dealers')
4 changes: 1 addition & 3 deletions lantern/django/django_orm/src/apps/dealers/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.shortcuts import render

# Create your views here.
pass
12 changes: 12 additions & 0 deletions lantern/django/django_orm/src/apps/photos/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.forms import ModelForm
from django import forms
from apps.photos.models import Photo


class PhotosForm(ModelForm):
class Meta:
model = Photo
fields = "__all__"
widgets = {
'image': forms.ClearableFileInput(attrs={'multiple': True}),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.0.6 on 2020-06-18 16:02

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


class Migration(migrations.Migration):

initial = True

dependencies = [
('cars', '0002_auto_20200605_1125'),
]

operations = [
migrations.CreateModel(
name='Photo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('image', models.ImageField(upload_to='photos/')),
('position', models.SmallIntegerField(default=1, validators=[django.core.validators.MinValueValidator(limit_value=1)])),
('car', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='cars.Car')),
],
options={
'ordering': ['position'],
},
),
]
Empty file.
3 changes: 1 addition & 2 deletions lantern/django/django_orm/src/apps/photos/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from django.core.validators import MinValueValidator
from django.db import models

# Create your models here.
from common.models import BaseDateAuditModel


class Photo(BaseDateAuditModel):
image = models.ImageField(upload_to='photos/')
position = models.SmallIntegerField(default=1, validators=[MinValueValidator(limit_value=1)])
car = models.ForeignKey('cars.Car', on_delete=models.CASCADE, related_name='photos')
car = models.ForeignKey(to='cars.Car', on_delete=models.CASCADE, related_name='photos')

class Meta:
ordering = ['position']
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}

{% block content %}
{% if user.is_authenticated %}
<form method="post" action="{% url 'cars:cars_photo_url' %}">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save">
</form>
{% endif %}
{% endblock %}
19 changes: 17 additions & 2 deletions lantern/django/django_orm/src/apps/photos/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import FormView

# Create your views here.

from apps.photos.forms import PhotosForm


class PhotoView(FormView):
template_name = 'add_photo_to_car_form.html'
form_class = PhotosForm
success_url = reverse_lazy('success_page_url')

def get_object(self):
return self.request.user

def form_valid(self, form):
form.save()
return super().form_valid(form)
6 changes: 5 additions & 1 deletion lantern/django/django_orm/src/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ def post(self, request):
)
if user:
login(request, user)
return HttpResponseRedirect(reverse('success_page_url'))
return HttpResponseRedirect(reverse('cars:cars_list'))
return render(request, 'login_page.html', {'login_form': form})


def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse('login_page_url'))


def home_page(request):
return render(request, "base.html")
9 changes: 5 additions & 4 deletions lantern/django/django_orm/src/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.views.generic import TemplateView

from apps.news_letters.views import NewsLetterView

from common.views import LoginView, logout_view
from common.views import LoginView, logout_view, home_page

urlpatterns = [
path('admin/', admin.site.urls),
path("", home_page, name="homepage", ),
path('succes/', TemplateView.as_view(template_name="success_page.html"), name='success_page_url'),
path('newsletter/', NewsLetterView.as_view(), name='news_letter_url'),
path('login/', LoginView.as_view(), name='login_page_url'),
path('logout/', logout_view, name='logout_url')
path('logout/', logout_view, name='logout_url'),
path('cars/', include('apps.cars.urls', namespace='cars')),
]

if settings.DEBUG:
Expand Down
Loading