Skip to content

Commit

Permalink
Merge pull request #28 from devmahmud/dev
Browse files Browse the repository at this point in the history
Improvements & bug fixes
  • Loading branch information
devmahmud authored Oct 18, 2023
2 parents 0f579eb + 941b949 commit e2d1ff5
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion accounts/templates/accounts/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block content %}
<div class="container">
<div class="row center">
<div class="col-md-6 offset-md-3">
<div class="col-md-5 mx-auto p-5 shadow-sm border rounded border-primary">
<p>Already have an account? <a href="{% url 'accounts:login' %}">Login Here</a></p>
{% if messages %}
<div class="messages">
Expand Down
13 changes: 9 additions & 4 deletions polls/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
from .models import Poll, Choice, Vote


class ChoiceInline(admin.TabularInline): # or admin.StackedInline for a different layout
model = Choice
extra = 1

@admin.register(Poll)
class PollAdmin(admin.ModelAdmin):
list_display = ["text", "owner", "pub_date", "active"]
list_display = ["text", "owner", "pub_date", "active", "created_at"]
search_fields = ["text", "owner__username"]
list_filter = ["active"]
list_filter = ["active", 'created_at', 'pub_date']
date_hierarchy = "pub_date"
inlines = [ChoiceInline]


@admin.register(Choice)
class ChoiceAdmin(admin.ModelAdmin):
list_display = ["choice_text", "poll"]
list_display = ["choice_text", "poll", 'created_at', 'updated_at']
search_fields = ["choice_text", "poll__text"]
autocomplete_fields = ["poll"]


@admin.register(Vote)
class VoteAdmin(admin.ModelAdmin):
list_display = ["choice", "poll", "user"]
list_display = ["choice", "poll", "user", 'created_at']
search_fields = ["choice__choice_text", "poll__text", "user__username"]
autocomplete_fields = ["choice", "poll", "user"]
42 changes: 42 additions & 0 deletions polls/migrations/0002_auto_20231018_1318.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 3.1.14 on 2023-10-18 07:18

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


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='choice',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='choice',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='poll',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='vote',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='vote',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
5 changes: 5 additions & 0 deletions polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Poll(models.Model):
text = models.TextField()
pub_date = models.DateTimeField(default=timezone.now)
active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)

def user_can_vote(self, user):
"""
Expand Down Expand Up @@ -50,6 +51,8 @@ def __str__(self):
class Choice(models.Model):
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

@property
def get_vote_count(self):
Expand All @@ -63,6 +66,8 @@ class Vote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice = models.ForeignKey(Choice, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f'{self.poll.text[:15]} - {self.choice.choice_text[:15]} - {self.user.username}'
2 changes: 1 addition & 1 deletion polls/templates/polls/poll_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% if poll.active %}
<h3 class="mt-3 mb-3 text-center">Result for: {{ poll.text }}</h3>
{% else %}
<h3 class="mt-3 mb-3 text-center">"{{ poll.text }}" Has Ended Polling !</h3>
<h3 class="mt-3 mb-3 text-center">"{{ poll.text }}" Has Ended Polling!</h3>
{% endif %}
<h3 class="mb-2 text-center">Total: {{ poll.get_vote_count }} votes</h3>
<!-- progress bar -->
Expand Down
4 changes: 3 additions & 1 deletion polls/templates/polls/polls_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="container">
<div class="row">
<div class="col-md-8 offset-sm-2">
<h1 class="text-center mt-3 mb-3">Welcome to polls List!</h1>
<h1 class="text-center mb-5">Welcome to polls List!</h1>
{% if messages %}
<div class="messages">
{% for message in messages %}
Expand Down Expand Up @@ -54,6 +54,7 @@ <h1 class="text-center mt-3 mb-3">Welcome to polls List!</h1>

{% endfor %}
</ul>
{% if polls.paginator.num_pages > 1 %}
<nav class="mt-3">
<ul class="pagination">
{% if polls.has_previous %}
Expand All @@ -69,6 +70,7 @@ <h1 class="text-center mt-3 mb-3">Welcome to polls List!</h1>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion polls/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
path('add/', views.polls_add, name='add'),
path('edit/<int:poll_id>/', views.polls_edit, name='edit'),
path('delete/<int:poll_id>/', views.polls_delete, name='delete_poll'),
path('end/<int:poll_id>/', views.endpoll, name='end_poll'),
path('end/<int:poll_id>/', views.end_poll, name='end_poll'),
path('edit/<int:poll_id>/choice/add/', views.add_choice, name='add_choice'),
path('edit/choice/<int:choice_id>/', views.choice_edit, name='choice_edit'),
path('delete/choice/<int:choice_id>/',
Expand Down
8 changes: 4 additions & 4 deletions polls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def polls_list(request):

get_dict_copy = request.GET.copy()
params = get_dict_copy.pop('page', True) and get_dict_copy.urlencode()
print(params)

context = {
'polls': polls,
'params': params,
Expand Down Expand Up @@ -63,9 +63,9 @@ def polls_add(request):
poll = form.save(commit=False)
poll.owner = request.user
poll.save()
new_choice1 = Choice(
Choice(
poll=poll, choice_text=form.cleaned_data['choice1']).save()
new_choice2 = Choice(
Choice(
poll=poll, choice_text=form.cleaned_data['choice2']).save()

messages.success(
Expand Down Expand Up @@ -210,7 +210,7 @@ def poll_vote(request, poll_id):


@login_required
def endpoll(request, poll_id):
def end_poll(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
if request.user != poll.owner:
return redirect('home')
Expand Down
2 changes: 1 addition & 1 deletion templates/includes/navbar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nav class="navbar navbar-expand-sm navbar-light bg-light">
<nav class="navbar navbar-expand-sm navbar-light bg-light mb-5">
<a class="navbar-brand" href="{% url 'home' %}"><i class="fas fa-person-booth"></i></a>
<button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#collapsibleNavId"
aria-controls="collapsibleNavId" aria-expanded="false" aria-label="Toggle navigation">
Expand Down

0 comments on commit e2d1ff5

Please sign in to comment.