~homeworkprod/byceps-bungalowcontest

50985cf244738f360a2d8497e88e0a6b5493c2bc — Jochen Kupperschmidt a month ago 52ec7c3
Remove interface for mutation from self-inhabited bungalow ratings
M byceps/blueprints/site/bungalow_contest/templates/site/bungalow_contest/contestants.html => byceps/blueprints/site/bungalow_contest/templates/site/bungalow_contest/contestants.html +7 -3
@@ 22,7 22,8 @@
  {%- if contest.contestants %}
    {%- for contestant in contest.contestants|sort(attribute='bungalow_occupancy.bungalow.number') %}
      {%- with bungalow = contestant.bungalow_occupancy.bungalow %}
    <li class="bungalow-contest-candidate">
        {%- with inhabited = inhabited_bungalow_contestant_id == contestant.id %}
        <li class="bungalow-contest-candidate{% if inhabited %} inhabited{% endif %}">
      <h2 class="title">
        {{ render_bungalow_avatar(bungalow, 24) }}
        <a href="{{ url_for('.view_contestant', id=contestant.id) }}">


@@ 55,7 56,9 @@
                      id="{{ name }}-{{ loop.index }}"
                      name="{{ name }}"
                      value="{{ value }}"
                      {{- ' checked' if (value == user_value) else '' }}>
                      {{- ' checked' if (value == user_value) else '' }}
                      {{- ' disabled' if inhabited else '' }}
                      >
                  <label
                      data-rating-contestant-id="{{ contestant.id }}"
                      data-rating-attribute-id="{{ attribute.id }}"


@@ 76,6 79,7 @@

      </div>
    </li>
        {%- endwith %}
      {%- endwith %}
    {%- endfor %}
  </ol>


@@ 90,7 94,7 @@
{% block scripts %}
    <script>
      onDomReady(() => {
        document.querySelectorAll('.rating')
        document.querySelectorAll('.bungalow-contest-candidate:not(.inhabited) .rating')
          .forEach(function(ratingElem) {
            const successIndicator = ratingElem.querySelector('.success');
            ratingElem.querySelectorAll('label')

M byceps/blueprints/site/bungalow_contest/views.py => byceps/blueprints/site/bungalow_contest/views.py +30 -1
@@ 37,6 37,7 @@ from byceps.util.framework.blueprint import create_blueprint
from byceps.util.framework.flash import flash_error, flash_success
from byceps.util.framework.templating import templated
from byceps.util.image.models import Dimensions, ImageType
from byceps.util.iterables import find
from byceps.util.views import login_required, redirect_to, respond_no_content

from .forms import ContestantUpdateForm, ImageCreateForm


@@ 262,22 263,44 @@ def contestants():
    """List constestants."""
    contest = _get_contest_or_404()

    contestants = contest.contestants

    user_ratings_by_contestant = {}
    if g.user.authenticated:
        for contestant in contest.contestants:
        for contestant in contestants:
            user_ratings_by_contestant[contestant.id] = (
                bungalow_contest_service.get_ratings_by_user(
                    g.user.id, contestant.id
                )
            )

    inhabited_bungalow_contestant_id = _find_inhabited_bungalow_contestant_id(
        contestants
    )

    return {
        'contest': contest,
        'Phase': Phase,
        'user_ratings_by_contestant': user_ratings_by_contestant,
        'inhabited_bungalow_contestant_id': inhabited_bungalow_contestant_id,
    }


def _find_inhabited_bungalow_contestant_id(contestants):
    bungalow = _get_inhabited_bungalow()
    if not bungalow:
        return None

    contestant = find(
        contestants,
        lambda c: c.bungalow_occupancy.bungalow_id == bungalow.id,
    )
    if not contestant:
        return None

    return contestant.id


@blueprint.put('/ratings')
@login_required
@respond_no_content


@@ 304,6 327,12 @@ def rate():

    creator = g.user

    inhabited_bungalow = _get_inhabited_bungalow()
    if inhabited_bungalow and (
        contestant.bungalow_occupancy.bungalow_id == inhabited_bungalow.id
    ):
        abort(400, 'Bungalow inhabitants must not rate their own bungalow.')

    value = json_data.get('value')
    if not value:
        abort(400, 'Missing value.')