Commit 208546e5 authored by Neal Berones's avatar Neal Berones

feat: created detail and index templates for announcements app

parents 98f4adc0 fffc9323
{% extends "base.html" %} {% extends "base.html" %}
{% block page-title %}Announcements Page{% endblock %}
{% block content %} {% block content %}
<head>
<h1>{{ announcement.announcement_title }}</h1>
<h2>by {{ announcement.author.first_name }} {{ announcement.author.last_name }}, {{ announcement.pub_date|date:"SHORT_DATE_FORMAT" }}</h2>
</head>
<body>
<p>{{ announcement.announcement_body }}</p>
<script>
var like = 0;
var love = 0;
var angry = 0;
for (let r in reaction_list){
if (r.reaction_name == "Like"){
like++;
}
else if (r.reaction_name == "Love"){
love++;
}
else if (r.reaction_name == "Angry"){
angry++;
}
}
document.getElementById("Like").innerHTML = like;
document.getElementById("Love").innerHTML = love;
document.getElementById("Angry").innerHTML = angry;
</script>
<p id = "Like"></p>
<p id = "Love"></p>
<p id = "Angry"></p>
{% endblock %} {% endblock %}
{% extends "base.html" %}
{% block page-title %}Announcements Page{% endblock %}
{% block content %}
<h1>Announcement Board</h1>
<body>
<p>Important announcements:</p>
<ul>
{% for announcement in announcements_list %}
<li><a href="{% url 'announcements:detail' announcement.id %}">
{{ announcement.announcement_title }}
by {{ announcement.author.first_name }} {{ announcement.author.last_name }}
dated {{ announcement.pub_date|date:"SHORT_DATE_FORMAT" }}
</a>
</li>
{% endfor %}
</ul>
</body>
{% endblock %}
from django.urls import path from django.urls import path
from . import views from . import views
app_name = "announcements"
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('welcome', views.welcome, name='welcome') path('/announcements/<int:announcement_id>/detail', views.detail, name="detail")
#path('welcome', views.welcome, name='welcome')
] ]
from django.http import HttpResponse from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render
from .models import WidgetUser, Announcement, Reaction from .models import WidgetUser, Announcement, Reaction
from django.template import loader
# Create your views here. # Create your views here.
def welcome(request):
return HttpResponse("This is the Announcement Board!")
def index(request): def index(request):
announcement_view = 'ANNOUNCEMENTS: ' announcements_list = Announcement.objects.all().order_by("pub_date")
announcements = Announcement.objects.all() #template = loader.get_template("announcements/index.html")
reactions = Reaction.objects.all() context = {
"announcements_list":announcements_list
}
return render(request, "announcements/index.html", context)
#HttpResponse(template.render(context, request))
def detail(request, announcement_id):
try:
announcement = Announcement.objects.get(pk=announcement_id)
reaction_list = Reaction.objects.get(announcment_id=announcement_id)
except Announcement.DoesNotExist:
raise Http404("Announcement does not exist!")
context = {
"announcement": announcement,
"reaction_list": reaction_list
}
return render(request, "announcements/detail.html", context)
for a in announcements: # Code from previous lab activities:
like_tally=0 #def welcome(request):
love_tally=0 # return HttpResponse("This is the Announcement Board!")
angry_tally=0
for r in reactions:
if r.reaction_name=="Like" and r.announcement==a:
like_tally+=r.tally
elif r.reaction_name=="Love" and r.announcement==a:
love_tally+=r.tally
elif r.reaction_name=="Angry" and r.announcement==a:
angry_tally+=r.tally
announcement_view += "<br/> {} by {} {} dated {}: <br/> {} <br/> Like: {} <br/> Love: {} <br/> Angry: {} <br/>".\
format(a.announcement_title,
a.author.first_name,
a.author.last_name,
a.pub_date,
a.announcement_body,
like_tally,
love_tally,
angry_tally)
return HttpResponse(announcement_view) # announcement_view = 'ANNOUNCEMENTS: '
# announcements = Announcement.objects.all()
# reactions = Reaction.objects.all()
#
# like_tally=0
# love_tally=0
# angry_tally=0
# for r in reactions:
# if r.reaction_name=="Like" and r.announcement==a:
# like_tally+=r.tally
# elif r.reaction_name=="Love" and r.announcement==a:
# love_tally+=r.tally
# elif r.reaction_name=="Angry" and r.announcement==a:
# angry_tally+=r.tally
# announcement_view += "<br/> {} by {} {} dated {}: <br/> {} <br/> Like: {} <br/> Love: {} <br/> Angry: {} <br/>".\
# format(a.announcement_title,
# a.author.first_name,
# a.author.last_name,
# a.pub_date,
# a.announcement_body,
# like_tally,
# love_tally,
# angry_tally)
#
# return HttpResponse(announcement_view)
from django.forms import ModelForm
from .models import WidgetUser, Department
class UserForm(ModelForm):
class Meta:
model = WidgetUser
fields = ["last_name", "first_name",
"middle_name", "id_num",
"email", "department",
"profile_url"]
# Generated by Django 4.0.3 on 2022-04-05 11:02 # Generated by Django 4.0.3 on 2022-05-15 15:43
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
...@@ -11,6 +11,11 @@ class Migration(migrations.Migration): ...@@ -11,6 +11,11 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
migrations.AddField(
model_name='widgetuser',
name='profile_url',
field=models.FileField(null=True, upload_to='homepage/static/'),
),
migrations.AlterField( migrations.AlterField(
model_name='widgetuser', model_name='widgetuser',
name='id_num', name='id_num',
......
# Generated by Django 4.0.3 on 2022-05-15 09:25 # Generated by Django 4.0.3 on 2022-05-15 16:36
from django.db import migrations, models from django.db import migrations, models
...@@ -6,13 +6,13 @@ from django.db import migrations, models ...@@ -6,13 +6,13 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('homepage', '0009_remove_widgetuser_homeunit'), ('homepage', '0007_widgetuser_profile_url_alter_widgetuser_id_num'),
] ]
operations = [ operations = [
migrations.AddField( migrations.AlterField(
model_name='widgetuser', model_name='widgetuser',
name='profile_url', name='profile_url',
field=models.FileField(blank=True, null=True, upload_to='homepage/static/'), field=models.FileField(null=True, upload_to='static/'),
), ),
] ]
# Generated by Django 4.0.3 on 2022-05-06 09:01
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0007_alter_widgetuser_id_num'),
]
operations = [
migrations.AddField(
model_name='widgetuser',
name='homeunit',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='homeunit', to='homepage.department'),
),
]
# Generated by Django 4.0.3 on 2022-05-06 09:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('homepage', '0008_widgetuser_homeunit'),
]
operations = [
migrations.RemoveField(
model_name='widgetuser',
name='homeunit',
),
]
...@@ -19,7 +19,7 @@ class WidgetUser(models.Model): ...@@ -19,7 +19,7 @@ class WidgetUser(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
null=True, null=True,
related_name='department') related_name='department')
profile_url = models.FileField(upload_to='homepage/static/', blank=True, null=True) profile_url = models.FileField(upload_to='static/', null=True)
def __str__(self): def __str__(self):
return self.first_name return self.first_name
......
{% extends "base.html" %}
{% block page-title %}Add Widget User{% endblock %}
{% block content %}
<div style = "position: relative; left:80px; top:30px;">
<h1>ADD WIDGET USER</h1>
</div>
<div style = "position: relative; left:80px; top:30px;">
<form method="POST" action ="{% url 'homepage:add' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ user_form.as_p }}
<button class="button" type="Save New User">Save New User</button>
<button onclick="location.href = '/homepage'">Return to Homepage</button>
</form>
</div>
{% endblock %}
\ No newline at end of file
...@@ -5,16 +5,22 @@ ...@@ -5,16 +5,22 @@
{% block content %} {% block content %}
<div style = "position: relative; left:80px; top:30px;"> <div style = "position: relative; left:80px; top:30px;">
<h1>WELCOME TO WIDGET!</h1> <h1>WELCOME TO WIDGET!</h1>
</div> </div>
<div style = "position:relative; left:80px; top:35px;"> <div style = "position:relative; left:80px; top:35px;">
<p>Widget Users: <p>Widget Users:</p>
<p>
{% if widget_user %}
<ol> <ol>
{% for user in widget_user %} {% for user in widget_user %}
<li><a href="/users/{{ user.id_num }}/details/"> <li><a href="/users/{{ user.id_num }}/details/">
{{ user.last_name }}, {{ user.first_name }} {{ user.middle_name }} {{ user.last_name }}, {{ user.first_name }} {{ user.middle_name }}
</a></li> </a></li>
{% endfor %} {% endfor %}
<button onclick="location.href = '/users/add'">Add Widget User</button>
</ol> </ol>
</p> </p>
{% else %}
<p>No users are available.</p>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
app_name = 'homepage'
urlpatterns = [ urlpatterns = [
path('homepage/', views.index, name='index'), path('homepage/', views.HomePageView.as_view(), name='index'),
path('welcome', views.welcome, name='welcome'), path('welcome', views.welcome, name='welcome'),
path('users/add/', views.add, name='add'),
path("users/<int:id_num>/details/", views.details, name='details') path("users/<int:id_num>/details/", views.details, name='details')
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from .models import Department, WidgetUser from .models import Department, WidgetUser
from .forms import UserForm
from django.views import View
# Create your views here. # Create your views here.
def welcome(request): def welcome(request):
return HttpResponse('Welcome to Widget!') return HttpResponse('Welcome to Widget!')
def index(request): class HomePageView(View):
def get(self, request):
widget_user = WidgetUser.objects.order_by("last_name") widget_user = WidgetUser.objects.order_by("last_name")
context = { return render(request, "homepage/index.html", {"widget_user": widget_user})
"widget_user": widget_user
}
return render(request, "homepage/index.html", context)
''' '''
...@@ -36,7 +36,17 @@ def index(request): ...@@ -36,7 +36,17 @@ def index(request):
def details(request, id_num): def details(request, id_num):
try: try:
details = WidgetUser.objects.get(pk=id_num) details = WidgetUser.objects.get(id_num=id_num)
except WidgetUser.DoesNotExist: except WidgetUser.DoesNotExist:
raise Http404("User does not exist.") raise Http404("User does not exist.")
return render(request, "homepage/details.html", {"details": details}) return render(request, "homepage/details.html", {"details": details})
def add(request):
if request.method == "POST":
user_form = UserForm(request.POST, request.FILES)
if user_form.is_valid():
user_form.save()
return redirect("/homepage")
else:
user_form = UserForm()
return render(request,"homepage/add.html", {"user_form": user_form})
\ No newline at end of file
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