mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-19 05:24:05 +01:00
Add Tag field for Question in Admin Page
This commit is contained in:
parent
5dd381c44d
commit
bd8fa9229b
@ -1,6 +1,6 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import Choice, Question
|
from .models import Choice, Question, Tag
|
||||||
|
|
||||||
|
|
||||||
class ChoiceInline(admin.TabularInline):
|
class ChoiceInline(admin.TabularInline):
|
||||||
@ -15,12 +15,14 @@ class QuestionAdmin(admin.ModelAdmin):
|
|||||||
("End date", {"fields": ["end_date"], "classes": ["collapse"]}),
|
("End date", {"fields": ["end_date"], "classes": ["collapse"]}),
|
||||||
("Short Description", {"fields": ["short_description"], "classes": ["collapse"]}),
|
("Short Description", {"fields": ["short_description"], "classes": ["collapse"]}),
|
||||||
("Long Description", {"fields": ["long_description"], "classes": ["collapse"]}),
|
("Long Description", {"fields": ["long_description"], "classes": ["collapse"]}),
|
||||||
|
("Add Tag", {"fields": ["tags"], "classes": ["collapse"]})
|
||||||
]
|
]
|
||||||
list_display = ["question_text", "pub_date", "end_date", "was_published_recently", "can_vote", "trending_score"]
|
list_display = ["question_text", "pub_date", "end_date", "was_published_recently", "can_vote", "trending_score", "get_tags"]
|
||||||
inlines = [ChoiceInline]
|
inlines = [ChoiceInline]
|
||||||
list_filter = ["pub_date", "end_date"]
|
list_filter = ["pub_date", "end_date"]
|
||||||
search_fields = ["question_text"]
|
search_fields = ["question_text"]
|
||||||
|
|
||||||
|
# https://stackoverflow.com/questions/10904848/adding-inline-many-to-many-objects-in-django-admin
|
||||||
admin.site.register(Question, QuestionAdmin)
|
admin.site.register(Question, QuestionAdmin)
|
||||||
|
admin.site.register(Tag) # Add Field to modify tags objects in Question
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ class Tag(models.Model):
|
|||||||
tag_text = models.CharField(max_length=50)
|
tag_text = models.CharField(max_length=50)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.tag_text
|
||||||
|
|
||||||
|
|
||||||
class Question(models.Model):
|
class Question(models.Model):
|
||||||
@ -128,6 +128,7 @@ class Question(models.Model):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def time_left(self):
|
def time_left(self):
|
||||||
|
"""Return time till ending of the poll"""
|
||||||
return self.calculate_time_left()
|
return self.calculate_time_left()
|
||||||
|
|
||||||
def calculate_vote_percentage(self):
|
def calculate_vote_percentage(self):
|
||||||
@ -143,10 +144,12 @@ class Question(models.Model):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def up_vote_percentage(self):
|
def up_vote_percentage(self):
|
||||||
|
"""Retrieve up vote percentage from calculate_vote_percentage"""
|
||||||
return self.calculate_vote_percentage()[0]
|
return self.calculate_vote_percentage()[0]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def down_vote_percentage(self):
|
def down_vote_percentage(self):
|
||||||
|
"""Retrieve down vote percentage from calculate_vote_percentage"""
|
||||||
return self.calculate_vote_percentage()[1]
|
return self.calculate_vote_percentage()[1]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -191,10 +194,12 @@ class Question(models.Model):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def up_vote_count(self):
|
def up_vote_count(self):
|
||||||
|
"""Count up vote of Question"""
|
||||||
return self.sentimentvote_set.filter(question=self, vote_types=True).count()
|
return self.sentimentvote_set.filter(question=self, vote_types=True).count()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def down_vote_count(self):
|
def down_vote_count(self):
|
||||||
|
"""Count down vote of Question"""
|
||||||
return self.sentimentvote_set.filter(question=self, vote_types=False).count()
|
return self.sentimentvote_set.filter(question=self, vote_types=False).count()
|
||||||
|
|
||||||
def trending_score(self, up=None, down=None):
|
def trending_score(self, up=None, down=None):
|
||||||
@ -218,7 +223,6 @@ class Question(models.Model):
|
|||||||
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
"""Modify save method of Question object"""
|
"""Modify save method of Question object"""
|
||||||
# to-be-added instance # * https://github.com/django/django/blob/866122690dbe233c054d06f6afbc2f3cc6aea2f2/django/db/models/base.py#L447
|
# to-be-added instance # * https://github.com/django/django/blob/866122690dbe233c054d06f6afbc2f3cc6aea2f2/django/db/models/base.py#L447
|
||||||
@ -229,6 +233,9 @@ class Question(models.Model):
|
|||||||
self.trend_score = self.trending_score(up=0, down=0)
|
self.trend_score = self.trending_score(up=0, down=0)
|
||||||
super(Question, self).save(*args, **kwargs)
|
super(Question, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
def get_tags(self, *args, **kwargs):
|
||||||
|
return "-".join([tag.tag_text for tag in self.tags.all()])
|
||||||
|
|
||||||
|
|
||||||
class Choice(models.Model):
|
class Choice(models.Model):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -93,6 +93,10 @@ class DetailView(LoginRequiredMixin, generic.DetailView):
|
|||||||
|
|
||||||
|
|
||||||
class ResultsView(LoginRequiredMixin, generic.DetailView):
|
class ResultsView(LoginRequiredMixin, generic.DetailView):
|
||||||
|
"""
|
||||||
|
Provide a view for Result page, a Result for the poll contain poll participants
|
||||||
|
number and other statistic such as up, down vote
|
||||||
|
"""
|
||||||
model = Question
|
model = Question
|
||||||
template_name = "polls/results.html"
|
template_name = "polls/results.html"
|
||||||
|
|
||||||
@ -110,6 +114,9 @@ class ResultsView(LoginRequiredMixin, generic.DetailView):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
class SignUpView(generic.CreateView):
|
class SignUpView(generic.CreateView):
|
||||||
|
"""
|
||||||
|
View that responsible for Sign Up page.
|
||||||
|
"""
|
||||||
form_class = SignUpForm
|
form_class = SignUpForm
|
||||||
success_url = reverse_lazy('polls:index')
|
success_url = reverse_lazy('polls:index')
|
||||||
template_name = 'registration/signup.html'
|
template_name = 'registration/signup.html'
|
||||||
@ -167,6 +174,9 @@ def vote(request, question_id):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def up_down_vote(request, question_id, vote_type):
|
def up_down_vote(request, question_id, vote_type):
|
||||||
|
"""
|
||||||
|
A function that control the upvote and downvote request.
|
||||||
|
"""
|
||||||
ip = get_client_ip(request)
|
ip = get_client_ip(request)
|
||||||
question = get_object_or_404(Question, pk=question_id)
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
|
|
||||||
@ -183,6 +193,9 @@ def up_down_vote(request, question_id, vote_type):
|
|||||||
|
|
||||||
# https://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
|
# https://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
|
||||||
def get_client_ip(request):
|
def get_client_ip(request):
|
||||||
|
"""
|
||||||
|
Use with logger to get ip of user.
|
||||||
|
"""
|
||||||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||||
if x_forwarded_for:
|
if x_forwarded_for:
|
||||||
ip = x_forwarded_for.split(',')[0]
|
ip = x_forwarded_for.split(',')[0]
|
||||||
@ -190,7 +203,12 @@ def get_client_ip(request):
|
|||||||
ip = request.META.get('REMOTE_ADDR')
|
ip = request.META.get('REMOTE_ADDR')
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
def search_poll(request):
|
def search_poll(request):
|
||||||
|
"""
|
||||||
|
A function that handle the rendering of search result after user search with
|
||||||
|
search bar.
|
||||||
|
"""
|
||||||
form = PollSearchForm
|
form = PollSearchForm
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
@ -199,7 +217,9 @@ def search_poll(request):
|
|||||||
form = PollSearchForm(request.GET)
|
form = PollSearchForm(request.GET)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
q = form.cleaned_data['q']
|
q = form.cleaned_data['q']
|
||||||
|
# Case insensitive (icontains)
|
||||||
results = Question.objects.filter(question_text__icontains=q)
|
results = Question.objects.filter(question_text__icontains=q)
|
||||||
|
# * If user search with empty string then show every poll.
|
||||||
if q == '':
|
if q == '':
|
||||||
results = Question.objects.all()
|
results = Question.objects.all()
|
||||||
return render(request, 'polls/search.html', {'form':form, 'results':results, 'q':q})
|
return render(request, 'polls/search.html', {'form':form, 'results':results, 'q':q})
|
||||||
Loading…
Reference in New Issue
Block a user