Skip to content

Commit

Permalink
Merge pull request #1261 from zestedesavoir/fix-versionning
Browse files Browse the repository at this point in the history
Correction du versionning a l'affichage
  • Loading branch information
dralliw committed Jul 23, 2014
2 parents 35e8888 + e14b6dc commit 6f65338
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 166 deletions.
2 changes: 1 addition & 1 deletion templates/tutorial/member/online.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2 class="ico-after ico-tutorials">Tutoriels publiés par {{ usr.username }}</h
{% if tutorials %}
<div class="tutorial-list">
{% for tutorial in tutorials %}
{% include 'tutorial/includes/tutorial_item.part.html' with beta=True %}
{% include 'tutorial/includes/tutorial_item.part.html' %}
{% endfor %}
</div>
{% else %}
Expand Down
31 changes: 26 additions & 5 deletions zds/article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import models
from math import ceil
from git import Repo
import os
import string
import uuid
Expand All @@ -25,15 +26,11 @@

from zds.utils import get_current_user
from zds.utils import slugify
from zds.utils.articles import export_article
from zds.utils.articles import export_article, get_blob
from zds.utils.models import SubCategory, Comment, Licence
from django.core.urlresolvers import reverse


IMAGE_MAX_WIDTH = 480
IMAGE_MAX_HEIGHT = 100


def image_path(instance, filename):
"""Return path to an image."""
ext = filename.split('.')[-1]
Expand Down Expand Up @@ -141,6 +138,30 @@ def load_json(self, path=None, online=False):
else:
return None

def load_json_for_public(self):
repo = Repo(self.get_path())
manarticle = get_blob(repo.commit(self.sha_public).tree, 'manifest.json')
data = json_reader.loads(manarticle)

return data

def load_dic(self, article_version):
article_version['pk'] = self.pk
article_version['slug'] = slugify(article_version['title'])
article_version['image'] = self.image
article_version['pubdate'] = self.pubdate
article_version['is_locked'] = self.is_locked
article_version['sha_draft'] = self.sha_draft
article_version['sha_validation'] = self.sha_validation
article_version['sha_public'] = self.sha_public
article_version['get_reaction_count'] = self.get_reaction_count
article_version['get_absolute_url'] = reverse('zds.article.views.view',
args=[self.pk, self.slug])
article_version['get_absolute_url_online'] = reverse('zds.article.views.view_online',
args=[self.pk,slugify(article_version['title'])])

return article_version

def dump_json(self, path=None):
if path is None:
man_path = os.path.join(self.get_path(), 'manifest.json')
Expand Down
2 changes: 1 addition & 1 deletion zds/article/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
url(r'^nouveau/$', 'zds.article.views.new'),
url(r'^editer/$', 'zds.article.views.edit'),
url(r'^modifier/$', 'zds.article.views.modify'),
url(r'^recherche/(?P<name>\d+)/$',
url(r'^recherche/(?P<pk_user>\d+)/$',
'zds.article.views.find_article'),


Expand Down
66 changes: 26 additions & 40 deletions zds/article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,26 @@ def index(request):
tag = None

if tag is None:
article = Article.objects\
articles = Article.objects\
.filter(sha_public__isnull=False).exclude(sha_public="")\
.order_by('-pubdate')\
.all()
else:
# The tag isn't None and exist in the system. We can use it to retrieve
# all articles in the subcategory specified.
article = Article.objects\
articles = Article.objects\
.filter(sha_public__isnull=False, subcategory__in=[tag])\
.exclude(sha_public="").order_by('-pubdate')\
.all()

article_versions = []
for article in articles:
article_version = article.load_json_for_public()
article_version = article.load_dic(article_version)
article_versions.append(article_version)

return render_template('article/index.html', {
'articles': article,
'articles': article_versions,
'tag': tag,
})

Expand All @@ -83,10 +89,6 @@ def view(request, article_pk, article_slug):
if not request.user.has_perm('article.change_article'):
raise PermissionDenied

# The slug of the article must to be right.
if article_slug != slugify(article.title):
return redirect(article.get_absolute_url())

# Retrieve sha given by the user. This sha must to be exist.
# If it doesn't exist, we take draft version of the article.
try:
Expand All @@ -105,17 +107,8 @@ def view(request, article_pk, article_slug):
manifest = get_blob(repo.commit(sha).tree, 'manifest.json')

article_version = json_reader.loads(manifest)
article_version['txt'] = get_blob(
repo.commit(sha).tree,
article_version['text'])
article_version['pk'] = article.pk
article_version['slug'] = article.slug
article_version['image'] = article.image
article_version['pubdate'] = article.pubdate
article_version['sha_draft'] = article.sha_draft
article_version['sha_validation'] = article.sha_validation
article_version['sha_public'] = article.sha_public
article_version['get_absolute_url_online'] = article.get_absolute_url_online()
article_version['txt'] = get_blob(repo.commit(sha).tree, article_version['text'])
article_version = article.load_dic(article_version)

validation = Validation.objects.filter(article__pk=article.pk,
version=sha)\
Expand All @@ -135,28 +128,14 @@ def view_online(request, article_pk, article_slug):
"""Show the given article if exists and is visible."""
article = get_object_or_404(Article, pk=article_pk)

# The slug of the article must to be right.
if article_slug != slugify(article.title):
return redirect(article.get_absolute_url_online())

# Load the article.
article_version = article.load_json()
txt = open(
os.path.join(
article.get_path(),
article_version['text'] +
'.html'),
"r")
article_version = article.load_json_for_public()
txt = open(os.path.join(article.get_path(),
article_version['text'] + '.html'),
"r")
article_version['txt'] = txt.read()
txt.close()
article_version['pk'] = article.pk
article_version['slug'] = article.slug
article_version['image'] = article.image
article_version['pubdate'] = article.pubdate
article_version['is_locked'] = article.is_locked
article_version['get_reaction_count'] = article.get_reaction_count
article_version['get_absolute_url'] = article.get_absolute_url()
article_version['get_absolute_url_online'] = article.get_absolute_url_online()
article_version = article.load_dic(article_version)

# If the user is authenticated
if request.user.is_authenticated():
Expand Down Expand Up @@ -364,16 +343,23 @@ def edit(request):
})


def find_article(request, name):
def find_article(request, pk_user):
"""Find an article from his author."""
user = get_object_or_404(User, pk=name)
user = get_object_or_404(User, pk=pk_user)
articles = Article.objects\
.filter(authors__in=[user], sha_public__isnull=False).exclude(sha_public="")\
.order_by('-pubdate')\
.all()

article_versions = []
for article in articles:
article_version = article.load_json_for_public()
article_version = article.load_dic(article_version)
article_versions.append(article_version)

# Paginator
return render_template('article/find.html', {
'articles': articles, 'usr': user,
'articles': article_versions, 'usr': user,
})


Expand Down
24 changes: 18 additions & 6 deletions zds/member/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,30 @@ def details(request, user_name):
fchart = os.path.join(img_path, "mod-{}.svg".format(str(usr.pk)))
dot_chart.render_to_file(fchart)
my_articles = Article.objects.filter(sha_public__isnull=False).order_by(
"-pubdate").filter(authors__in=[usr]).all()
"-pubdate").filter(authors__in=[usr]).all()[:5]
my_tutorials = \
Tutorial.objects.filter(sha_public__isnull=False) \
.filter(authors__in=[usr]) \
.order_by("-pubdate"
).all()
).all()[:5]

my_tuto_versions = []
for my_tutorial in my_tutorials:
mandata = my_tutorial.load_json_for_public()
mandata = my_tutorial.load_dic(mandata)
my_tuto_versions.append(mandata)
my_article_versions = []
for my_article in my_articles:
article_version = my_article.load_json_for_public()
article_version = my_article.load_dic(article_version)
my_article_versions.append(article_version)

my_topics = \
Topic.objects\
.filter(author=usr)\
.exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=request.user.groups.all()))\
.prefetch_related("author")\
.order_by("-pubdate").all()
.order_by("-pubdate").all()[:5]

form = OldTutoForm(profile)
oldtutos = []
Expand All @@ -142,9 +154,9 @@ def details(request, user_name):
"usr": usr,
"profile": profile,
"bans": bans,
"articles": my_articles[:5],
"tutorials": my_tutorials[:5],
"topics": my_topics[:5],
"articles": my_article_versions,
"tutorials": my_tuto_versions,
"topics": my_topics,
"form": form,
"old_tutos": oldtutos,
})
Expand Down
23 changes: 8 additions & 15 deletions zds/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,14 @@ def home(request):
tutos = []
for tuto in get_last_tutorials():
data = tuto.load_json_for_public()
data['pk'] = tuto.pk
data['image'] = tuto.image
data['gallery'] = tuto.gallery
data['pubdate'] = tuto.pubdate
data['update'] = tuto.update
data['subcategory'] = tuto.subcategory
data['get_absolute_url_online'] = reverse(
'zds.tutorial.views.view_tutorial_online',
args=[
tuto.pk,
slugify(
data['title'])])

data = tuto.load_dic(data)
tutos.append(data)

articles = []
for article in get_last_articles():
data = article.load_json_for_public()
data = article.load_dic(data)
articles.append(data)

try:
with open(os.path.join(SITE_ROOT, 'quotes.txt'), 'r') as fh:
Expand All @@ -51,9 +45,8 @@ def home(request):
quote = u'Zeste de Savoir, la connaissance pour tous et sans pépins !'

return render_template('home.html', {
'last_topics': get_last_topics(request.user),
'last_tutorials': tutos,
'last_articles': get_last_articles(),
'last_articles': articles,
'quote': quote,
})

Expand Down
Loading

0 comments on commit 6f65338

Please sign in to comment.