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

Delete operation for all sharables

üst 1bf26eaf
......@@ -19,17 +19,15 @@ class SocialUserChangeForm(UserChangeForm):
model = get_user_model()
fields = ("username", "email")
class CreateSharedForm(forms.ModelForm):
class SharedForm(forms.ModelForm):
captcha = CaptchaField()
class CreatePostForm(CreateSharedForm):
captcha = CaptchaField()
class CreatePostForm(SharedForm):
class Meta:
model = Post
fields = ("text",)
class CreateSnippetForm(CreateSharedForm):
captcha = CaptchaField()
class CreateSnippetForm(SharedForm):
class Meta:
model = Snippet
fields = ("text",)
......@@ -37,8 +35,7 @@ class CreateSnippetForm(CreateSharedForm):
"text": AceWidget(mode='python', theme='monokai'),
}
class CreateLinkForm(CreateSharedForm):
captcha = CaptchaField()
class CreateLinkForm(SharedForm):
class Meta:
model = Link
fields = ("url",)
{% extends 'base.html' %}
{% block title %}Delete link{% endblock %}
{% block content %}
{% load bootstrap4 %}
{% load renderer %}
<h2>Delete Link</h2>
<div class="container-fluid __{{ item | get_class }}">
<fieldset class="border p-2">
{% as_html object %}
<small class="text-muted">{{ object.pub_date }}</small>
</fieldset>
</div>
<form method="post" class="form">
{% csrf_token %}
{% buttons %}
<button type="submit" class="btn btn-primary" value="Confirm">Delete Link</button>
{% endbuttons %}
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}Delete post{% endblock %}
{% block content %}
{% load bootstrap4 %}
{% load renderer %}
<h2>Delete Post</h2>
<div class="container-fluid __{{ item | get_class }}">
<fieldset class="border p-2">
{% as_html object %}
<small class="text-muted">{{ object.pub_date }}</small>
</fieldset>
</div>
<form method="post" class="form">
{% csrf_token %}
{% buttons %}
<button type="submit" class="btn btn-primary" value="Confirm">Delete Post</button>
{% endbuttons %}
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}Delete snippet{% endblock %}
{% block content %}
{% load bootstrap4 %}
{% load renderer %}
<h2>Delete Snippet</h2>
<div class="container-fluid __{{ item | get_class }}">
<fieldset class="border p-2">
{% as_html object %}
<small class="text-muted">{{ object.pub_date }}</small>
</fieldset>
</div>
<form method="post" class="form">
{% csrf_token %}
{% buttons %}
<button type="submit" class="btn btn-primary" value="Confirm">Delete Snippet</button>
{% endbuttons %}
</form>
{% endblock %}
{% block js %}
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
{% endblock %}
......@@ -13,6 +13,9 @@
<legend class="w-auto"><a href="{% url 'profile' item.author %}">{{ item.author }}</a></legend>
{% 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>
{% endif %}
</fieldset>
</div>
{% endfor %}
......
from urllib.parse import urlparse
from django import template
from django.urls import reverse
from django.template.loader import render_to_string
from purima.utils.get_preview import _get_preview, construct_preview
register = template.Library()
def _get_class(item):
return item.__class__.__name__.lower()
@register.filter
def get_class(item):
return item.__class__.__name__.lower()
return _get_class(item)
@register.simple_tag
......@@ -29,3 +33,7 @@ def get_preview(url):
preview = construct_preview(url, {'title': _url.netloc})
return preview
@register.simple_tag
def get_delete(item):
return reverse(f"delete_{_get_class(item)}", args=(item.id,))
......@@ -21,9 +21,14 @@ class SocialPatterns(PatternManager):
}
class SocialPostPatterns(PatternManager):
create_post = "create/post", CreatePost
create_link = "create/link", CreateLink
create_snippet = "create/snippet", CreateSnippet
create_post = "post/create", CreatePost
delete_post = "post/delete/<int:pk>", DeletePost
create_link = "link/create", CreateLink
delete_link = "link/delete/<int:pk>", DeleteLink
create_snippet = "snippet/create", CreateSnippet
delete_snippet = "snippet/delete/<int:pk>", DeleteSnippet
urlpatterns = SocialPatterns() + SocialPostPatterns()
......@@ -6,9 +6,12 @@ __all__ = ['CreatePost', 'CreateLink', 'CreateSnippet']
class SharableCreate(CreateView):
def __init_subclass__(cls):
simple_name = cls.__name__.replace('Create', '').lower()
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"
cls.template_name = f"forms/create/{simple_name}.html"
super().__init_subclass__()
def form_valid(self, form):
......
from django.urls import reverse_lazy
from django.views.generic import DeleteView
from django.http import HttpResponse
from social import models
__all__ = ['DeletePost', 'DeleteLink', 'DeleteSnippet']
class SharableDelete(DeleteView):
def __init_subclass__(cls):
simple_name = cls.__name__.replace('Delete', '').lower()
cls.model = getattr(models, f"{simple_name.title()}")
cls.success_url = reverse_lazy("home")
cls.template_name = f"forms/delete/{simple_name}.html"
super().__init_subclass__()
def delete(self, request, *args, **kwargs):
if self.get_object().author == request.user:
return super().delete(request, *args, **kwargs)
else:
return HttpResponse('Unauthorized', status=401)
class DeletePost(SharableDelete):
pass
class DeleteLink(SharableDelete):
pass
class DeleteSnippet(SharableDelete):
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