-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,4 @@ venv/ | |
db.sqlite3 | ||
.idea/ | ||
|
||
media/ |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.http import HttpResponseForbidden | ||
|
||
from articleapp.models import Article | ||
|
||
|
||
def article_ownership_required(func): | ||
def decorated(request,*args,**kwargs): | ||
article = Article.objects.get(pk=kwargs['pk']) | ||
if not article.writer == request.user: | ||
return HttpResponseForbidden() | ||
return func(request,*args,**kwargs) | ||
return decorated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.forms import ModelForm | ||
|
||
from articleapp.models import Article | ||
|
||
|
||
class ArticleCreationForm(ModelForm): | ||
class Meta: | ||
model=Article | ||
fields = ['title', 'image', 'content'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 4.1.4 on 2023-02-11 06:10 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Article', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateField(auto_created=True, null=True)), | ||
('title', models.CharField(max_length=200, null=True)), | ||
('image', models.ImageField(upload_to='article/')), | ||
('content', models.TextField(null=True)), | ||
('writer', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='article', to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
from django.contrib.auth.models import User | ||
from django.db import models | ||
|
||
# Create your models here. | ||
|
||
class Article(models.Model): | ||
writer = models.ForeignKey(User, on_delete = models.SET_NULL, related_name='article', null=True) | ||
|
||
title = models.CharField(max_length=200, null=True) | ||
image = models.ImageField(upload_to='article/', null=False) | ||
content = models.TextField(null=True) | ||
|
||
created_at = models.DateField(auto_created=True,null=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends 'base.html' %} | ||
{% load bootstrap4 %} | ||
|
||
{% block content %} | ||
|
||
<div style="text-align: center;max-width:500px; margin: 4rem auto"> | ||
<div class="mb-4"> {# margin bottom #} | ||
<h4>Article Create</h4> | ||
</div> | ||
<form action="{% url 'articleapp:create' %}" method="post" enctype="multipart/form-data"> | ||
{% csrf_token %} | ||
{% bootstrap_form form %} | ||
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3"> | ||
</form> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{% extends 'base.html'%} | ||
{% load bootstrap4 %} | ||
|
||
{% block content %} | ||
|
||
<div style="text-align: center;max-width:500px; margin: 4rem auto"> | ||
<div class="mb-4"> {# margin bottom #} | ||
<h4>Delete Article : {{ target_article.title }}</h4> | ||
</div> | ||
<form action="{% url 'articleapp:delete' pk=target_article.pk %}" method="post"> | ||
{% csrf_token %} | ||
<input type="submit" class="btn btn-danger rounded-pill col-6 mt-3"> | ||
</form> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block content %} | ||
|
||
<div> | ||
<div style="text-align: center; max-width: 500px; margin: 4rem auto;"> | ||
|
||
<h1> | ||
{{ target_article.title }} | ||
</h1> | ||
|
||
<img src="{{ target_article.image.url }}" alt=""> | ||
<p> | ||
{{ target_article.content }} | ||
</p> | ||
|
||
<a href="{% url 'articleapp:update' pk=target_article.pk %}"> | ||
<p>Update Article</p> | ||
</a> | ||
<a href="{% url 'articleapp:delete' pk=target_article.pk %}"> | ||
<p>Delete Article</p> | ||
</a> | ||
</div> | ||
</div> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends 'base.html' %} | ||
{% load bootstrap4 %} | ||
|
||
{% block content %} | ||
|
||
<div style="text-align: center;max-width:500px; margin: 4rem auto"> | ||
<div class="mb-4"> {# margin bottom #} | ||
<h4>Article Update</h4> | ||
</div> | ||
<form action="{% url 'articleapp:update' pk=target_article.pk%}" method="post" enctype="multipart/form-data"> | ||
{% csrf_token %} | ||
{% bootstrap_form form %} | ||
<input type="submit" class="btn btn-dark rounded-pill col-6 mt-3"> | ||
</form> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from django.urls import path | ||
from django.views.generic import TemplateView | ||
|
||
from articleapp.views import ArticleCreateView, ArticleDetailView, ArticleUpdateView, ArticleDeleteView | ||
|
||
app_name='articleapp' | ||
|
||
urlpatterns = [ | ||
path('list/', TemplateView.as_view(template_name='articleapp/list.html'), name='list') | ||
path('list/', TemplateView.as_view(template_name='articleapp/list.html'), name='list'), | ||
|
||
path('create/', ArticleCreateView.as_view(), name='create'), | ||
path('detail/<int:pk>', ArticleDetailView.as_view(), name='detail'), | ||
path('update/<int:pk>', ArticleUpdateView.as_view(), name='update'), | ||
path('delete/<int:pk>', ArticleDeleteView.as_view(), name='delete') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,51 @@ | ||
import form as form | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import render | ||
from django.urls import reverse, reverse_lazy | ||
from django.utils.decorators import method_decorator | ||
from django.views.generic import CreateView, DetailView, UpdateView, DeleteView | ||
|
||
from articleapp.decorators import article_ownership_required | ||
from articleapp.forms import ArticleCreationForm | ||
from articleapp.models import Article | ||
|
||
|
||
# Create your views here. | ||
|
||
@method_decorator(login_required, 'get') | ||
@method_decorator(login_required, 'post') | ||
class ArticleCreateView(CreateView): | ||
model = Article | ||
form_class = ArticleCreationForm | ||
template_name = 'articleapp/create.html' | ||
|
||
def form_valid(self,form): | ||
temp_article = form.save(commit=False) | ||
temp_article.writer = self.request.user | ||
return super().form_valid(form) | ||
def get_success_url(self): | ||
return reverse('articleapp:detail', kwargs={'pk':self.object.pk}) | ||
|
||
class ArticleDetailView(DetailView): | ||
model = Article | ||
context_object_name = 'target_article' | ||
template_name = 'articleapp/detail.html' | ||
|
||
@method_decorator(article_ownership_required, 'get') | ||
@method_decorator(article_ownership_required, 'post') | ||
class ArticleUpdateView(UpdateView): | ||
model = Article | ||
form_class = ArticleCreationForm | ||
context_object_name = 'target_article' | ||
template_name = 'articleapp/update.html' | ||
|
||
def get_success_url(self): | ||
return reverse('articleapp:detail', kwargs={'pk':self.object.pk}) | ||
|
||
@method_decorator(article_ownership_required, 'get') | ||
@method_decorator(article_ownership_required, 'post') | ||
class ArticleDeleteView(DeleteView): | ||
model = Article | ||
context_object_name = 'target_article' | ||
template_name = 'articleapp/delete.html' | ||
success_url = reverse_lazy('articleapp:list') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 4.1.4 on 2023-02-11 06:10 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('profileapp', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='profile', | ||
name='user', | ||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
Binary file added
BIN
+813 Bytes
profileapp/migrations/__pycache__/0002_alter_profile_user.cpython-310.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters