diff --git a/src/mysite/Readme.txt b/src/mysite/Readme.txt new file mode 100644 index 0000000..6f232c5 --- /dev/null +++ b/src/mysite/Readme.txt @@ -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 diff --git a/src/mysite/blog/__init__.py b/src/mysite/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mysite/blog/admin.py b/src/mysite/blog/admin.py new file mode 100644 index 0000000..cda4327 --- /dev/null +++ b/src/mysite/blog/admin.py @@ -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. diff --git a/src/mysite/blog/apps.py b/src/mysite/blog/apps.py new file mode 100644 index 0000000..7930587 --- /dev/null +++ b/src/mysite/blog/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + name = 'blog' diff --git a/src/mysite/blog/forms.py b/src/mysite/blog/forms.py new file mode 100644 index 0000000..b7e7704 --- /dev/null +++ b/src/mysite/blog/forms.py @@ -0,0 +1,8 @@ +from .models import Comment +from django import forms + + +class CommentForm(forms.ModelForm): + class Meta: + model = Comment + fields = ('name', 'email', 'body') diff --git a/src/mysite/blog/migrations/0001_initial.py b/src/mysite/blog/migrations/0001_initial.py new file mode 100644 index 0000000..2ae2392 --- /dev/null +++ b/src/mysite/blog/migrations/0001_initial.py @@ -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'], + }, + ), + ] diff --git a/src/mysite/blog/migrations/0002_comment.py b/src/mysite/blog/migrations/0002_comment.py new file mode 100644 index 0000000..1e6c647 --- /dev/null +++ b/src/mysite/blog/migrations/0002_comment.py @@ -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'], + }, + ), + ] diff --git a/src/mysite/blog/migrations/0003_post_cover.py b/src/mysite/blog/migrations/0003_post_cover.py new file mode 100644 index 0000000..6ee9437 --- /dev/null +++ b/src/mysite/blog/migrations/0003_post_cover.py @@ -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, + ), + ] diff --git a/src/mysite/blog/migrations/__init__.py b/src/mysite/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mysite/blog/models.py b/src/mysite/blog/models.py new file mode 100644 index 0000000..879dcea --- /dev/null +++ b/src/mysite/blog/models.py @@ -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. diff --git a/src/mysite/blog/tests.py b/src/mysite/blog/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/src/mysite/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/src/mysite/blog/urls.py b/src/mysite/blog/urls.py new file mode 100644 index 0000000..d3777f8 --- /dev/null +++ b/src/mysite/blog/urls.py @@ -0,0 +1,9 @@ +from . import views +from django.urls import path + +urlpatterns = [ + path('', views.PostList.as_view(), name='home'), + path('/', views.PostDetail.as_view(), name='post_detail'), + path('/', views.post_detail, name='post_detail') + +] diff --git a/src/mysite/blog/views.py b/src/mysite/blog/views.py new file mode 100644 index 0000000..9afd8c0 --- /dev/null +++ b/src/mysite/blog/views.py @@ -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. diff --git a/src/mysite/db.sqlite3 b/src/mysite/db.sqlite3 new file mode 100644 index 0000000..a74a63a Binary files /dev/null and b/src/mysite/db.sqlite3 differ diff --git a/src/mysite/manage.py b/src/mysite/manage.py new file mode 100644 index 0000000..341863c --- /dev/null +++ b/src/mysite/manage.py @@ -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() diff --git a/src/mysite/media/images/img1.png b/src/mysite/media/images/img1.png new file mode 100644 index 0000000..f1d9f8c Binary files /dev/null and b/src/mysite/media/images/img1.png differ diff --git a/src/mysite/mysite/__init__.py b/src/mysite/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mysite/mysite/asgi.py b/src/mysite/mysite/asgi.py new file mode 100644 index 0000000..35d925e --- /dev/null +++ b/src/mysite/mysite/asgi.py @@ -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() diff --git a/src/mysite/mysite/settings.py b/src/mysite/mysite/settings.py new file mode 100644 index 0000000..39f6a9f --- /dev/null +++ b/src/mysite/mysite/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 3.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TEMPLATES_DIR = os.path.join(BASE_DIR,'templates') +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'm_=nq9ggeug!fk&yq#5ko9g88hoxu2h2u8h6g4sdil!md@cr@8' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'blog.apps.BlogConfig', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'mysite.urls' +MEDIA_URL = '/media/' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATES_DIR], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/src/mysite/mysite/urls.py b/src/mysite/mysite/urls.py new file mode 100644 index 0000000..a9b14d2 --- /dev/null +++ b/src/mysite/mysite/urls.py @@ -0,0 +1,27 @@ +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.conf import settings +from django.urls import path, include +from django.conf.urls.static import static + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('blog.urls')), +] + +if settings.DEBUG: # new + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/src/mysite/mysite/wsgi.py b/src/mysite/mysite/wsgi.py new file mode 100644 index 0000000..dbe7bb5 --- /dev/null +++ b/src/mysite/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_wsgi_application() diff --git a/src/mysite/templates/base.html b/src/mysite/templates/base.html new file mode 100644 index 0000000..2585707 --- /dev/null +++ b/src/mysite/templates/base.html @@ -0,0 +1,104 @@ + + + + ZOIT NEWSROOM + + + + + + + + + + + + + + + + {% block content %} + + + {% endblock content %} + + +
+

Copyright © Django Central

+
+ + + diff --git a/src/mysite/templates/index.html b/src/mysite/templates/index.html new file mode 100644 index 0000000..b13ec04 --- /dev/null +++ b/src/mysite/templates/index.html @@ -0,0 +1,81 @@ +{% extends "base.html" %} + + {% block content %} + + +
+
+
+
+
+
+

WELCOME TO ZOIT NEWSROOM

+

We Love YOU As much as you do..! +

+
+
+
+
+ +
+ +
+
+ + +
+ {% for post in post_list %} +
+
+

{{ post.title }}

+

{{ post.author }} | {{ post.created_on}}

+ +

{{post.content|slice:":200" }}

+ Read More → +
+ +
+ {% endfor %} +
+ {% block sidebar %} + {% include 'sidebar.html' %} + {% endblock sidebar %} +
+
+{%endblock%} + +{% if is_paginated %} + + + + +{% endif %} +

Django Image Uploading

+ \ No newline at end of file diff --git a/src/mysite/templates/post_detail.html b/src/mysite/templates/post_detail.html new file mode 100644 index 0000000..648ce15 --- /dev/null +++ b/src/mysite/templates/post_detail.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} {% block content %} + +
+
+
+
+

{% block title %} {{ object.title }} {% endblock title %}

+

{{ post.author }} | {{ post.created_on }}

+

{{ object.content | safe }}

+
+
+ {% block sidebar %} {% include 'sidebar.html' %} {% endblock sidebar %} +
+
+ +{% endblock content %} + + diff --git a/src/mysite/templates/sidebar.html b/src/mysite/templates/sidebar.html new file mode 100644 index 0000000..cfb5b36 --- /dev/null +++ b/src/mysite/templates/sidebar.html @@ -0,0 +1,22 @@ +{% block sidebar %} + + + + +
+
+
About Us
+
+

This awesome blog is made on the top of our Favourite full stack Framework 'Django', follow up the tutorial to learn how we made it..!

+ Know more! +
+
+
+ +{% endblock sidebar %} diff --git a/src/mysite/templates/views.html b/src/mysite/templates/views.html new file mode 100644 index 0000000..fdb6f32 --- /dev/null +++ b/src/mysite/templates/views.html @@ -0,0 +1,51 @@ +{% extends 'base.html' %} {% block content %} + +
+
+
+
+

{% block title %} {{ post.title }} {% endblock title %}

+

{{ post.author }} | {{ post.created_on }}

+

{{ post.content | safe }}

+
+
+ + {% block sidebar %} {% include 'sidebar.html' %} {% endblock sidebar %} + +
+
+ +

{{ comments.count }} comments

+ + {% for comment in comments %} +
+

+ {{ comment.name }} + + {{ comment.created_on }} + +

+ {{ comment.body | linebreaks }} +
+ {% endfor %} +
+
+
+
+ {% if new_comment %} + + {% else %} +

Leave a comment

+
+ {{ comment_form.as_p }} + {% csrf_token %} + +
+ {% endif %} +
+
+
+
+{% endblock content %} diff --git a/src/usersapp/migrations/0008_auto_20200608_1958.py b/src/usersapp/migrations/0008_auto_20200608_1958.py new file mode 100644 index 0000000..b551f88 --- /dev/null +++ b/src/usersapp/migrations/0008_auto_20200608_1958.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.6 on 2020-06-09 02:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('usersapp', '0007_auto_20200601_2102'), + ] + + operations = [ + migrations.AlterField( + model_name='incident', + name='accident_location', + field=models.CharField(choices=[('FCT', 'FCT'), ('Abia', 'Abia'), ('Adamawa', 'Adamawa'), ('Akwa Ibom', 'Akwa Ibom'), ('Anambra', 'Anambra'), ('Bauchi', 'Bauchi'), ('Bayelsa', 'Bayelsa'), ('Benue', 'Benue'), ('Borno', 'Borno'), ('Cross River', 'Cross River'), ('Delta', 'Delta'), ('Ebonyi', 'Ebonyi'), ('Enugu', 'Enugu'), ('Edo', 'Edo'), ('Ekiti', 'Ekiti'), ('Gombe', 'Gombe'), ('Imo', 'Imo'), ('Jigawa', 'Jigawa'), ('Kaduna', 'Kaduna'), ('Kano', 'Kano'), ('Katsina', 'Katsina'), ('Kebbi', 'Kebbi'), ('Kogi', 'Kogi'), ('Kwara', 'Kwara'), ('Lagos', 'Lagos'), ('Nasarawa', 'Nasarawa'), ('Niger', 'Niger'), ('Ogun', 'Ogun'), ('Ondo', 'Ondo'), ('Osun', 'Osun'), ('Oyo', 'Oyo'), ('Plateau', 'Plateau'), ('Rivers', 'Rivers'), ('Sokoto', 'Sokoto'), ('Taraba', 'Taraba'), ('Yobe', 'Yobe'), ('Zamfara', 'Zamfara')], max_length=45), + ), + ]