bornhack-website/src/rideshare/views.py

113 lines
3.4 KiB
Python
Raw Normal View History

from django import forms
2018-08-10 15:41:19 +00:00
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
2018-08-13 17:06:30 +00:00
from django.urls import reverse
2018-08-08 20:18:39 +00:00
from django.views.generic import (
CreateView,
DeleteView,
DetailView,
ListView,
UpdateView,
2018-08-08 20:18:39 +00:00
)
from camps.mixins import CampViewMixin
2018-08-10 15:41:19 +00:00
from utils.email import add_outgoing_email
from utils.mixins import UserIsObjectOwnerMixin
2018-08-08 20:18:39 +00:00
from .models import Ride
2018-08-10 15:41:19 +00:00
class ContactRideForm(forms.Form):
message = forms.CharField(
2019-06-16 12:32:24 +00:00
widget=forms.Textarea(
attrs={"placeholder": "Remember to include your contact information!"}
),
label="Write a message to the author of this rideshare",
help_text="ATTENTION!: Pressing send will send an email to the author with the above text. It is up to you to include your contact information in the message so the person receiving the email can contact you.",
2018-08-10 15:41:19 +00:00
)
2018-08-08 20:18:39 +00:00
class RideList(LoginRequiredMixin, CampViewMixin, ListView):
model = Ride
class RideDetail(LoginRequiredMixin, CampViewMixin, DetailView):
model = Ride
2018-08-10 15:41:19 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2019-06-16 12:32:24 +00:00
context["form"] = ContactRideForm()
2018-08-10 15:41:19 +00:00
return context
def post(self, request, **kwargs):
form = ContactRideForm(request.POST)
if form.is_valid():
ride = self.get_object()
add_outgoing_email(
2019-06-16 12:32:24 +00:00
text_template="rideshare/emails/contact_mail.txt",
2018-08-10 15:41:19 +00:00
to_recipients=[ride.user.emailaddress_set.get(primary=True).email],
formatdict=dict(
rideshare_url="https://bornhack.dk{}".format(
reverse(
2019-06-16 12:32:24 +00:00
"rideshare:detail",
kwargs={"camp_slug": self.camp.slug, "pk": ride.pk},
2018-08-10 15:41:19 +00:00
)
),
2019-06-16 12:32:24 +00:00
message=form.cleaned_data["message"],
2018-08-10 15:41:19 +00:00
),
subject="BornHack rideshare message!",
)
messages.info(request, "Your message has been sent.")
return HttpResponseRedirect(ride.get_absolute_url())
2018-08-08 20:18:39 +00:00
class RideCreate(LoginRequiredMixin, CampViewMixin, CreateView):
model = Ride
2020-02-07 17:46:34 +00:00
fields = [
"author",
"has_car",
"from_location",
"to_location",
"when",
"seats",
"description",
]
def get_initial(self):
"""
Default 'author' to users public_credit_name where relevant
"""
2020-02-07 17:46:34 +00:00
return {"author": self.request.user.profile.get_public_credit_name}
2018-08-08 20:18:39 +00:00
def form_valid(self, form, **kwargs):
"""
Set camp and user before saving
"""
2018-08-08 20:18:39 +00:00
ride = form.save(commit=False)
ride.camp = self.camp
ride.user = self.request.user
ride.save()
self.object = ride
return HttpResponseRedirect(self.get_success_url())
class RideUpdate(LoginRequiredMixin, CampViewMixin, UserIsObjectOwnerMixin, UpdateView):
2018-08-08 20:18:39 +00:00
model = Ride
2020-02-07 17:46:34 +00:00
fields = [
"author",
"has_car",
"from_location",
"to_location",
"when",
"seats",
"description",
]
2018-08-08 20:18:39 +00:00
class RideDelete(LoginRequiredMixin, CampViewMixin, UserIsObjectOwnerMixin, DeleteView):
2018-08-08 20:18:39 +00:00
model = Ride
2018-08-13 17:06:30 +00:00
def get_success_url(self):
2019-06-16 12:32:24 +00:00
return reverse("rideshare:list", kwargs={"camp_slug": self.camp.slug})