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

Full CRUD

üst 710ed9f5
{% extends 'base.html' %}
{% block title %}Update link{% endblock %}
{% block content %}
{% load bootstrap4 %}
<h2>Update Link</h2>
<form method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Update Link</button>
{% endbuttons %}
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}Update post{% endblock %}
{% block content %}
{% load bootstrap4 %}
<h2>Update Post</h2>
<form method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Update Post</button>
{% endbuttons %}
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}Update snippet{% endblock %}
{% block css %}{{ form.media }}{% endblock %}
{% block content %}
{% load bootstrap4 %}
<h2>Update Snippet</h2>
<form method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Update Snippet</button>
{% endbuttons %}
</form>
{% endblock %}
......@@ -14,7 +14,8 @@
{% as_html item %}
<small class="text-muted">{{ item.pub_date }}</small>
{% if item.author == request.user %}
<small class="text-muted"><a href="{% get_delete item %}">delete</a></small>
<small class="text-muted"><a href="{% get_opt 'delete' item %}">delete</a></small>
<small class="text-muted"><a href="{% get_opt 'update' item %}">update</a></small>
{% endif %}
</fieldset>
</div>
......
......@@ -35,5 +35,5 @@ def get_preview(url):
return preview
@register.simple_tag
def get_delete(item):
return reverse(f"delete_{_get_class(item)}", args=(item.id,))
def get_opt(opt, item):
return reverse(f"{opt}_{_get_class(item)}", args=(item.id,))
......@@ -22,12 +22,15 @@ class SocialPatterns(PatternManager):
class SocialPostPatterns(PatternManager):
create_post = "post/create", CreatePost
update_post = "post/update/<int:pk>", UpdatePost
delete_post = "post/delete/<int:pk>", DeletePost
create_link = "link/create", CreateLink
update_link = "link/update/<int:pk>", UpdateLink
delete_link = "link/delete/<int:pk>", DeleteLink
create_snippet = "snippet/create", CreateSnippet
update_snippet = "snippet/update/<int:pk>", UpdateSnippet
delete_snippet = "snippet/delete/<int:pk>", DeleteSnippet
urlpatterns = SocialPatterns() + SocialPostPatterns()
......
from django.urls import reverse_lazy
from django.views.generic import UpdateView
from django.http import HttpResponse
from social import models, forms
__all__ = ['UpdatePost', 'UpdateLink', 'UpdateSnippet']
class SharableUpdate(UpdateView):
def __init_subclass__(cls):
simple_name = cls.__name__.replace('Update', '').lower()
cls.model = getattr(models, f"{simple_name.title()}")
cls.success_url = reverse_lazy("home")
cls.template_name = f"forms/update/{simple_name}.html"
cls.form_class = getattr(forms, f"Create{simple_name.title()}Form")
super().__init_subclass__()
def form_valid(self, *args, **kwargs):
if self.get_object().author == self.request.user:
return super().form_valid(*args, **kwargs)
else:
return HttpResponse('Unauthorized', status=401)
class UpdatePost(SharableUpdate):
pass
class UpdateLink(SharableUpdate):
pass
class UpdateSnippet(SharableUpdate):
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