Merge pull request 'Improve PLZ functionality' (#26) from dev into main
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

Reviewed-on: #26
This commit is contained in:
Jannis Portmann 2023-10-15 17:30:40 +02:00
commit 884839c829
4 changed files with 26 additions and 9 deletions

View file

@ -32,6 +32,12 @@ Searching with filters such as:
| Name | `string` | textfield |
| Category | `Category` | dropdown |
## Setup
When deploying, don't forget to pull the PLZ index and save it to the database using
```
python manage.py getplzindex --force
```
## Development
To get started with development, see [DEVELOPMENT.md](DEVELOPMENT.md)

View file

@ -21,21 +21,21 @@
<h1 class="mb-3">
{{ offer.title }}<span class="ms-2 badge text-bg-secondary">{{ offer.get_category_display }}</span>
</h1>
<div class="mb-3 d-flex column-gap-2">
<p class="mr-3">
<i class="fas fa-user"></i>
<div class="mb-3 d-flex flex-column">
<p class="mb-1">
<i class="fas fa-user me-1"></i>
{% if offer.user == user %}
{% trans "You" %}
{% else %}
{{ offer.user.username }}
{% endif %}
</p>
<p class="mr-3">
{% with plz=offer|get_plz %}<i class="fas fa-map-marker-alt"></i> {{ offer.zipcode }} {{ plz.name }}{% endwith %}
<p class="mb-1">
{% with plz=offer|get_plz %}<i class="fas fa-map-marker-alt me-1"></i> {{ offer.zipcode }} {{ plz.name }}{% endwith %}
</p>
{% if dist %}
<p class="pr-3">
<i class="fas fa-map-signs mr-1"></i> ca. {{ dist }} km
<p class="mb-1">
<i class="fas fa-map-signs me-1"></i> ca. {{ dist }} km
</p>
{% endif %}
</div>

View file

@ -7,7 +7,10 @@ def calculate_distance(plz_1: Plz, plz_2: Plz):
if plz_1 == plz_2:
return 0
dist = round(distance((plz_1.lat, plz_1.lon), (plz_2.lat, plz_2.lon)).kilometers)
try:
dist = round(distance((plz_1.lat, plz_1.lon), (plz_2.lat, plz_2.lon)).kilometers)
except AttributeError:
return None
return None if dist > 400 else dist

View file

@ -59,7 +59,10 @@ def offer_detail(request, offer_id):
if offer.zipcode == request.user.zipcode:
dist = 0
else:
dist = calculate_distance(Plz.objects.get(plz=offer.zipcode), Plz.objects.get(plz=request.user.zipcode))
user_plz = get_single_plz(request.user.zipcode)
offer_plz = get_single_plz(offer.zipcode)
dist = calculate_distance(offer_plz, user_plz)
else:
dist = None
else:
@ -193,6 +196,11 @@ def register_user(request):
return render(request, "basic_form.html", {"form": form, "button_label": _("Register"), "title": _("Registeration"), "umami_event": "User registration", "form_note": mark_safe(_('Please note the <a href="/imprint">privacy policy</a>'))})
def get_single_plz(plz):
p = Plz.objects.filter(plz=plz)
return p[0] if len(p) >= 1 else None
def save_language(request):
referer_url = request.META.get('HTTP_REFERER')
response = redirect(referer_url)