geosolutions-it / smb-portal

SaveMyByke portal
https://waffle.io/geosolutions-it/smb-portal
1 stars 3 forks source link

add `winner_description` to won competitions #162

Closed ricardogsilva closed 6 years ago

ricardogsilva commented 6 years ago

This PR adds a new attribute to the representation sent to requests of /api/my-competitions-won/.

The attribute is called winner_description and it shows a description for the message that can be shown for each of the prizes won by the user.

Example response:

GET /api/my-competitions-won/

{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 162,
      "url": "http://10.0.1.66:8000/api/competitions/162/",
      "name": "Weekly CO2 saver",
      "description": "",
      "age_groups": [
        "19 - 30"
      ],
      "start_date": "2018-09-23T00:00:00Z",
      "end_date": "2018-09-29T00:00:00Z",
      "criteria": [
        "saved CO2 emissions"
      ],
      "winner_threshold": 1,
      "score": {
        "saved CO2 emissions": 404.142961006002
      },
      "leaderboard": [
        {
          "username": "track_tester1",
          "saved CO2 emissions": 404.142961006002
        }
      ],
      "prizes": [
        {
          "winner_description": "Congratulations, you got 1st place. You score was saved CO2 emissions: 404.143",
          "prize": {
            "name": "1 5& 5 e spuma",
            "description": "",
            "image": null,
            "url": null,
            "sponsor": {
              "name": "Torteria 'Da Gagarin'",
              "logo": null,
              "url": null
            }
          },
          "user_rank": 1
        }
      ]
    }
  ]
}

This new description may be edited by the site staff by visiting /admin/prizes/competitionprize/{prize-id}/change and editing the prize_attribution_template field. This field is rendered by the API as if it were a django template. It has the following context variables available:

Example:

If the prize's prize_attribution_template is set to Congratulations, you got {{ rank|ordinal }} place. You score was {{ score }}, the API will later render it as Congratulations, you got 1st place. You score was saved CO2 emissions: 404.143