# HG changeset patch # User Nicolas DURAND # Date 1424454172 -3600 # Node ID 5bd3fb0233965e5b81d2d37b376ad5659d3a87ce # Parent 1506da593f40a0392325871cb3f526a08fd7da4a Added pagination to commit list and issue list + changeset page now display categories before and after modifications diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/__init__.py --- a/src/catedit/__init__.py Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/__init__.py Fri Feb 20 18:42:52 2015 +0100 @@ -85,6 +85,7 @@ """ session["pagination_links"] = {} log_api_rate(r, *args, **kwargs) + print("link header: "+r.headers.get("link", "none")) if r.headers.get("link", None) is not None: session["pagination_links"] = r.links for (key, item) in session["pagination_links"].items(): @@ -92,32 +93,33 @@ item["url"] = resource if session["pagination_links"].get("next", None) is not None: page_arg = re.search( - "(\?|&)page=\d", + "(\?|&)page=(\d+)", string=session["pagination_links"]["next"]["url"] ) session["pagination_links"]["current_page"] = int( - page_arg.group(0)[-1] + page_arg.group(2) )-1 if session["pagination_links"].get("last", None) is not None: last_page_arg = re.search( - "(\?|&)page=\d", + "(\?|&)page=(\d+)", string=session["pagination_links"]["last"]["url"] ) session["pagination_links"]["last_page"] = int( - page_arg.group(0)[-1] + last_page_arg.group(2) ) else: # We don't know what is the last page (case: github commits # API) - session["pagination_links"]["last_page"] = -1 + session["pagination_links"]["last_page"] = \ + session["pagination_links"]["current_page"] + 1 elif session["pagination_links"].get("prev", None) is not None: # This means we're at the last page page_arg = re.search( - "(\?|&)page=\d", + "(\?|&)page=(\d+)", string=session["pagination_links"]["prev"]["url"] ) session["pagination_links"]["current_page"] = int( - page_arg.group(0)[-1] + page_arg.group(2) )+1 session["pagination_links"] \ ["last_page"] = session["pagination_links"]["current_page"] @@ -151,15 +153,20 @@ endpoint='category_changes') # Pagination utility functions for templates -def url_for_other_page(page): +def url_for_other_page(page, arg_name="page"): args = request.view_args.copy() - args['page'] = page + args[arg_name] = page return url_for(request.endpoint, **args) app.jinja_env.globals['url_for_other_page'] = url_for_other_page -def url_for_other_per_page(per_page): +def url_for_other_per_page( + per_page, + page_arg_name="page", + per_page_arg_name="per_page" +): args = request.view_args.copy() - args['per_page'] = per_page + args[page_arg_name] = 1 + args[per_page_arg_name] = per_page return url_for(request.endpoint, **args) app.jinja_env.globals['url_for_other_per_page'] = url_for_other_per_page diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/static/css/style.css --- a/src/catedit/static/css/style.css Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/static/css/style.css Fri Feb 20 18:42:52 2015 +0100 @@ -50,6 +50,15 @@ color: white !important; } +p.text-justified{ + text-align: justify; +} + +.changeset-menu +{ + display: block; +} + .footer-notes { padding-right: 40px; diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/templates/macros.html --- a/src/catedit/templates/macros.html Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/templates/macros.html Fri Feb 20 18:42:52 2015 +0100 @@ -103,16 +103,16 @@ {% endfor %} {%- endmacro %} -{% macro render_pagination(pagination) -%} +{% macro render_pagination(pagination, per_page, page_arg_name="page", per_page_arg_name="per_page") -%} {% if pagination %} -
- - -
{% endif %} +
+ + +
{%- endmacro %} diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/templates/social/changeset.html --- a/src/catedit/templates/social/changeset.html Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/templates/social/changeset.html Fri Feb 20 18:42:52 2015 +0100 @@ -11,17 +11,77 @@ @@ -32,17 +92,44 @@
  • Discussion: changements
  • {% endblock navbar_items %} {% block additional_content %} -

    Etat de l'ensemble des catégories pour ces modifications - - - -

    -
    +
    +
    +

    Avant modifications + + +

    +

    + Permet de visualiser l'état de l'ensemble de catégories {{current_repository}} avant que ne soient pris en compte + les changements considérés. +

    +
    +
    +

    Résumé des modifications + + +

    +

    + Permet de visualiser les différences entre l'ensemble de catégories {{current_repository}} avant les modifications considérées + et le même ensemble après les modifications +

    +
    +
    +

    Après modifications + + +

    +

    + Permet de visualiser l'état de l'ensemble de catégories {{current_repository}} après prise en compte + les changements considérés. +

    +
    +

    +
    - - + + @@ -55,13 +142,52 @@ {% else %} - {% if cat_list|length == 0 %} + {% if old_cat_list|length == 0 %} - + {% else %} {% import "macros.html" as macros %} - {{ macros.category_table(cat_list, current_repository, state_list=["original"], interactive=False) }} + {{ macros.category_table(old_cat_list, current_repository, state_list=["original"], interactive=False) }} + {% endif %} + {% endif %} + +
    Nom de la catégorieDescription de la catégorieNomDescription
    Aucune catégorie n'existait suite à ces changements. {% if not readonly %}Créer une catégorie{% endif %}Aucune catégorie n'existait avant ces changements. {% if not readonly %}Créer une catégorie{% endif %}
    +
    +
    + + + + +
    + placeholder +
    +
    +
    + + + + + + + + + {% if not session["user_logged"] %} + + + + {% else %} + {% if new_cat_list|length == 0 %} + + + + {% else %} + {% import "macros.html" as macros %} + {{ macros.category_table(new_cat_list, current_repository, state_list=["modified"], interactive=False) }} {% endif %} {% endif %} diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/templates/social/comment_thread_layout.html --- a/src/catedit/templates/social/comment_thread_layout.html Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/templates/social/comment_thread_layout.html Fri Feb 20 18:42:52 2015 +0100 @@ -14,68 +14,69 @@ {% endblock navbar_items %} {% block page_content %}

    CatEdit - {{current_repository}}

    -{% block additional_content %} -{% endblock additional_content %} -

    Discussion

    - -{% if comment_form.comment_field.errors %} -
    - - - Erreur: - - Votre commentaire est vide. -
    -{% endif %} -
    NomDescription
    + + Attention: + Veuillez vous identifier pour visualiser les catégories +
    Aucune catégorie n'existe suite à ces changements. {% if not readonly %}Créer une catégorie{% endif %}
    - - - - - +
    + {% block additional_content %} + {% endblock additional_content %} +

    Discussion

    + {% if comment_form.comment_field.errors %} +
    + + + Erreur: + + Votre commentaire est vide. +
    + {% endif %} +
    {{comments["author"]}} - {{comments["opening_date"]}} - Titre: {{comments["title"]}} -
    - {{comments["opening_post"]}}
    + + + - - - {% if comments["comment_list"]|length == 0 %} - - - - {% else %} - {% for comment in comments["comment_list"] %} + + + + + + {% if comments["comment_list"]|length == 0 %} - - - + - {% endfor %} - {% endif %} - - - - - - - - -
    {{comments["author"]}} + {{comments["opening_date"]}} + Titre: {{comments["title"]}}
    Aucun commentaire à afficher
    + {{comments["opening_post"]}}
    {{comment["author"]}} {{comment["date"]}} {{comment["body"]}} Aucun commentaire à afficher
    - {% import "macros.html" as macros %} - {{ macros.render_pagination(pagination) }} -
    - {{ comment_form.comment_field.label }} - -
    -
    - {{ comment_form.hidden_tag() }} -
    - {{ comment_form.comment_field(class="form-control", readonly=readonly) }} -
    - - Retour -
    -
    -
    + {% else %} + {% for comment in comments["comment_list"] %} + + {{comment["author"]}} + {{comment["date"]}} + {{comment["body"]}} + + {% endfor %} + {% endif %} + + + {% import "macros.html" as macros %} + {{ macros.render_pagination(comments_pagination, comments_per_page) }} + + + + + {{ comment_form.comment_field.label }} + + +
    +
    + {{ comment_form.hidden_tag() }} +
    + {{ comment_form.comment_field(class="form-control", readonly=readonly) }} +
    + + Retour +
    +
    + + + + +
    {% endblock page_content %} diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/templates/social/index.html --- a/src/catedit/templates/social/index.html Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/templates/social/index.html Fri Feb 20 18:42:52 2015 +0100 @@ -40,19 +40,33 @@ - {% for changeset in changeset_list %} + {% if not changeset_list %} - {{changeset["author"]}} - {{changeset["date"]}} - {{changeset["title"]}} - {{changeset["comment_count"]}} - - - + + Il n'y a aucune modification à afficher. - {% endfor %} + {% else %} + {% for changeset in changeset_list %} + + {{changeset["author"]}} + {{changeset["date"]}} + {{changeset["title"]}} + {{changeset["comment_count"]}} + + + + + + {% endfor %} + {% endif %} + + + {% import "macros.html" as macros %} + {{ macros.render_pagination(commits_pagination, commits_per_page, page_arg_name="commits_page", per_page_arg_name="commits_per_page") }} + +

    Discussions générales

    @@ -74,8 +88,8 @@ {% if not discussion_list %} - - Il n'y a pas encore de discussion pour cette ensemble de catégories. Créer une discussion + + Il n'y a de discussion à afficher pour cette ensemble de catégories. Créer une discussion {% else %} @@ -93,6 +107,11 @@ {% endfor %} + + + {{ macros.render_pagination(discussions_pagination, discussions_per_page, page_arg_name="discussions_page", per_page_arg_name="discussions_per_page") }} + + {% endif %} diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/views/social.py --- a/src/catedit/views/social.py Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/views/social.py Fri Feb 20 18:42:52 2015 +0100 @@ -18,26 +18,91 @@ logger = app.logger -@module.route("//social", - methods=["GET"]) -def index(repository): +@module.route( + "//social", + methods=["GET"], + defaults={ + "commits_page": 1, + "commits_per_page": 10, + "discussions_page": 1, + "discussions_per_page": 10 + } +) +@module.route( + "//social_" + + "commits_page_perpage", + methods=["GET"], + defaults={ + "discussions_page": 1, + "discussions_per_page": 10 + } +) +@module.route( + "//social_" + + "discussions_page" + + "_perpage", + methods=["GET"], + defaults={ + "commits_page": 1, + "commits_per_page": 10, + } +) +@module.route( + "//social_" + + "commits_page_perpage-" + + "discussions_page" + + "_perpage", + methods=["GET"] +) +def index(repository, commits_page, commits_per_page, + discussions_page, discussions_per_page): """ View that displays every changeset and thread in general discussion and links to every thread """ - if repository not in app.config["PERSISTENCE_CONFIG"]["REPOSITORY_LIST"]: - abort(404) - check_user_status_and_repo_access(repository) - changeset_list = get_commits(repository) - discussion_list = get_issues(repository) + changeset_list = get_commits( + repository, + commits_per_page, + commits_page + ) + if (changeset_list == [] and commits_page != 1 + and session.get("pagination_links", None) is None): + abort(404) + commits_pagination = None + if session.get("pagination_links", None) is not None: + commits_pagination = Pagination( + page=session["pagination_links"]["current_page"], + per_page=commits_per_page, + last_page=session["pagination_links"]["last_page"] + ) + session.pop("pagination_links", None) + discussion_list = get_issues( + repository, + discussions_per_page, + discussions_page + ) + if discussion_list == [] and discussions_page != 1: + abort(404) + discussions_pagination = None + if session.get("pagination_links", None) is not None: + discussions_pagination = Pagination( + page=session["pagination_links"]["current_page"], + per_page=discussions_per_page, + last_page=session["pagination_links"]["last_page"] + ) + session.pop("pagination_links", None) return render_template( "social/index.html", current_repository=repository, discussion_list=discussion_list, - changeset_list=changeset_list + changeset_list=changeset_list, + commits_pagination = commits_pagination, + commits_per_page = commits_per_page, + discussions_pagination = discussions_pagination, + discussions_per_page = discussions_per_page ) @@ -73,10 +138,8 @@ --> Will probably be incorporated into a function later on """ - if repository not in app.config["PERSISTENCE_CONFIG"]["REPOSITORY_LIST"]: - abort(404) + check_user_status_and_repo_access(repository) - check_user_status_and_repo_access(repository) comment_form = CommentForm() comments_list = get_comments( repository=repository, @@ -85,6 +148,8 @@ page=page, per_page=per_page ) + if comments_list == [] and page != 1: + abort(404) pagination=None if session.get("pagination_links", None) is not None: # If there are multiple pages we create a pagination class that @@ -94,17 +159,45 @@ per_page=per_page, last_page=session["pagination_links"]["last_page"] ) - cat_list = get_category_list_for_commit(repository, changeset_id) - + session.pop("pagination_links", None) + rdf_old_cat_list = get_category_list_for_commit( + repository, + changeset_id + ) + rdf_new_cat_list = get_category_list_for_commit( + repository, + changeset_id, + get_parent=True + ) + old_cat_list = [] + for category in rdf_old_cat_list: + old_cat_list.append({ + "cat_label": category.label, + "cat_description": category.description, + "cat_id": category.cat_id, + "cat_properties": category.properties, + "state": "original" + }) + new_cat_list = [] + for category in rdf_new_cat_list: + new_cat_list.append({ + "cat_label": category.label, + "cat_description": category.description, + "cat_id": category.cat_id, + "cat_properties": category.properties, + "state": "modified" + }) if request.method == "GET": return render_template( "social/changeset.html", - cat_list=cat_list, + old_cat_list=old_cat_list, + new_cat_list=new_cat_list, comments=comments_list, changeset_id=changeset_id, comment_form=comment_form, current_repository=repository, - pagination=pagination + comments_pagination=pagination, + comments_per_page=per_page ) elif request.method == "POST" and comment_form.validate_on_submit(): return_id = post_comment( @@ -128,7 +221,8 @@ changeset_id=changeset_id, comment_form=comment_form, current_repository=repository, - pagination=pagination + comments_pagination=pagination, + comments_per_page=per_page ) @@ -161,7 +255,6 @@ to post comments on it. """ check_user_status_and_repo_access(repository) - comment_form = None pagination = None if discussion_id == "new": @@ -176,14 +269,17 @@ page=page, per_page=per_page ) + if comments_list == [] and page != 1: + abort(404) if session.get("pagination_links", None) is not None: # If there are multiple pages we create a pagination class that # will be sent to the template - pagination = Pagination( + comments_pagination = Pagination( page=session["pagination_links"]["current_page"], per_page=per_page, last_page=session["pagination_links"]["last_page"] ) + session.pop("pagination_links", None) if request.method == "GET": return render_template( @@ -192,7 +288,8 @@ comment_form=comment_form, current_repository=repository, discussion_id=discussion_id, - pagination=pagination + comments_pagination=pagination, + comments_per_page=per_page ) elif request.method == "POST" and comment_form.validate_on_submit(): if discussion_id == "new": @@ -225,5 +322,6 @@ comment_form=comment_form, current_repository=repository, discussion_id=discussion_id, - pagination=pagination + comments_pagination=pagination, + comments_per_page=per_page ) diff -r 1506da593f40 -r 5bd3fb023396 src/catedit/views/utils.py --- a/src/catedit/views/utils.py Fri Feb 20 10:55:54 2015 +0100 +++ b/src/catedit/views/utils.py Fri Feb 20 18:42:52 2015 +0100 @@ -34,32 +34,20 @@ @property def has_next(self): - if self.last_page != -1: - return self.page < self.pages - else: - return True + return self.page < self.pages def iter_pages(self, left_edge=2, left_current=2, right_current=5, right_edge=2): last = 0 - if self.last_page != -1: - for num in range(1, self.pages+1): - if num <= left_edge or \ - (num > self.page - left_current - 1 and \ - num < self.page + right_current) or \ - num > self.pages+1 - right_edge: - if last + 1 != num: - yield None - yield num - last = num - else: - for num in range(1, self.page+2): - if num <= left_edge or \ - num > self.page - left_current - 1: - if last + 1 != num: - yield None - yield num - last = num + for num in range(1, self.pages+1): + if num <= left_edge or \ + (num > self.page - left_current - 1 and \ + num < self.page + right_current) or \ + num > self.pages+1 - right_edge: + if last + 1 != num: + yield None + yield num + last = num def check_user_status_and_repo_access(repository): @@ -94,15 +82,6 @@ """ github_comments_data = [] - if thread_type == "commits": - commits_list = get_commits(repository) - if thread_id not in [commit["id"] for commit in commits_list]: - abort(404) - elif thread_type == "issues": - issues_list = get_issues(repository) - if thread_id not in [issue["id"] for issue in issues_list]: - abort(404) - try: github_comments_data = github.get( "repos/" @@ -266,8 +245,7 @@ return return_id -@cache.memoize(timeout=3600) -def get_commits(repository): +def get_commits(repository, per_page, page): """ Fuction that get the list of commits for a given repository. Returns a list of dict with the format: @@ -284,7 +262,7 @@ "repos/" + app.config["PERSISTENCE_CONFIG"]["REPOSITORY_OWNER"] + "/" + repository - + "/commits?per_page=5", + + "/commits?per_page=" + str(per_page) + "&page=" + str(page), hooks=dict(response=save_links) ) except GitHubError as ghe: @@ -306,8 +284,7 @@ return changeset_list -@cache.memoize(timeout=3600) -def get_issues(repository): +def get_issues(repository, per_page=30, page=1): """ Fuction that get the list of issues for a given repository. Returns a list of dict with the format: @@ -326,7 +303,8 @@ "repos/" + app.config["PERSISTENCE_CONFIG"]["REPOSITORY_OWNER"] + "/" + repository - + "/issues?per_page=100" + + "/issues?per_page=" + str(per_page) + "&page=" + str(page), + hooks=dict(response=save_links) ) except GitHubError as ghe: logger.error("Error getting issues for repo " + repository) @@ -343,20 +321,14 @@ } for issue in issues_data ] - - return discussion_list -@cache.memoize(timeout=3600) -def get_category_list_for_commit(repository, changeset_id): +def get_category_list_for_commit(repository, changeset_id, get_parent=False): """ Get the category list as it was following the changeset of id changeset_id """ - commits_list = get_commits(repository) - if changeset_id not in [commit["id"] for commit in commits_list]: - abort(404) # First step commit_data = {} @@ -371,7 +343,20 @@ logger.error("Error trying to get the commit of id " + changeset_id) logger.error(ghe.response.text) - + if get_parent: + parents = commit_data.get("parents", []) + if parents != []: + parent_sha = parents[0].get("sha", "") + try: + commit_data = github.get( + "repos/" + + app.config["PERSISTENCE_CONFIG"]["REPOSITORY_OWNER"] + "/" + + repository + "/commits/" + + parent_sha + ) + except GitHubError as ghe: + logger.error("Error trying to get the commit of id " + parent_sha) + logger.error(ghe.response.text) tree_sha = commit_data.get("commit", {}).get("tree", {}).get("sha", "") @@ -389,9 +374,6 @@ logger.error("Error trying to get the tree of sha " + tree_sha) logger.error(ghe.response.text) - - logger.debug(tree_data) - # Third step and fourth step cat_list = [] for blob in tree_data.get("tree", []): @@ -418,16 +400,7 @@ format="turtle" ) category = Category(graph=cat_graph) - cat_list.append( - { - "cat_label": category.label, - "cat_description": category.description, - "cat_id": category.cat_id, - "cat_properties": category.properties, - "state": "original" - } - ) - + cat_list.append(category) return cat_list