Kaydet (Commit) bd0d2ed6 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Adapting CRUD system

üst eb75f115
......@@ -16,12 +16,12 @@ class SocialUserChangeForm(UserChangeForm):
model = get_user_model()
fields = ("username", "email", "avatar")
class PostCreationForm(forms.ModelForm):
class CreatePostForm(forms.ModelForm):
class Meta:
model = Post
fields = ("text",)
class SnippetCreationForm(forms.ModelForm):
class CreateSnippetForm(forms.ModelForm):
class Meta:
model = Snippet
fields = ("text",)
......@@ -29,7 +29,7 @@ class SnippetCreationForm(forms.ModelForm):
"text": AceWidget(mode='python', theme='monokai'),
}
class LinkCreationForm(forms.ModelForm):
class CreateLinkForm(forms.ModelForm):
class Meta:
model = Link
fields = ("url",)
......@@ -28,13 +28,13 @@
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'post' %}">Post</a>
<a class="nav-link" href="{% url 'create_post' %}">Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'link' %}">Link</a>
<a class="nav-link" href="{% url 'create_link' %}">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'snippet' %}">Snippet</a>
<a class="nav-link" href="{% url 'create_snippet' %}">Snippet</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Logout</a>
......
......@@ -6,7 +6,7 @@
{% block content %}
{% load bootstrap4 %}
<h2>Post link to feed</h2>
<form action="{% url 'link' %}" method="post" class="form">
<form action="{% url 'create_link' %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
......
......@@ -6,7 +6,7 @@
{% block content %}
{% load bootstrap4 %}
<h2>Post to feed</h2>
<form action="{% url 'post' %}" method="post" class="form">
<form action="{% url 'create_post' %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
......
......@@ -6,7 +6,7 @@
{% block content %}
{% load bootstrap4 %}
<h2>Post snippet to feed</h2>
<form action="{% url 'snippet' %}" method="post" class="form">
<form action="{% url 'create_snippet' %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
......
......@@ -2,7 +2,7 @@ from urllib.parse import urlparse
from django import template
from django.template.loader import render_to_string
from purima.utils.get_preview import get_preview as _get_preview
from purima.utils.get_preview import _get_preview
register = template.Library()
......
......@@ -5,14 +5,11 @@ from typing import Optional, Sequence
from django.urls import include, path
from purima.urls import PatternManager, IncludeFilter
from social.views import Home, Profile, Register, Post, Link, Snippet
from social.views import *
class SocialPatterns(PatternManager):
home = "", Home
post = "share/post/", Post
link = "share/link/", Link
snippet = "share/snippet/", Snippet
profile = "people/<slug>/", Profile
register = "accounts/register/", Register
......@@ -22,5 +19,10 @@ class SocialPatterns(PatternManager):
)
}
urlpatterns = SocialPatterns()
class SocialPostPatterns(PatternManager):
create_post = "create/post", CreatePost
create_link = "create/link", CreateLink
create_snippet = "create/snippet", CreateSnippet
urlpatterns = SocialPatterns() + SocialPostPatterns()
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views.generic import CreateView, DetailView, ListView
from django.views.generic.base import TemplateView
from purima.views import ExtendedListView
from social import forms
from social.models import Link, Post, Snippet
class Home(ExtendedListView):
models = Post, Snippet, Link
template_name = "home.html"
context_object_name = "feed"
def get_queryset(self):
qs = list(super().get_queryset())
qs.sort(key=(lambda item: item.id))
return reversed(qs)
class Register(CreateView):
form_class = forms.SocialUserCreationForm
success_url = reverse_lazy("login")
template_name = "registration/register.html"
class SharableCreate(CreateView):
def __init_subclass__(cls):
cls.form_class = getattr(forms, f"{cls.__name__}CreationForm")
cls.success_url = reverse_lazy("home")
cls.template_name = f"forms/{cls.__name__.lower()}.html"
super().__init_subclass__()
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class SharableCreate(CreateView):
def __init_subclass__(cls):
cls.form_class = getattr(forms, f"{cls.__name__}CreationForm")
cls.success_url = reverse_lazy("home")
cls.template_name = f"forms/{cls.__name__.lower()}.html"
super().__init_subclass__()
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class Post(SharableCreate):
pass
class Link(SharableCreate):
pass
class Snippet(SharableCreate):
pass
class Profile(DetailView):
model = get_user_model()
slug_field = "username"
template_name = "user/profile.html"
from social.views.crud import *
from social.views.auth import *
from purima.views import ExtendedListView
from social.models import Link, Post, Snippet
class Home(ExtendedListView):
models = Post, Snippet, Link
template_name = "home.html"
context_object_name = "feed"
def get_queryset(self):
qs = list(super().get_queryset())
qs.sort(key=(lambda item: item.id))
return reversed(qs)
from django.views.generic import CreateView, DetailView
from django.urls import reverse_lazy
from django.contrib.auth import get_user_model
from social import forms
__all__ = ['Register', 'Profile']
class Register(CreateView):
form_class = forms.SocialUserCreationForm
success_url = reverse_lazy("login")
template_name = "registration/register.html"
class Profile(DetailView):
model = get_user_model()
slug_field = "username"
template_name = "user/profile.html"
from social.views.crud.create import *
from social.views.crud.update import *
from social.views.crud.delete import *
from django.urls import reverse_lazy
from django.views.generic import CreateView
from social import forms
__all__ = ['CreatePost', 'CreateLink', 'CreateSnippet']
class SharableCreate(CreateView):
def __init_subclass__(cls):
cls.form_class = getattr(forms, f"{cls.__name__}Form")
cls.success_url = reverse_lazy("home")
cls.template_name = f"forms/create/{cls.__name__.replace('Create', '').lower()}.html"
super().__init_subclass__()
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class CreatePost(SharableCreate):
pass
class CreateLink(SharableCreate):
pass
class CreateSnippet(SharableCreate):
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment