Initial Forum build

This commit is contained in:
Pratyush Desai 2022-06-20 21:18:20 +05:30
parent 0190bc9d45
commit 31a2e4004c
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
15 changed files with 332 additions and 0 deletions

View File

@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'forum.apps.ForumConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

0
forum/__init__.py Normal file
View File

7
forum/admin.py Normal file
View File

@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Post, Replie, Profile
admin.site.register(Post)
admin.site.register(Replie)
admin.site.register(Profile)

6
forum/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ForumConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'forum'

View File

@ -0,0 +1,47 @@
# Generated by Django 4.0.4 on 2022-06-20 15:45
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('post_content', models.CharField(max_length=5000)),
('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
('image', models.ImageField(default='', upload_to='images')),
('user1', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Reply',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reply_content', models.CharField(max_length=5000)),
('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
('image', models.ImageField(default='', upload_to='images')),
('post', models.ForeignKey(default='', on_delete=django.db.models.deletion.CASCADE, to='forum.post')),
('user', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Profile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(default='default/user.png', upload_to='images')),
('user', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

25
forum/models.py Normal file
View File

@ -0,0 +1,25 @@
from django.db import models
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
image = models.ImageField(upload_to="images",default="default/user.png")
class Post(models.Model):
user1 = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
post_id = models.AutoField
post_content = models.CharField(max_length=5000)
timestamp= models.DateTimeField(default=now)
image = models.ImageField(upload_to="images",default="")
class Reply(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
reply_id = models.AutoField
reply_content = models.CharField(max_length=5000)
post = models.ForeignKey(Post, on_delete=models.CASCADE, default='')
timestamp= models.DateTimeField(default=now)
image = models.ImageField(upload_to="images",default="")

View File

@ -0,0 +1,44 @@
<title>{% block title %} {% endblock %}</title>
<style>
{% block css %} {% endblock %}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="/">Discussion Forum</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>
{% if user.is_authenticated %}
<ul class="navbar-nav mr-2">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href='#' id="navbarDropdown" role="button" data-toggle="dropdown"> Welcome {{request.user}}</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/logout">Logout</a>
</div>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="/myprofile">Profile</a>
</li>
</ul>
</div>
</nav>
<br>
</body>
{% block body %} {% endblock %}

View File

@ -0,0 +1,37 @@
{% for reply in replies %}
<br>
<div class="container">
<div class="media">
<img class="mr-3" src="/media/{{reply.image}}" alt="." width="30px" height="30px">
<div class="media-body">
<h4 class="mt-0">{{reply.user}} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span><small style="font-size: 13px;">({{reply.timestamp}})</small></span></h4>
<h5>{{reply.reply_content}}</h5>
</div>
</div>
</div>
{% endfor %}
<!-- Modal -->
<div class="modal fade" id="reply" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="/discussion/{{post.id}}/" method="POST"> {% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1">Post Your Solution Here</label>
<input type="hidden" name="post_id" value="{{post.id}}">
<textarea class="form-control" id="desc" name="desc" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Post Solution</button>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,60 @@
<div class="container my-4">
<div class="jumbotron">
<h1 class="display-4">Project Gurukul Disscussion Forum</h1>
<p class="lead">This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
<hr class="my-4">
<p>It uses utility classes for typography and spacing to space content out within the larger container.</p>
<button class="btn btn-primary btn-lg" data-target="#questions" data-toggle="modal" role="button">Add Question</button>
</div>
{% for post in posts %}
<div class="container-fluid mt-10">
<div class="row">
<div class="col-md-12">
<div class="card mb-4">
<div class="card-header">
<div class="media flex-wrap w-100 align-items-center"> <img src="/media/{{post.image}}"
class="d-block ui-w-40 rounded-circle" alt="">
<div class="media-body ml-3"> <a href="/discussion/{{post.id}}" data-abc="true">{{post.user1}}</a>
</div>
<div class="text-muted small ml-3">
<div class="px-4 pt-3">{{post.timestamp}} </div>
</div>
</div>
</div>
<div class="card-body">
<p>{{post.post_content}}</p>
</div>
<div class="card-footer d-flex flex-wrap justify-content-between align-items-center px-0 pt-0 pb-3">
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
<!-- Modal -->
<div class="modal fade" id="questions" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="/" method="POST"> {% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1">Post Your Question Here</label>
<textarea class="form-control" id="content" name="content" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Post</button>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,10 @@
<div id="login-box">
<div class="items">
<h1>Log In</h1>
<form action="/login/" method="post"> {% csrf_token %}
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" name="signup_submit" value="Log In" />
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,19 @@
{% block body %}
<br>
<div id="login-box">
<div class="items">
<h1>Register</h1>
<form action="/register/" method="post"> {% csrf_token %}
<input type="text" name="username" placeholder="Username" />
<input type="email" name="email" placeholder="E-mail" />
<input type="text" name="first_name" placeholder="First Name" />
<input type="text" name="last_name" placeholder="Last Name" />
<input type="password" name="password" placeholder="Password" />
<input type="password" name="confirm_password" placeholder="Confirm your password" />
<input type="submit" name="signup_submit" value="Register" />
</div>
</div>
{% endblock %}

3
forum/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
forum/urls.py Normal file
View File

@ -0,0 +1,11 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.forum, name="Forum"),
path("discussion/<int:myid>/", views.discussion, name="Discussions"),
path("register/", views.UserRegister, name="Register"),
path("login/", views.UserLogin, name="Login"),
path("logout/", views.UserLogout, name="Logout"),
path("myprofile/", views.myprofile, name="Myprofile"),
]

62
forum/views.py Normal file
View File

@ -0,0 +1,62 @@
from django.shortcuts import render, redirect, HttpResponse, Http404
from .models import User, Profile, Post, Reply
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
def UserRegister(request):
if request.method=="POST":
username = request.POST['username']
email = request.POST['email']
first_name=request.POST['first_name']
last_name=request.POST['last_name']
password = request.POST['password']
confirm_password = request.POST['confirm_password']
user = User.objects.create_user(username, email, password)
user.first_name = first_name
user.last_name = last_name
user.save()
return render(request, 'login.html')
return render(request, "register.html")
def UserLogin(request):
if request.method=="POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
messages.success(request, "Successfully Logged In")
return redirect("/myprofile")
else:
return HttpResponse("User not Found")
alert = True
return render(request, 'login.html', {'alert':alert})
return render(request, "login.html")
def forum(request):
profile = Profile.objects.all()
if request.method=="POST":
user = request.user
image = request.user.profile.image
content = request.POST.get('content','')
post = Post(user1=user, post_content=content, image=image)
post.save()
alert = True
return render(request, "forum.html", {'alert':alert})
posts = Post.objects.all()
return render(request, "forum.html", {'posts':posts})
def discussion(request, myid):
post = Post.objects.filter(id=myid).first()
replies = Reply.objects.filter(post=post)
if request.method=="POST":
user = request.user
image = request.user.profile.image
desc = request.POST.get('desc','')
post_id =request.POST.get('post_id','')
reply = Reply(user = user, reply_content = desc, post=post, image=image)
reply.save()
alert = True
return render(request, "discussion.html", {'alert':alert})
return render(request, "discussion.html", {'post':post, 'replies':replies})