Add camper app

This commit is contained in:
Krzysztof Bonecki 2021-01-08 22:23:52 +01:00
parent c13ef66cb2
commit a661502a71
38 changed files with 239 additions and 303 deletions

Binary file not shown.

Binary file not shown.

View File

@ -9,12 +9,9 @@ class OfferALlDetail(admin.StackedInline):
class OfferAdmin(admin.ModelAdmin):
model = Offer
inlines = [OfferALlDetail]
list_display = ('title','category','price','status')
list_display = ('id','title','category','date_added','status',)
order = 'date_added'
admin.site.register(Offer,OfferAdmin)
admin.site.register(Offer_Category)

View File

@ -1,5 +1,6 @@
from django import forms
from .models import Offer,Offer_Category
from django.forms import formset_factory
from .models import Offer,Offer_Category,Image_offer
class OfferCreateForm(forms.ModelForm):
@ -9,3 +10,10 @@ class OfferCreateForm(forms.ModelForm):
model = Offer
fields = ('title','category','price','DMC','Places_to_sleep','cooker','fridge', 'microwave', 'Webasto', 'auto_temp_control', 'water_level_indicator', 'battery_level', 'photovoltaic_panels', 'gearbox', 'tempomat')
class OfferImageCreateForm(forms.ModelForm):
image = forms.ImageField()
class Meta:
model = Image_offer
fields = ('image','main_image')
OfferImageFormSet = formset_factory(OfferImageCreateForm, extra = 5, max_num = 5)

View File

@ -0,0 +1,27 @@
if check_user_is_owner(request) == True or request.user.is_staff == True:
if request.method == 'POST':
form = OfferCreateForm(request.POST)
formset = OfferImageFormSet(request.POST, request.FILES)
if form.is_valid() and formset.is_valid():
offer = form.save(commit = False)
offer.created_by = request.user
offer.status = 'Verification'
offer.save()
for photo in formset:
imageoffer = photo.save(commit = False)
imageoffer.user = request.user
imageoffer.offer = offer
imageoffer.save()
messages.success(request,'Oferta utworzona i przesłana do weryfikacji')
return HttpResponseRedirect('/')
else:
messages.warning(request, 'Wprowadzone dane są nieprawidłowe.')
return render(request,'offer/offer.html', {'form': form, 'formset': formset} )
else:
messages.warning(request,'Spróbuj ponownie później!')
return redirect("/")
else:
messages.error(request, 'Nie masz uprawnień!')
return HttpResponseRedirect('/')

View File

@ -1,5 +1,6 @@
# Generated by Django 3.1.3 on 2020-12-10 20:21
# Generated by Django 3.1.3 on 2021-01-07 21:01
import camper.models
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
@ -14,15 +15,6 @@ class Migration(migrations.Migration):
]
operations = [
migrations.CreateModel(
name='Offer',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('title', models.CharField(default='Tytuł oferty', max_length=50, verbose_name='Title')),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('date_added', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Offer_Category',
fields=[
@ -35,20 +27,46 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='Offer_Stats',
name='Offer',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('offer', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer', verbose_name='')),
('id', models.AutoField(primary_key=True, serialize=False)),
('date_added', models.DateTimeField(auto_now_add=True)),
('status', models.CharField(choices=[('Active', 'Active offer'), ('Inactive', 'Inactive offer'), ('Verification', 'Waiting for verication'), ('Blocked', 'Blocked Offer')], default='Inactive', max_length=32)),
('title', models.CharField(default='Tytuł oferty', max_length=50, verbose_name='Title')),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('DMC', models.CharField(choices=[('B', 'Do 3.5 tony.'), ('C', 'Między 3.5 a 7.5 tony'), ('C1', 'Powyzej 7.5 tony')], default='B', max_length=32)),
('Places_to_sleep', models.IntegerField(default=2)),
('cooker', models.CharField(choices=[('Elecric', 'Kuchenka elektryczna'), ('Gas', 'Kuchenka gazowa'), ('None', 'Brak')], default='None', max_length=32)),
('fridge', models.BooleanField(default=False)),
('microwave', models.BooleanField(default=False)),
('Webasto', models.BooleanField(default=False)),
('auto_temp_control', models.BooleanField(default=False)),
('water_level_indicator', models.BooleanField(default=False)),
('battery_level', models.BooleanField(default=False)),
('photovoltaic_panels', models.BooleanField(default=False)),
('gearbox', models.CharField(choices=[('Manual', 'Skrzynia manualna'), ('Automat', 'Skrzynia automatyczna')], default='Manual', max_length=32)),
('tempomat', models.BooleanField(default=False)),
('category', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer_category')),
('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='offer',
name='category',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer_category'),
migrations.CreateModel(
name='Image_offer',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to=camper.models._upload_path)),
('main_image', models.BooleanField(default=False)),
('offer', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer', verbose_name='')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='offer',
name='created_by_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
migrations.CreateModel(
name='Favourite_Offers',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('timestap', models.DateTimeField(auto_now_add=True, db_index=True)),
('offer', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='camper.offer')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -1,18 +0,0 @@
# Generated by Django 3.1.3 on 2020-12-10 20:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('camper', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='offer',
name='status',
field=models.CharField(choices=[('Active', 'Active offer'), ('Inactive', 'Inactive offer'), ('Verification', 'Waiting for verication'), ('Blocked', 'Blocked Offer')], default='Inactive', max_length=32),
),
]

View File

@ -1,18 +0,0 @@
# Generated by Django 3.1.3 on 2020-12-10 21:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('camper', '0002_offer_status'),
]
operations = [
migrations.RenameField(
model_name='offer',
old_name='created_by_id',
new_name='created_by',
),
]

View File

@ -1,29 +0,0 @@
# Generated by Django 3.1.3 on 2020-12-11 21:02
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('camper', '0003_auto_20201210_2155'),
]
operations = [
migrations.AlterField(
model_name='offer_stats',
name='offer',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer'),
),
migrations.CreateModel(
name='Image_offer',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('image', models.ImageField(upload_to='images/')),
('main_image', models.BooleanField(default=False)),
('offer', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='camper.offer', verbose_name='')),
],
),
]

View File

@ -1,73 +0,0 @@
# Generated by Django 3.1.3 on 2020-12-18 20:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('camper', '0004_auto_20201211_2102'),
]
operations = [
migrations.AddField(
model_name='offer',
name='DMC',
field=models.CharField(choices=[('B', 'Do 3.5 tony.'), ('C', 'Między 3.5 a 7.5 tony'), ('C1', 'Powyzej 7.5 tony')], default='C1', max_length=32),
),
migrations.AddField(
model_name='offer',
name='Places_to_sleep',
field=models.IntegerField(default=2),
),
migrations.AddField(
model_name='offer',
name='Webast',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='auto_temp_control',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='battery_level',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='cooker',
field=models.CharField(choices=[('Elecric', 'Kuchenka elektryczna'), ('Gas', 'Kuchenka gazowa'), ('None', 'Brak')], default='None', max_length=32),
),
migrations.AddField(
model_name='offer',
name='fridge',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='gearbox',
field=models.CharField(choices=[('Manual', 'Skrzynia manualna'), ('Automat', 'Skrzynia automatyczna')], default='Manual', max_length=32),
),
migrations.AddField(
model_name='offer',
name='microwave',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='photovoltaic_panels',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='tempomat',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='offer',
name='water_level_indicator',
field=models.BooleanField(default=False),
),
]

View File

@ -1,23 +0,0 @@
# Generated by Django 3.1.3 on 2020-12-27 00:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('camper', '0005_auto_20201218_2053'),
]
operations = [
migrations.RenameField(
model_name='offer',
old_name='Webast',
new_name='Webasto',
),
migrations.AlterField(
model_name='offer',
name='DMC',
field=models.CharField(choices=[('B', 'Do 3.5 tony.'), ('C', 'Między 3.5 a 7.5 tony'), ('C1', 'Powyzej 7.5 tony')], default='B', max_length=32),
),
]

View File

@ -3,11 +3,13 @@ from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from users.models import Base_User
import users
from django.conf import settings
class Offer_Category(models.Model):
id = models.AutoField(primary_key= True)
name = models.CharField(max_length=50,null = False, blank = False,)
name = models.CharField(max_length=50)
class Meta:
verbose_name = 'Category'
@ -41,12 +43,13 @@ GEARBOX_Option = (
)
class Offer(models.Model):
id = models.AutoField(primary_key= True)
created_by = models.ForeignKey('users.Base_User', on_delete=models.CASCADE)
title = models.CharField(_("Title"), max_length=50,default='Tytuł oferty')
category = models.ForeignKey(Offer_Category,null = False, blank = False, default= None,on_delete= models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_added = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=32, choices=STATUS, default='Inactive')
title = models.CharField(_("Title"), max_length=50,default='Tytuł oferty')
category = models.ForeignKey(Offer_Category ,default= None,on_delete= models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
DMC = models.CharField(max_length=32, choices=DMC_Value, default='B')
Places_to_sleep = models.IntegerField(default=2)
@ -66,18 +69,33 @@ class Offer(models.Model):
pass
def __str__(self):
return self.title
return self.id
def get_absolute_url(self):
return reverse('offer_detail', kwargs = {'pk': self.id })
def _upload_path(instance,filename):
return instance.get_upload_path(filename)
class Image_offer(models.Model):
offer = models.ForeignKey("Offer", verbose_name="", on_delete = models.CASCADE, default=None)
name = models.CharField(max_length=255)
image = models.ImageField(upload_to='images/')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
image = models.ImageField(upload_to=_upload_path, null = True, blank = True)
main_image = models.BooleanField(default= False)
def get_upload_path(self,filename):
return "uploads/user/id-"+str(self.user.id)+"/"+str(self.offer.id) + "/" +filename
def get_absolute_url(self):
return reverse("model_detail", kwargs={"pk": self.pk})
class Offer_Stats(models.Model):
offer = models.ForeignKey('Offer',on_delete=models.CASCADE, default=None)
class Favourite_Offers(models.Model):
user = models.ForeignKey('users.Base_User', on_delete=models.CASCADE,)
offer = models.ForeignKey('Offer', on_delete=models.SET_NULL, null = True)
timestap = models.DateTimeField(auto_now_add=True, db_index=True)

16
vagus/camper/test.py Normal file
View File

@ -0,0 +1,16 @@
from django.test import TestCase
# Create your tests here.
from .models import Offer, Image_offer, Offer_Category
class CreateOfferTest(TestCase):
def offer_create_test(self):
cat_id = Offer_Category.objects.get(name ='CamperVan').id
self.form_data ={
'title': 'Kamper',
'price': 213,
'category': cat_id,
}
def offer_image_create_test(self):
pass

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -12,4 +12,4 @@ urlpatterns = [
path('', views.Home.as_view(), name='homepage')
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -1,100 +0,0 @@
from django.shortcuts import render
from .models import Offer,Offer_Category,Offer_Stats
from users.models import Base_User
from django.core.mail import send_mail
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import (
CreateView,
ListView,
DetailView,
UpdateView,
DeleteView,
View
)
from .forms import OfferCreateForm
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from users.models import Base_User
from django.contrib import messages
from django.http import HttpResponseRedirect
def check_user_is_auth(request):
if request.users.Base_user.is_authenticated():
return True
else:
render (request,'signup.html')
def check_user_is_owner(request):
if check_user_is_auth == True:
if request.users.Base_user.status =="Company_Account": # check if user has status 'owner':
pass
else:
render (request,'upgrade-to-onwer.html')
#def check_offer_belong_to_user(request):
# if request.check_user_is_owner() == True:
# if offer.author == request.users.id:
# return True
# else:
# raise ValueError("To chyba nie twoje :)")
# if check_users_is_owner == True:
# Create your views here.
class Home(View):
def get(self, request, *args, **kwargs):
return render(request, 'index.html')
class OfferDetailView(DetailView):
model = Offer
template_name = 'offer/offer-detail.html'
class OfferListView(ListView):
model = Offer
template_name = 'offer/offers.html'
def get_queryset(self):
return Offer.objects.filter(status = 'Active')
class CreateOffer(View):
@method_decorator(login_required)
def get(self, request,*args,**kwargs):
form = OfferCreateForm()
context = {'form': form}
#if check_user_is_owner(request) == True or request.Base_user.is_staff == True:
return render(request,'offer/offer.html', context)
@method_decorator(login_required)
def post(self,request,*args,**kwargs):
form = OfferCreateForm(request.POST)
if request.method == 'POST':
if form.is_valid():
if check_user_is_owner(request) == True or request.user.is_staff == True:
offer = form.save(commit=False)
offer.created_by = request.user
offer.status = 'Verification'
offer.save()
messages.success(request, 'OFERTA UTWORZONA!') # <-
return HttpResponseRedirect('/')
else:
messages.success(request, 'Nie masz uprawnień!') # <-
return HttpResponseRedirect('/')
else:
messages.warning(request, 'Nie utworzona')
return render(request,'offer/offer.html', {'form': form} )
else:
messages.warning(request,'BLAD')
return redirect("/")

View File

@ -0,0 +1,3 @@
from .offer_create_and_edit import *
from .offer_detail import *
from .offer_stats import *

Binary file not shown.

View File

@ -0,0 +1,82 @@
from django.shortcuts import render,redirect,get_object_or_404
from camper.models import Offer,Offer_Category,Image_offer
from users.models import Base_User
from django.views.generic import (
CreateView,
UpdateView,
DeleteView,
)
from camper.forms import OfferCreateForm,OfferImageCreateForm,OfferImageFormSet
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib import messages
from django.http import HttpResponseRedirect
import users
def check_user_is_auth(request):
if request.users.Base_user.is_authenticated():
return True
else:
render (request,'signup.html')
def check_user_is_owner(request):
if check_user_is_auth == True:
if request.users.Base_user.status =="Company_Account": # check if user has status 'owner':
pass
else:
render (request,'upgrade-to-onwer.html')
#def check_offer_belong_to_user(request):
# if request.check_user_is_owner() == True:
# if offer.author == request.users.id:
# return True
# else:
# raise ValueError("To chyba nie twoje :)")
# if check_users_is_owner == True:
# Create your views here.
class CreateOffer(CreateView):
@method_decorator(login_required)
def get(self, request,*args,**kwargs):
form = OfferCreateForm()
formset = OfferImageFormSet()
context = {
'form': form,
'formset': formset,
}
#if check_user_is_owner(request) == True or request.Base_user.is_staff == True:
return render(request,'offer/add-offer.html', context)
@method_decorator(login_required)
def post(self,request,*args,**kwargs):
if check_user_is_owner(request) == True or request.user.is_staff == True:
if request.method == 'POST':
form = OfferCreateForm(request.POST)
formset = OfferImageFormSet(request.POST, request.FILES)
if form.is_valid() and formset.is_valid():
offer = form.save(commit = False)
offer.created_by = request.user
offer.status = 'Verification'
offer.save()
for photo in formset:
imageoffer = photo.save(commit = False)
imageoffer.user = request.user
imageoffer.offer = offer
imageoffer.save()
messages.success(request,'Oferta utworzona i przesłana do weryfikacji')
return HttpResponseRedirect('/')
else:
messages.warning(request, 'Wprowadzone dane są nieprawidłowe.')
return render(request,'offer/add-offer.html', {'form': form, 'formset': formset} )
else:
messages.warning(request,'Spróbuj ponownie później!')
return redirect("/")
else:
messages.error(request, 'Nie masz uprawnień!')
return HttpResponseRedirect('/')

View File

@ -0,0 +1,31 @@
from django.shortcuts import render,redirect, get_object_or_404
from django.views.generic import (
ListView,
DetailView,
View
)
from camper.models import Offer, Image_offer
from django.http import Http404, HttpResponseRedirect
class Home(View):
def get(self, request, *args, **kwargs):
return render(request, 'index.html')
class OfferDetailView(DetailView):
template_name = 'offer/offer-detail.html'
model = Offer
#def get_context_data(self, **kwargs):
#context = super(OfferDetailView,self).get_context_data(**kwargs)
#context["images"] = self.queryset
#return context
class OfferListView(ListView):
model = Offer
template_name = 'offer/offers.html'
def get_queryset(self):
return Offer.objects.filter(status = 'Active')

View File