diff --git a/map/forms.py b/map/forms.py index 36eb9e6..c3c9b22 100644 --- a/map/forms.py +++ b/map/forms.py @@ -1,6 +1,6 @@ from django import forms -from map.models import FriendLocation +from map.models import FriendLocation, Friend class LocationForm(forms.ModelForm): @@ -21,3 +21,20 @@ class LocationForm(forms.ModelForm): def clean_friend(self): return self.request.user + + +class ProfileForm(forms.ModelForm): + class Meta: + model = Friend + fields = [ + 'username', + 'first_name', + 'last_name', + 'email', + 'shares_location_to', + ] + + def __init__(self, request, *args, **kwargs): + super().__init__(*args, **kwargs) + self.request = request + self.fields['shares_location_to'].queryset = Friend.objects.exclude(pk=request.user.pk) diff --git a/map/migrations/0010_auto_20190303_1955.py b/map/migrations/0010_auto_20190303_1955.py new file mode 100644 index 0000000..ba1e7c6 --- /dev/null +++ b/map/migrations/0010_auto_20190303_1955.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.7 on 2019-03-03 18:55 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('map', '0009_auto_20190303_1943'), + ] + + operations = [ + migrations.AlterField( + model_name='friend', + name='shares_location_to', + field=models.ManyToManyField(blank=True, related_name='is_shared_location_by', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/map/models.py b/map/models.py index 1ee939c..ffa87f7 100644 --- a/map/models.py +++ b/map/models.py @@ -13,7 +13,7 @@ class BaseModel(models.Model): class Friend(AbstractUser): - shares_location_to = models.ManyToManyField('Friend', related_name='is_shared_location_by') + shares_location_to = models.ManyToManyField('Friend', related_name='is_shared_location_by', blank=True) def get_display_name(self): display_name = super().get_full_name() diff --git a/map/views.py b/map/views.py index f05463a..57ef3d1 100644 --- a/map/views.py +++ b/map/views.py @@ -4,7 +4,7 @@ from django.urls import reverse_lazy from django.views import generic from map import models -from map.forms import LocationForm +from map.forms import LocationForm, ProfileForm from map.mixins import QuickActionsMixin @@ -99,12 +99,8 @@ class UpdateProfileView(LoginRequiredMixin, generic.UpdateView): template_name = 'map/change_profile.html' success_url = reverse_lazy('map') - fields = [ - 'username', - 'first_name', - 'last_name', - 'email', - ] + def get_form(self, form_class=None): + return ProfileForm(self.request, **self.get_form_kwargs()) def get_object(self, queryset=None): return self.request.user