Commit c78077df authored by Ray Rafael Abenido's avatar Ray Rafael Abenido

New Feature: Partial manual merge with branch 'forum'.

parent 1aee063d
...@@ -4,7 +4,7 @@ from django.db import models ...@@ -4,7 +4,7 @@ from django.db import models
# Create your models here. # Create your models here.
class Post(models.Model): class Post(models.Model):
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE) author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE)
post_image = models.ImageField(null=True) post_image = models.ImageField(null=True, upload_to='')
post_title = models.CharField(max_length=100) post_title = models.CharField(max_length=100)
post_body = models.TextField() post_body = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True, null=True) pub_date = models.DateTimeField(auto_now_add=True, null=True)
......
from django.urls import path from django.urls import path
from . import views from . import views
from .views import CreateNewPost
urlpatterns = [ urlpatterns = [
path('', views.show_index, name="Show Forum Page"), path('', views.show_index, name="Show Forum Page"),
path('post/<int:post_id>/details', views.show_post, name="Show Post Details"), path('post/<int:post_id>/details', views.show_post, name="Show Post Details"),
path('posts/add', CreateNewPost.show_form, name="Show Add Post"),
# Disabled for the time being. May be used again in the near future. path('post/add/success', CreateNewPost.add_new_post, name="Show Confirmation"),
#path('post/add', views.show_form, name="Add Post Form"),
#path('post/confirmation', views.show_confirmation, name="Show confirmation"),
] ]
\ No newline at end of file
# =================================
# MODULE IMPORTS
# =================================
# importing from django
from django.shortcuts import render from django.shortcuts import render
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from django.views import View
from django.core.files.storage import FileSystemStorage
# importing from other apps in project
from .models import Post, Reply from .models import Post, Reply
from homepage.models import WidgetUser
# from python
import datetime
# Create your views here. # =================================
# FUNCTION LIST
# =================================
# for showing list of posts
def show_index(request): def show_index(request):
posts = Post.objects.all().order_by("-pub_date") posts = Post.objects.all().order_by("-pub_date")
return render(request, 'forum/index.html', {'posts':posts}) return render(request, 'forum/index.html', {'posts':posts})
# for showing post details given a post_id
def show_post(request, post_id): def show_post(request, post_id):
try: try:
post = Post.objects.get(pk=post_id) post = Post.objects.get(pk=post_id)
...@@ -16,16 +34,30 @@ def show_post(request, post_id): ...@@ -16,16 +34,30 @@ def show_post(request, post_id):
replies = Reply.objects.filter(post=post_id).order_by("-pub_date") replies = Reply.objects.filter(post=post_id).order_by("-pub_date")
return render(request, 'forum/post_details.html', {'post':post, 'replies': replies}) return render(request, 'forum/post_details.html', {'post':post, 'replies': replies})
# The following functions are not used for the time being. May be used again class CreateNewPost(View):
# in the near future. # for adding a new post to database, then returning back to reply list
def show_form(request):
def show_form(request): users = WidgetUser.objects.all()
return render(request, 'forum/post_form.html', {}) return render(request, 'forum/post_form.html', {'users':users})
def show_confirmation(request): # for showing form where users can add new posts.
if (request.method == "POST"): def add_new_post(request):
# populate new record
title = request.POST.get("post_title") title = request.POST.get("post_title")
body = request.POST.get("post_body") body = request.POST.get("post_body")
author = request.POST.get("author_id") author = request.POST.get("post_author")
post = Post.objects.create(post_title=title,post_body=body,author_id=author) image = request.FILES['post_image']
return HttpResponse("Article saved!") current_date = datetime.datetime.now()
\ No newline at end of file
new_post = Post.objects.create(post_title=title, post_body=body,author_id=author,
post_image=image,pub_date=current_date)
#upload file into static\images folder
fss = FileSystemStorage()
file = fss.save(image.name, image)
return render(request, 'forum/confirmation.html', {})
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