Skip to content

Commit

Permalink
Merge pull request #25 from Qwizi/redesign
Browse files Browse the repository at this point in the history
Redesign
  • Loading branch information
Qwizi authored Jun 16, 2024
2 parents 039a784 + 3dd5539 commit c0d28a0
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 94 deletions.
91 changes: 46 additions & 45 deletions src/dealhub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

import os
from pathlib import Path

Expand All @@ -34,60 +35,60 @@
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'allauth',
'allauth.account',
'django_htmx',
'crispy_forms',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"accounts",
"allauth",
"allauth.account",
"django_htmx",
"crispy_forms",
"crispy_daisyui",
'offers',
'categories'
"offers",
"categories",
"reviews",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
"django_htmx.middleware.HtmxMiddleware"
"django_htmx.middleware.HtmxMiddleware",
]

ROOT_URLCONF = 'dealhub.urls'
ROOT_URLCONF = "dealhub.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.request",
],
},
},
]

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
]

WSGI_APPLICATION = 'dealhub.wsgi.application'
WSGI_APPLICATION = "dealhub.wsgi.application"

# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
Expand All @@ -108,25 +109,25 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]

# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'pl-PL'
LANGUAGE_CODE = "pl-PL"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -141,7 +142,7 @@
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

ACCOUNT_EMAIL_VERIFICATION = "none"

Expand Down Expand Up @@ -171,12 +172,12 @@
"Felix",
"Rascal",
"Miss kitty",
"Sasha"
"Sasha",
]
}

CRISPY_ALLOWED_TEMPLATE_PACKS = "daisyui"

CRISPY_TEMPLATE_PACK = "daisyui"

PEXELS_API_KEY = os.environ.get("PEXELS_API_KEY", "")
PEXELS_API_KEY = os.environ.get("PEXELS_API_KEY", "")
19 changes: 19 additions & 0 deletions src/offers/migrations/0005_offer_reviews.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.6 on 2024-06-16 18:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('offers', '0004_offer_created_at'),
('reviews', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='offer',
name='reviews',
field=models.ManyToManyField(blank=True, related_name='offers', to='reviews.review'),
),
]
24 changes: 20 additions & 4 deletions src/offers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

class Offer(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="accounts", null=True,
blank=True)
category = models.ForeignKey("categories.Category", on_delete=models.CASCADE, related_name="offers", null=True)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="accounts",
null=True,
blank=True,
)
category = models.ForeignKey(
"categories.Category",
on_delete=models.CASCADE,
related_name="offers",
null=True,
)
images = models.TextField()
description = models.TextField(default="ESZ")
price = models.FloatField(default=50)
Expand All @@ -17,5 +27,11 @@ def __str__(self):
return self.title

def get_images(self):
return self.images.split('\n')
return self.images.split("\n")

def buy(self):
self.is_active = False
self.save()


# Create your models here.
7 changes: 5 additions & 2 deletions src/offers/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from django.urls import path
from offers.views import OfferDetailView, offer_index, OfferListView, CreateOfferView
from offers.views import BuyOfferView, OfferDetailView, offer_index, OfferListView, CreateOfferView
from reviews.views import CreateReviewView

urlpatterns = [
path("", OfferListView.as_view(), name="offers_list"),
path("create/", CreateOfferView.as_view(), name="create_offer"),
path("<int:pk>/", OfferDetailView.as_view(), name="offer_detail")
path("<int:pk>/", OfferDetailView.as_view(), name="offer_detail"),
path("<int:pk>/review", CreateReviewView.as_view(), name="offer_create_review"),
path("<int:pk>/buy", BuyOfferView.as_view(), name="offer_buy"),
]
41 changes: 35 additions & 6 deletions src/offers/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import View
from django_htmx.http import HttpResponseLocation

from django.views.generic import ListView, CreateView
from django.views.generic.detail import DetailView
from categories.models import Category
from offers.forms import CreateOfferForm
from offers.models import Offer
from reviews.forms import CreateReviewForm


def offer_index(request):
offers = Offer.objects.all()
categories = Category.objects.all()
return render(request, "offers/index.html", {"offers" :offers, "categories" :categories})
return render(
request, "offers/index.html", {"offers": offers, "categories": categories}
)


# Create your views here.


Expand All @@ -20,11 +29,11 @@ class OfferListView(ListView):
context_object_name = "offers"

def get_queryset(self):
category = self.request.GET.get('category', None)
category = self.request.GET.get("category", None)
if category:
return Offer.objects.filter(category_id=category).order_by('-created_at')
return Offer.objects.filter(category_id=category).order_by("-created_at")
else:
return Offer.objects.all().order_by('-created_at')
return Offer.objects.all().order_by("-created_at")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand All @@ -41,9 +50,29 @@ class CreateOfferView(LoginRequiredMixin, CreateView):
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)



class OfferDetailView(DetailView):
model = Offer
template_name = "offers/offer_detail.html"
context_object_name = "offer"


def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['reviews'] = self.object.review_set.all().order_by('-created_at')[:10]
total_stars = self.object.review_set.count()
for i in range(1, 6):
context[f"number_of_{i}_stars"] = self.object.review_set.filter(stars=i).count()
context[f"percentage_of_{i}_stars"] = int((context[f"number_of_{i}_stars"] / total_stars) * 100) if total_stars > 0 else 0
print(context)
context["create_review_form"] = CreateReviewForm()
return context


class BuyOfferView(LoginRequiredMixin, View):
model = Offer

def post(self, request, pk):
offer = Offer.objects.get(pk=pk)
offer.buy()
return HttpResponseLocation(reverse_lazy("offer_detail", kwargs={"pk": pk}))
Empty file added src/reviews/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions src/reviews/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from reviews.models import Review

admin.site.register(Review)
6 changes: 6 additions & 0 deletions src/reviews/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ReviewsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'reviews'
16 changes: 16 additions & 0 deletions src/reviews/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django import forms
from reviews.models import Review


class CreateReviewForm(forms.ModelForm):
content = forms.CharField(
widget=forms.Textarea(attrs={"style": "height: 250px;"}),
min_length=2,
label="Tresc",
)
stars = forms.IntegerField(min_value=1, max_value=5, label="Ocena (1-5)")


class Meta:
model = Review
fields = ["content", "stars"]
24 changes: 24 additions & 0 deletions src/reviews/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.6 on 2024-06-16 18:34

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Review',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField()),
('author', models.CharField(max_length=100)),
('stars', models.PositiveIntegerField()),
('created_at', models.DateTimeField(auto_now_add=True)),
],
),
]
21 changes: 21 additions & 0 deletions src/reviews/migrations/0002_alter_review_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.6 on 2024-06-16 18:36

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


class Migration(migrations.Migration):

dependencies = [
('reviews', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name='review',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
Loading

0 comments on commit c0d28a0

Please sign in to comment.