Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Iss19 #25

Merged
merged 6 commits into from
This page is out of date. Refresh to see the latest.
View
40 articles/templates/article_list.html
@@ -45,12 +45,40 @@ <h1 class="page-header">{{ eventType }} Events <small>Article List</small></h1>
<hr>
- <div class="row">
-
- <ul class="pager">
- <li class="previous"><a href="?page=0">&larr; Older</a></li>
- <li class="next"><a href="?page=2">Newer &rarr;</a></li>
- </ul>
+ <div class="row text-center">
+ <div class="col-lg-12">
+ <ul class="pagination">
+ {% if prev_page == 0 %}
+
+ {% else %}
+ {% if article_count %}
+ <li><a href="?page={{ prev_page }}&count={{ article_count }}">&laquo;</a></li>
+ {% else %}
+ <li><a href="?page={{ prev_page }}">&laquo;</a></li>
+ {% endif %}
+ {% endif %}
+ {% for i in page_numbers %}
+ {% if page_number == i %}
+ <li class="active"><a href="#">{{ page_number }}</a></li>
+ {% else %}
+ {% if article_count %}
+ <li><a href="?page={{ i }}&count={{ article_count }}">{{ i }}</a></li>
+ {% else %}
+ <li><a href="?page={{ i }}">{{ i }}</a></li>
+ {% endif %}
+ {% endif %}
+ {% endfor %}
+ {% if next_page == 0 %}
+
+ {% else %}
+ {% if article_count %}
+ <li><a href="?page={{ next_page }}&count={{ article_count }}">&raquo;</a></li>
+ {% else %}
+ <li><a href="?page={{ next_page }}">&raquo;</a></li>
+ {% endif %}
+ {% endif %}
+ </ul>
+ </div>
</div>
View
58 articles/views.py
@@ -2,37 +2,55 @@
from django.http import HttpResponse
from django.template import Context, loader
+import math
from articles.models import Article, ArticleCategory, ArticleEntity, Gallery, InGallery, Picture
+from listing.pages import PageHelper
-def index(request):
+default_count_per_page = 5
+max_count_per_page = 25
+max_pages_listed_on_screen = 5
+
+def general_listing(request, eventType, category):
t = loader.get_template('article_list.html')
- article_list = Article.objects.all().order_by('date').reverse()
- if len(article_list) == 0:
- article_list = None
- c = Context({'eventType': 'All', 'article_list': article_list})
+ if category != None:
+ type_id = ArticleCategory.objects.filter(name=category)[0].id
+ article_list = Article.objects.filter(category=type_id).order_by('date').reverse()
+ else:
+ article_list = Article.objects.all().order_by('date').reverse()
+ article_count = PageHelper.get_request_count(request, default_count_per_page, max_count_per_page)
+ page_number = PageHelper.get_page_number(request)
+ articles_range_min = (page_number - 1) * article_count
+ articles_range_max = (page_number) * article_count
+ number_of_articles = len(article_list)
+ total_pages = int(math.ceil(number_of_articles / float(article_count)))
+ article_list = article_list[articles_range_min:articles_range_max]
+ page_numbers_list = PageHelper.calculate_page_range(total_pages, page_number, max_pages_listed_on_screen)
+ next_page = page_number + 1 if number_of_articles > articles_range_max else 0
+ prev_page = page_number - 1
+ context_dict = {
+ 'eventType': eventType,
+ 'article_list' : article_list,
+ 'article_count': article_count,
+ 'page_number' : page_number,
+ 'prev_page': prev_page,
+ 'next_page' : next_page,
+ 'page_numbers' : page_numbers_list
+ }
+ c = Context(context_dict)
return HttpResponse(t.render(c))
+
+def index(request):
+ return general_listing(request, 'All', None)
def service(request):
- type_id = ArticleCategory.objects.filter(name="Service")[0].id
- t = loader.get_template('article_list.html')
- article_list = Article.objects.filter(category=type_id)
- c = Context({'eventType': 'Service', 'article_list': article_list})
- return HttpResponse(t.render(c))
+ return general_listing(request, 'Service', 'Service')
def professional_development(request):
- type_id = ArticleCategory.objects.filter(name="PD")[0].id
- t = loader.get_template('article_list.html')
- article_list = Article.objects.filter(category=type_id)
- c = Context({'eventType': 'Professional Development', 'article_list': article_list})
- return HttpResponse(t.render(c))
+ return general_listing(request, 'Professional Development', 'PD')
def social(request):
- type_id = ArticleCategory.objects.filter(name="Social")[0].id
- t = loader.get_template('article_list.html')
- article_list = Article.objects.filter(category=type_id)
- c = Context({'eventType': 'Social', 'article_list': article_list})
- return HttpResponse(t.render(c))
+ return general_listing(request, 'Social', 'Social')
def get_article(request, article_id):
art = get_object_or_404(Article, pk=article_id)
View
59 info/views.py
@@ -8,6 +8,7 @@
from info import utility
from marketing.models import Picture as MarketingPic
from articles.models import Article
+from listing.pages import PageHelper
max_brothers_per_page = 24
standard_brothers_per_page = 9
@@ -50,8 +51,8 @@ def general_listing(request, isAlumniFilter, isPledgeFilter, name):
Retrieves all of the information necessary for each of the brother listings.
Retrieves information based on the isAlumniFilter and isPledgeFilter
'''
- brothers_count = get_brother_count(request)
- page_number = get_page_number(request)
+ brothers_count = PageHelper.get_request_count(request, standard_brothers_per_page, max_brothers_per_page)
+ page_number = PageHelper.get_page_number(request)
brothers_range_min = (page_number - 1) * brothers_count
brothers_range_max = (page_number) * brothers_count
brothers = Brother.objects.filter(isAlumni=isAlumniFilter, isPledge=isPledgeFilter).order_by(
@@ -61,7 +62,7 @@ def general_listing(request, isAlumniFilter, isPledgeFilter, name):
brothers = brothers[brothers_range_min:brothers_range_max]
brothers = convert_brothers_to_brotherentities(brothers)
brother_list_list = utility.convert_array_to_YxZ(brothers, brothers_per_row) if len(brothers) > 0 else None
- page_numbers_list = calculate_page_range(total_pages, page_number)
+ page_numbers_list = PageHelper.calculate_page_range(total_pages, page_number, max_pages_listed_on_screen)
next_page = page_number + 1 if number_of_brothers > brothers_range_max else 0
prev_page = page_number - 1
context_dict = {
@@ -87,55 +88,3 @@ def convert_brothers_to_brotherentities(broList):
for bro in broList:
broEList.append(BrotherEntity(bro))
return broEList
-
-def get_brother_count(request):
- '''
- Finds the requested number of brothers and corrects it if there are any issues
- If the number is invalid, it will return standard_brothers_per_page
- '''
- brothers_count = request.GET.get('count',str(standard_brothers_per_page))
- try:
- brothers_count = int(brothers_count)
- if brothers_count > max_brothers_per_page:
- brothers_count = max_brothers_per_page
- except:
- brothers_count = standard_brothers_per_page
- return brothers_count
-
-def get_page_number(request):
- '''
- Finds the page number and corrects it if there are any issues
- If the page number is invalid, it will return 1
- '''
- page_number = request.GET.get('page','1')
- try:
- page_number = int(page_number)
- if page_number < 1:
- page_number = 1
- except:
- page_number = 1
- return page_number
-
-def calculate_page_range(total_pages, page_number):
- '''
- This determines which page numbers to show at the bottom of the brothers list pages.
- It returns a list of integers that should be displayed on the page based on the total
- number of pages and the current page number.
- '''
- if total_pages == 1: # If there is only the one page, there is no need to display page numbers
- return []
- elif total_pages <= max_pages_listed_on_screen: # In this case, just display all of the available pages
- min_page_number_displayed = 1
- max_page_number_displayed = total_pages + 1
- elif page_number - max_pages_listed_on_screen / 2 <= 1: # We are near the beginning. In this case, display from page 1 to max_pages_listed_on_screen
- min_page_number_displayed = 1
- max_page_number_displayed = min_page_number_displayed + max_pages_listed_on_screen
- elif page_number + max_pages_listed_on_screen / 2 >= total_pages: # We are near the end. In this case, display from (end - max_pages_listed_on_screen) to end
- max_page_number_displayed = total_pages + 1
- min_page_number_displayed = max_page_number_displayed - max_pages_listed_on_screen
- else: # We are somewhere in the middle. In this case, just display some pages on either side
- min_page_number_displayed = page_number - max_pages_listed_on_screen / 2
- max_page_number_displayed = min_page_number_displayed + max_pages_listed_on_screen
-
- page_numbers_list = range(min_page_number_displayed,max_page_number_displayed)
- return page_numbers_list
View
0  listing/__init__.py
No changes.
View
62 listing/pages.py
@@ -0,0 +1,62 @@
+'''
+Created on Mar 4, 2014
+
+@author: kylerogers
+'''
+
+class PageHelper:
+
+ @staticmethod
+ def get_request_count(request, default_count_per_page, max_count_per_page):
+ '''
+ Finds the requested number of brothers and corrects it if there are any issues
+ If the number is invalid, it will return standard_brothers_per_page
+ '''
+ brothers_count = request.GET.get('count',str(default_count_per_page))
+ try:
+ brothers_count = int(brothers_count)
+ if brothers_count > max_count_per_page:
+ brothers_count = max_count_per_page
+ except:
+ brothers_count = default_count_per_page
+ return brothers_count
+
+ @staticmethod
+ def get_page_number(request):
+ '''
+ Finds the page number and corrects it if there are any issues
+ If the page number is invalid, it will return 1
+ '''
+ page_number = request.GET.get('page','1')
+ try:
+ page_number = int(page_number)
+ if page_number < 1:
+ page_number = 1
+ except:
+ page_number = 1
+ return page_number
+
+ @staticmethod
+ def calculate_page_range(total_pages, page_number, max_pages_listed_on_screen):
+ '''
+ This determines which page numbers to show at the bottom of the brothers list pages.
+ It returns a list of integers that should be displayed on the page based on the total
+ number of pages and the current page number.
+ '''
+ if total_pages == 1: # If there is only the one page, there is no need to display page numbers
+ return []
+ elif total_pages <= max_pages_listed_on_screen: # In this case, just display all of the available pages
+ min_page_number_displayed = 1
+ max_page_number_displayed = total_pages + 1
+ elif page_number - max_pages_listed_on_screen / 2 <= 1: # We are near the beginning. In this case, display from page 1 to max_pages_listed_on_screen
+ min_page_number_displayed = 1
+ max_page_number_displayed = min_page_number_displayed + max_pages_listed_on_screen
+ elif page_number + max_pages_listed_on_screen / 2 >= total_pages: # We are near the end. In this case, display from (end - max_pages_listed_on_screen) to end
+ max_page_number_displayed = total_pages + 1
+ min_page_number_displayed = max_page_number_displayed - max_pages_listed_on_screen
+ else: # We are somewhere in the middle. In this case, just display some pages on either side
+ min_page_number_displayed = page_number - max_pages_listed_on_screen / 2
+ max_page_number_displayed = min_page_number_displayed + max_pages_listed_on_screen
+
+ page_numbers_list = range(min_page_number_displayed,max_page_number_displayed)
+ return page_numbers_list
Something went wrong with that request. Please try again.