~edwargix/git.sr.ht

1a30d7f5aa98b0f42ae1dcf913794f9784814bc1 — Drew DeVault 8 years ago edbcfc1
Add pagination to user repos
4 files changed, 68 insertions(+), 31 deletions(-)

M git/blueprints/manage.py
M git/blueprints/public.py
M templates/manage.html
M templates/user.html
M git/blueprints/manage.py => git/blueprints/manage.py +7 -2
@@ 15,9 15,10 @@ repos_path = cfg("cgit", "repos")
@manage.route("/manage")
@loginrequired
def index():
    another = request.args.get("another")
    repos = Repository.query.filter(Repository.owner_id == current_user.id)\
            .order_by(Repository.updated.desc()).all()
    return render_template("manage.html", repos=repos)
    return render_template("manage.html", repos=repos, another=another)

@manage.route("/manage/create", methods=["POST"])
@loginrequired


@@ 34,6 35,7 @@ def create():
            .order_by(Repository.updated.desc()).all()
    valid.expect(not repo_name or not repo_name in [r.name for r in repos],
            "This name is already in use.", field="repo-name")
    another = valid.optional("another")

    if not valid.ok:
        return render_template("manage.html",


@@ 59,4 61,7 @@ def create():

    db.session.commit()

    return redirect("/~{}/{}".format(current_user.username, repo_name))
    if another == "on":
        return redirect("/manage?another")
    else:
        return redirect("/~{}/{}".format(current_user.username, repo_name))

M git/blueprints/public.py => git/blueprints/public.py +7 -3
@@ 37,13 37,16 @@ def user_index(username):
    if search:
        repos = repos.filter(Repository.name.ilike("%" + search + "%"))
    repos = repos.order_by(Repository.updated.desc())
    total_pages = repos.count() // 5 + 1
    if page:
        try:
            page = int(page) - 1
            repos = repos.offset(page * 10)
            repos = repos.offset(page * 5)
        except:
            page = None
    repos = repos.limit(10).all()
    else:
        page = 0
    repos = repos.limit(5).all()
    r = requests.get(meta_uri + "/api/user/profile", headers={
        "Authorization": "token " + user.oauth_token
    }) # TODO: cache


@@ 56,4 59,5 @@ def user_index(username):
            repos=repos,
            profile=profile,
            search=search,
            page=page)
            page=page + 1,
            total_pages=total_pages)

M templates/manage.html => templates/manage.html +39 -26
@@ 3,36 3,14 @@
<div class="container">
  <div class="row">
    <section class="col-md-6">
      {% if len(repos) != 0 %}
      <h3>Manage repositories</h3>
      <dl>
        {% for repo in repos %}
          <dt>
            <a href="/~{{current_user.username}}/{{repo.name}}">
              ~{{current_user.username}}/{{repo.name}}
            </a>
          </dt>
          <dd>
            {% if not repo.description %}
            <em>This repository has no description</em>
            {% else %}
            {{ repo.description }}
            {% endif %}
            <br />
            <a href="#">Edit details</a>
            &mdash;
            <a href="#">Manage collaborators</a>
            &mdash;
            <a href="#">Delete repository</a>
          </dd>
        {% endfor %}
      </dl>
      {% endif %}
      <h3>Create new repository</h3>
      <h3 id="create">Create new repository</h3>
      <form method="POST" action="/manage/create">
        <div class="form-group {{valid.cls("repo-name")}}">
          <label for="repo-name">Name</label>
          <input
            {% if another %}
            autofocus
            {% endif %}
            type="text"
            name="repo-name"
            id="repo-name"


@@ 85,7 63,42 @@
          </div>
        </fieldset>
        <button type="submit" class="btn btn-default">Create</button>
        <label class="form-check-label" style="margin-left: 0.5rem">
          <input
            class="form-check-input"
            type="checkbox"
            name="another"
            style="position: relative; top: 2px;"
            {% if another %}
            checked
            {% endif %}> Create another?
        </label>
      </form>
      {% if len(repos) != 0 %}
      <h3>Manage repositories</h3>
      <dl>
        {% for repo in repos %}
          <dt>
            <a href="/~{{current_user.username}}/{{repo.name}}">
              ~{{current_user.username}}/{{repo.name}}
            </a>
          </dt>
          <dd>
            {% if not repo.description %}
            <em>This repository has no description</em>
            {% else %}
            {{ repo.description }}
            {% endif %}
            <br />
            <a href="#">Edit details</a>
            &mdash;
            <a href="#">Manage collaborators</a>
            &mdash;
            <a href="#">Delete repository</a>
          </dd>
        {% endfor %}
      </dl>
      {% endif %}
    </section>
  </div>
</div>

M templates/user.html => templates/user.html +15 -0
@@ 46,6 46,21 @@
        </p>
      {% endfor %}
      {% endif %}
      {% if total_pages > 1 %}
      {% if page != 1 %}
      <a href="?page={{ page - 1 }}{{ '&search=' + search if search else '' }}" class="pull-left">[previous]</a>
      {% else %}
      <span class="pull-left" style="color: transparent">[previous]</span>
      {% endif %}
      {% if page != total_pages %}
      <a href="?page={{ page + 1 }}{{ '&search=' + search if search else '' }}" class="pull-right">[next]</a>
      {% else %}
      <span class="pull-right" style="color: transparent">[previous]</span>
      {% endif %}
      <div class="text-centered">
        [ {{ page }} / {{ total_pages }} ]
      </div>
      {% endif %}
    </section>
  </div>
</div>