Skip to content

Commit

Permalink
Merge pull request #35 from BuildForSDG/ft-zoitblog
Browse files Browse the repository at this point in the history
Ft zoitblog
  • Loading branch information
happyjosh-tech authored Jun 10, 2020
2 parents ab658f2 + 29da911 commit 753a92b
Show file tree
Hide file tree
Showing 27 changed files with 733 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/mysite/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Description
This Pull Request contains the Blog application for ZOIT Emergency Accident Response project. Since one of the objective of the project is to make a social website where users can interact, post and read present and past accident information nearest and distant from them. The blog shall be updated everyday by an admin who shall gather accident complaints from accident victims and eyewitness report to validate, post and update the general public.
The application was built with Django full stack with all the dependencies installed.
get the Readme file of the src folder to get instructions on how to clone and install dependencies and libraries for the repository.
This pull request is related to issue number #18

## How Has This Been Tested?
1. First locate the application directory on the Team-102-GoodHealthAndWellBeing repo by:
cd Team-102-GoodHealthAndWellBeing
cd src
cd mysite
2. create your own virtual env or try 'workon zoitblog'
3 Create superuser in other to access the admin
4. Run server with python manage.py runserver
5. Run python -m pip install Pillow to enable the app access ImageField
Empty file added src/mysite/blog/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions src/mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.contrib import admin
from .models import Post, Comment

class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'status','created_on')
list_filter = ("status",)
search_fields = ['title', 'content']
prepopulated_fields = {'slug': ('title',)}


admin.site.register(Post, PostAdmin)

@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('name', 'body', 'post', 'created_on', 'active')
list_filter = ('active', 'created_on')
search_fields = ('name', 'email', 'body')
actions = ['approve_comments']

def approve_comments(self, request, queryset):
queryset.update(active=True)
# Register your models here.
5 changes: 5 additions & 0 deletions src/mysite/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
name = 'blog'
8 changes: 8 additions & 0 deletions src/mysite/blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .models import Comment
from django import forms


class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name', 'email', 'body')
33 changes: 33 additions & 0 deletions src/mysite/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.0.6 on 2020-05-27 00:42

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='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200, unique=True)),
('slug', models.SlugField(max_length=200, unique=True)),
('updated_on', models.DateTimeField(auto_now=True)),
('content', models.TextField()),
('created_on', models.DateTimeField(auto_now_add=True)),
('status', models.IntegerField(choices=[(0, 'Draft'), (1, 'Publish')], default=0)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-created_on'],
},
),
]
29 changes: 29 additions & 0 deletions src/mysite/blog/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.0.6 on 2020-05-27 15:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=80)),
('email', models.EmailField(max_length=254)),
('body', models.TextField()),
('created_on', models.DateTimeField(auto_now_add=True)),
('active', models.BooleanField(default=False)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.Post')),
],
options={
'ordering': ['created_on'],
},
),
]
20 changes: 20 additions & 0 deletions src/mysite/blog/migrations/0003_post_cover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.6 on 2020-06-09 13:20

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_comment'),
]

operations = [
migrations.AddField(
model_name='post',
name='cover',
field=models.ImageField(default=django.utils.timezone.now, upload_to='images/'),
preserve_default=False,
),
]
Empty file.
40 changes: 40 additions & 0 deletions src/mysite/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.db import models
from django.contrib.auth.models import User


STATUS = (
(0,"Draft"),
(1,"Publish")
)

class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
cover = models.ImageField(upload_to='images/')

class Meta:
ordering = ['-created_on']

def __str__(self):
return self.title

class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)

class Meta:
ordering = ['created_on']

def __str__(self):
return 'Comment {} by {}'.format(self.body, self.name)

# Create your models here.
3 changes: 3 additions & 0 deletions src/mysite/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions src/mysite/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from . import views
from django.urls import path

urlpatterns = [
path('', views.PostList.as_view(), name='home'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('<slug:slug>/', views.post_detail, name='post_detail')

]
50 changes: 50 additions & 0 deletions src/mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.views import generic
from .models import Post
from .forms import CommentForm
from django.shortcuts import render, get_object_or_404

class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'

class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
paginate_by = 3

class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'

class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
def post_detail(request, slug):
template_name = 'post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():

# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()

return render(request, template_name, {'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})


# Create your views here.
Binary file added src/mysite/db.sqlite3
Binary file not shown.
21 changes: 21 additions & 0 deletions src/mysite/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Binary file added src/mysite/media/images/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added src/mysite/mysite/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions src/mysite/mysite/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for mysite project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_asgi_application()
Loading

0 comments on commit 753a92b

Please sign in to comment.