Skip to content

Commit 811f6c4

Browse files
authored
Add files via upload
1 parent ef4ac97 commit 811f6c4

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed

blog_project/blog/admin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.contrib import admin
2+
3+
from .models import Post, Comment
4+
5+
6+
admin.site.register(Post)
7+
admin.site.register(Comment)

blog_project/blog/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BlogConfig(AppConfig):
5+
name = 'blog'

blog_project/blog/forms.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django import forms
2+
3+
from .models import Post, Comment
4+
5+
6+
class PostForm(forms.ModelForm):
7+
8+
class Meta:
9+
model = Post
10+
fields = ('author','title', 'text',)
11+
12+
widgets = {
13+
'title': forms.TextInput(attrs={'class': 'textinputclass'}),
14+
'text': forms.Textarea(attrs={'class': 'editable medium-editor-textarea postcontent'}),
15+
}
16+
17+
18+
class CommentForm(forms.ModelForm):
19+
20+
class Meta:
21+
model = Comment
22+
fields = ('author', 'text',)
23+
24+
widgets = {
25+
'author': forms.TextInput(attrs={'class': 'textinputclass'}),
26+
'text': forms.Textarea(attrs={'class': 'editable medium-editor-textarea'}),
27+
}

blog_project/blog/models.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from django.db import models
2+
from django.utils import timezone
3+
from django.core.urlresolvers import reverse
4+
5+
6+
class Post(models.Model):
7+
author = models.ForeignKey('auth.User')
8+
title = models.CharField(max_length=200)
9+
text = models.TextField()
10+
created_date = models.DateTimeField(default=timezone.now)
11+
published_date = models.DateTimeField(blank=True, null=True)
12+
13+
def publish(self):
14+
self.published_date = timezone.now()
15+
self.save()
16+
17+
def approve_comments(self):
18+
return self.comments.filter(approved_comment=True)
19+
20+
def get_absolute_url(self):
21+
return reverse("post_detail",kwargs={'pk':self.pk})
22+
23+
24+
def __str__(self):
25+
return self.title
26+
27+
28+
29+
class Comment(models.Model):
30+
post = models.ForeignKey('blog.Post', related_name='comments')
31+
author = models.CharField(max_length=200)
32+
text = models.TextField()
33+
created_date = models.DateTimeField(default=timezone.now)
34+
approved_comment = models.BooleanField(default=False)
35+
36+
def approve(self):
37+
self.approved_comment = True
38+
self.save()
39+
40+
def get_absolute_url(self):
41+
return reverse("post_list")
42+
43+
def __str__(self):
44+
return self.text

blog_project/blog/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

blog_project/blog/urls.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.conf.urls import url
2+
from . import views
3+
4+
5+
urlpatterns = [
6+
url(r'^$',views.PostListView.as_view(),name='post_list'),
7+
url(r'^about/$',views.AboutView.as_view(),name='about'),
8+
url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), name='post_detail'),
9+
url(r'^post/new/$', views.CreatePostView.as_view(), name='post_new'),
10+
url(r'^post/(?P<pk>\d+)/edit/$', views.PostUpdateView.as_view(), name='post_edit'),
11+
url(r'^drafts/$', views.DraftListView.as_view(), name='post_draft_list'),
12+
url(r'^post/(?P<pk>\d+)/remove/$', views.PostDeleteView.as_view(), name='post_remove'),
13+
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
14+
url(r'^post/(?P<pk>\d+)/comment/$', views.add_comment_to_post, name='add_comment_to_post'),
15+
url(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve, name='comment_approve'),
16+
url(r'^comment/(?P<pk>\d+)/remove/$', views.comment_remove, name='comment_remove'),
17+
]

blog_project/blog/views.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from django.shortcuts import render, get_object_or_404, redirect
2+
from django.contrib.auth.decorators import login_required
3+
from blog.models import Post, Comment
4+
from django.utils import timezone
5+
from blog.forms import PostForm, CommentForm
6+
7+
from django.views.generic import (TemplateView,ListView,
8+
DetailView,CreateView,
9+
UpdateView,DeleteView)
10+
11+
from django.urls import reverse_lazy
12+
from django.contrib.auth.mixins import LoginRequiredMixin
13+
14+
class AboutView(TemplateView):
15+
template_name = 'about.html'
16+
17+
class PostListView(ListView):
18+
model = Post
19+
20+
def get_queryset(self):
21+
return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
22+
23+
class PostDetailView(DetailView):
24+
model = Post
25+
26+
27+
class CreatePostView(LoginRequiredMixin,CreateView):
28+
login_url = '/login/'
29+
redirect_field_name = 'blog/post_detail.html'
30+
31+
form_class = PostForm
32+
33+
model = Post
34+
35+
36+
class PostUpdateView(LoginRequiredMixin,UpdateView):
37+
login_url = '/login/'
38+
redirect_field_name = 'blog/post_detail.html'
39+
40+
form_class = PostForm
41+
42+
model = Post
43+
44+
45+
class DraftListView(LoginRequiredMixin,ListView):
46+
login_url = '/login/'
47+
redirect_field_name = 'blog/post_draft_list.html'
48+
49+
model = Post
50+
51+
def get_queryset(self):
52+
return Post.objects.filter(published_date__isnull=True).order_by('created_date')
53+
54+
55+
class PostDeleteView(LoginRequiredMixin,DeleteView):
56+
model = Post
57+
success_url = reverse_lazy('post_list')
58+
59+
#######################################
60+
## Functions that require a pk match ##
61+
#######################################
62+
63+
@login_required
64+
def post_publish(request, pk):
65+
post = get_object_or_404(Post, pk=pk)
66+
post.publish()
67+
return redirect('post_detail', pk=pk)
68+
69+
@login_required
70+
def add_comment_to_post(request, pk):
71+
post = get_object_or_404(Post, pk=pk)
72+
if request.method == "POST":
73+
form = CommentForm(request.POST)
74+
if form.is_valid():
75+
comment = form.save(commit=False)
76+
comment.post = post
77+
comment.save()
78+
return redirect('post_detail', pk=post.pk)
79+
else:
80+
form = CommentForm()
81+
return render(request, 'blog/comment_form.html', {'form': form})
82+
83+
84+
@login_required
85+
def comment_approve(request, pk):
86+
comment = get_object_or_404(Comment, pk=pk)
87+
comment.approve()
88+
return redirect('post_detail', pk=comment.post.pk)
89+
90+
91+
@login_required
92+
def comment_remove(request, pk):
93+
comment = get_object_or_404(Comment, pk=pk)
94+
post_pk = comment.post.pk
95+
comment.delete()
96+
return redirect('post_detail', pk=post_pk)

0 commit comments

Comments
 (0)