From def52230a94b3085d566a2e5488b6639731170d4 Mon Sep 17 00:00:00 2001 From: doesnm Date: Sat, 7 Sep 2024 18:59:47 +0300 Subject: [PATCH] admin, blog and feedback --- cmsMain/admin.py | 36 +++++++++---------- .../0007_post_site_alter_post_url.py | 25 +++++++++++++ cmsMain/models.py | 1 + cmsMain/views.py | 16 +++++++-- 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 cmsMain/migrations/0007_post_site_alter_post_url.py diff --git a/cmsMain/admin.py b/cmsMain/admin.py index e04769e..5abc470 100644 --- a/cmsMain/admin.py +++ b/cmsMain/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin,GroupAdmin from django.contrib.auth.models import User, Group -from unfold.admin import ModelAdmin +from unfold.admin import ModelAdmin, TabularInline + from .models import Site, Page, Url, Variable, File, Post, Feedback from unfold.contrib.forms.widgets import WysiwygWidget from django.db import models @@ -14,24 +15,30 @@ class UserAdmin(UserAdmin, ModelAdmin): pass @admin.register(Group) class GroupAdmin(GroupAdmin, ModelAdmin): pass +class UrlInline(TabularInline): + model = Url + extra = 0 +class PostInline(TabularInline): + model = Post + extra = 0 + formfield_overrides = { + models.TextField: { + "widget": WysiwygWidget + } + } + def save_form(self,request,obj,form,change): + obj.author = request.user + obj.save() + @admin.register(Site) class SiteAdmin(ModelAdmin): list_display = ["domain","base_page"] list_editable = ["base_page"] + inlines = [UrlInline,PostInline] @admin.register(Page) class PageAdmin(ModelAdmin): pass - # formfield_overrides = { - # models.TextField: { - # "widget": WysiwygWidget - # } - # } -@admin.register(Url) -class UrlAdmin(ModelAdmin): - list_display = ["site","page","path"] - list_display_links = ["path"] - list_editable = ["site","page"] @admin.register(Variable) class VariableAdmin(ModelAdmin): @@ -41,13 +48,6 @@ class VariableAdmin(ModelAdmin): @admin.register(File) class FileAdmin(ModelAdmin): pass -@admin.register(Post) -class PostAdmin(ModelAdmin): - list_display = ["author","title"] - list_display_links = ["author","title"] - def save_model(self,request,obj,form,change): - obj.author = request.user - obj.save() @admin.register(Feedback) class FeedbackAdmin(ModelAdmin): diff --git a/cmsMain/migrations/0007_post_site_alter_post_url.py b/cmsMain/migrations/0007_post_site_alter_post_url.py new file mode 100644 index 0000000..3b61214 --- /dev/null +++ b/cmsMain/migrations/0007_post_site_alter_post_url.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1 on 2024-09-07 08:14 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cmsMain', '0006_post_url'), + ] + + operations = [ + migrations.AddField( + model_name='post', + name='site', + field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, to='cmsMain.site', verbose_name='Сайт'), + preserve_default=False, + ), + migrations.AlterField( + model_name='post', + name='url', + field=models.CharField(help_text='например: my-first-test-post', max_length=100, verbose_name='Ссылка'), + ), + ] diff --git a/cmsMain/models.py b/cmsMain/models.py index 23a7cf1..8157822 100644 --- a/cmsMain/models.py +++ b/cmsMain/models.py @@ -49,6 +49,7 @@ class File(models.Model): verbose_name_plural = "Файлы" class Post(models.Model): + site = models.ForeignKey(Site,verbose_name="Сайт",on_delete=models.PROTECT) url = models.CharField("Ссылка",help_text="например: my-first-test-post",max_length=100) title = models.CharField("Название",max_length=100) author = models.ForeignKey(User,editable=False,verbose_name="Автор",on_delete=models.PROTECT) diff --git a/cmsMain/views.py b/cmsMain/views.py index 4259b39..49790f1 100644 --- a/cmsMain/views.py +++ b/cmsMain/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render, get_object_or_404 from django.views import View from django import template -from .models import Site, Url, Variable, Post +from .models import Site, Url, Variable, Post, Feedback from django.http import HttpResponse context = template.Context({}) @@ -28,4 +28,16 @@ class PageView(View): "post": post }) t = template.Template(s.base_page.content) - return HttpResponse(t.render(context)) \ No newline at end of file + return HttpResponse(t.render(context)) + +class FeedpackView(View): + def post(self,request): + name = request.POST["name"] + email = request.POST["email"] + message = request.POST["message"] + Feedback.objects.create( + name=name, + email=email, + message=message + ) + return HttpResponse("Ваше обращение отправлено успешно") \ No newline at end of file