Commit cbb9f8b9 authored by PkSM3's avatar PkSM3

[UPDATE] paginator progress: client and server (install django_dajax)

parent ee94ae16
...@@ -46,6 +46,8 @@ urlpatterns = patterns('', ...@@ -46,6 +46,8 @@ urlpatterns = patterns('',
url(r'^api/nodes/(\d+)$', gargantext_web.api.Nodes.as_view()), url(r'^api/nodes/(\d+)$', gargantext_web.api.Nodes.as_view()),
url(r'^api/nodes$', gargantext_web.api.NodesList.as_view()), url(r'^api/nodes$', gargantext_web.api.NodesList.as_view()),
url(r'^api/project/(\d+)/corpus/(\d+)/timerange/(\d+)/(\d+)$', views.subcorpusJSON),
url(r'^api/nodes/(\d+)/ngrams$', gargantext_web.api.CorpusController.ngrams), url(r'^api/nodes/(\d+)/ngrams$', gargantext_web.api.CorpusController.ngrams),
url(r'^graph-it$', views.graph_it), url(r'^graph-it$', views.graph_it),
......
...@@ -434,6 +434,75 @@ def subcorpus(request, project_id, corpus_id, start , end ): ...@@ -434,6 +434,75 @@ def subcorpus(request, project_id, corpus_id, start , end ):
return HttpResponse(html) return HttpResponse(html)
import json
def subcorpusJSON(request, project_id, corpus_id, start , end ):
if not request.user.is_authenticated():
return redirect('/login/?next=%s' % request.path)
try:
offset = str(project_id)
offset = str(corpus_id)
offset = str(start)
offset = str(end)
except ValueError:
raise Http404()
# parameters received via web. Format = (yearmonthday = 20150106 = 06 jan 2015)
import datetime
dateini = datetime.datetime.strptime(str(start), '%Y%m%d').date()
datefin = datetime.datetime.strptime(str(end), '%Y%m%d').date()
t = get_template('subcorpus.html')
user = request.user
date = datetime.datetime.now()
project = Node.objects.get(id=project_id)
corpus = Node.objects.get(id=corpus_id)
# retrieving all the documents
documents = corpus.children.all()
number = corpus.children.count()
filtered_docs = []
# filtering documents by range-date
for doc in documents:
if "publication_date" in doc.metadata:
realdate = doc.metadata["publication_date"].split(" ")[0] # in database is = (year-month-day = 2015-01-06 00:00:00 = 06 jan 2015 00 hrs)
realdate = datetime.datetime.strptime(str(realdate), '%Y-%m-%d').date() # finalform = (yearmonthday = 20150106 = 06 jan 2015)
if dateini <= realdate <= datefin:
doc.date = realdate
filtered_docs.append(doc)
# ordering from most recent to the older.
ordered = sorted(filtered_docs, key=lambda x: x.date, reverse=True)
# pages of 10 elements. Like a sir.
paginator = Paginator(ordered, 10)
page = request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
results = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
results = paginator.page(paginator.num_pages)
from rest_framework.pagination import PaginationSerializer
serializer = PaginationSerializer(instance=results)
print(serializer.data)
html = t.render(Context({\
'user': user,\
'date': date,\
'corpus': corpus,\
}))
# return HttpResponse(html)
return HttpResponse( serializer.data , content_type='application/json')
......
This diff is collapsed.
{% if date %}
<p>Today: {{date}}</p>
{% endif %}
{% if documents %}
<p>Paginator stuff</p>
<ul>
{% for doc in documents %}
{% if doc.date %}
<li> <b>{{ doc.date }}</b>, <a href="/admin/node/document/{{doc.id}}">id:{{ doc.id}} title:{{ doc.name}}</a></li>
{% endif %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if documents.has_previous %}
<a href="?page={{ documents.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
{% if documents.has_next %}
<a href="?page={{ documents.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endif %}
\ 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