Remove groups and start working on F2F location sharing

This commit is contained in:
Gabriel Augendre 2019-03-03 19:50:09 +01:00
parent 218abab16f
commit 875fa56cc4
9 changed files with 34 additions and 155 deletions

View file

@ -2,7 +2,7 @@ from django.contrib import admin
from django.contrib.admin import register
from django.contrib.auth.admin import UserAdmin
from .models import Friend, FriendLocation, LocationSharingGroup
from .models import Friend, FriendLocation
admin.site.register(Friend, UserAdmin)
@ -14,10 +14,3 @@ class FriendLocationAdmin(admin.ModelAdmin):
('Place', {'fields': ('latitude', 'longitude')}),
('Dates', {'fields': ('start_date', 'end_date')}),
]
@register(LocationSharingGroup)
class LocationSharingGroupAdmin(admin.ModelAdmin):
list_display = [
'name',
]

View file

@ -0,0 +1,26 @@
# Generated by Django 2.1.7 on 2019-03-03 18:43
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('map', '0008_auto_20190303_1834'),
]
operations = [
migrations.RemoveField(
model_name='locationsharinggroup',
name='friends',
),
migrations.AddField(
model_name='friend',
name='shares_location_to',
field=models.ManyToManyField(related_name='is_shared_location_by', to=settings.AUTH_USER_MODEL),
),
migrations.DeleteModel(
name='LocationSharingGroup',
),
]

View file

@ -13,6 +13,8 @@ class BaseModel(models.Model):
class Friend(AbstractUser):
shares_location_to = models.ManyToManyField('Friend', related_name='is_shared_location_by')
def get_display_name(self):
display_name = super().get_full_name()
if not display_name:
@ -20,24 +22,6 @@ class Friend(AbstractUser):
return display_name
@property
def _relatives_set(self):
relative_set = set()
for group in self.location_sharing_groups.all():
for relative in group.friends.all():
relative_set.add(relative)
return relative_set
@property
def relatives(self):
return list(self._relatives_set)
@property
def relatives_with_self(self):
relatives = self._relatives_set
relatives.add(self)
return list(relatives)
class FriendLocation(BaseModel):
latitude = CoordinateField()
@ -70,9 +54,3 @@ class FriendLocation(BaseModel):
if self.end_date:
html += f' until {self.end_date}'
return html
class LocationSharingGroup(models.Model):
name = models.CharField(max_length=250)
friends = models.ManyToManyField(Friend, related_name='location_sharing_groups', blank=True)

View file

@ -1,19 +0,0 @@
{% extends 'map/base.html' %}
{% load crispy_forms_filters %}
{% block title %}Create a group{% endblock %}
{% block h1 %}Create a group{% endblock %}
{% block content %}
<div class="alert alert-info">
<h4 class="alert-heading">Notice</h4>
<div>
All members in this group can see your location.
</div>
</div>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Create</button>
</form>
{% endblock %}

View file

@ -1,20 +0,0 @@
{% extends 'map/base.html' %}
{% load crispy_forms_filters %}
{% block title %}Leave a group{% endblock %}
{% block h1 %}Leave a group{% endblock %}
{% block content %}
<div class="alert alert-warning">
<h4 class="alert-heading">Are you sure?</h4>
<div>
If you leave this group you will not be able to see the location of people in the group.
If you're the last member of the group, it will be deleted.
</div>
</div>
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-warning">Yes, I'm sure</button>
<a href="{% url 'manage-groups' %}" class="btn btn-secondary">No, cancel</a>
</form>
{% endblock %}

View file

@ -1,27 +0,0 @@
{% extends 'map/base.html' %}
{% load crispy_forms_filters %}
{% block title %}Manage your groups{% endblock %}
{% block h1 %}Manage your groups{% endblock %}
{% block content %}
<div class="alert alert-info">
<h4 class="alert-heading">Notice</h4>
<div>
Here you can list and manage groups you belong to. All members in these groups can see your location.
</div>
</div>
<ul class="list-group">
{% for group in groups %}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ group.name }}
<span>
<span class="badge badge-secondary badge-pill">{{ group.friends.count }}</span>
<a href="{% url 'leave-group' group.pk %}" class="btn btn-sm btn-secondary">Leave</a>
</span>
</li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -24,11 +24,15 @@
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let marker = null;
{% for relative in friend.relatives_with_self %}
{% for relative in friend.is_shared_location_by.all %}
{% with relative.location as location %}
marker = L.marker([{{ location.latitude_str }}, {{ location.longitude_str }}]).addTo(map);
marker.bindPopup("{{ location.safe_html|safe }}");
{% endwith %}
{% endfor %}
{% with friend.location as location %}
marker = L.marker([{{ location.latitude_str }}, {{ location.longitude_str }}]).addTo(map);
marker.bindPopup("{{ location.safe_html|safe }}");
{% endwith %}
</script>
{% endblock %}

View file

@ -7,9 +7,6 @@ urlpatterns = [
path('change-location', views.EditLocationView.as_view(), name='change-location'),
path('add-location', views.AddLocationView.as_view(), name='add-location'),
path('delete-location', views.DeleteLocationView.as_view(), name='delete-location'),
path('manage-groups', views.ManageGroupsView.as_view(), name='manage-groups'),
path('add-group', views.CreateGroupView.as_view(), name='add-group'),
path('leave-group/<int:pk>', views.LeaveGroupView.as_view(), name='leave-group'),
path('accounts/profile', views.UpdateProfileView.as_view(), name='change-profile'),
path('accounts/profile/delete', views.DeleteProfileView.as_view(), name='delete-profile'),
]

View file

@ -34,12 +34,6 @@ class MapView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView):
'display': f'Add your location'
}]
actions.append({
'url': reverse_lazy('manage-groups'),
'category': 'secondary',
'display': f'Manage your groups'
})
return actions
@ -132,50 +126,3 @@ class DeleteProfileView(LoginRequiredMixin, generic.DeleteView):
def get_success_url(self):
messages.success(self.request, 'Your profile has been successfully and permanently deleted')
return super().get_success_url()
class ManageGroupsView(LoginRequiredMixin, QuickActionsMixin, generic.ListView):
model = models.LocationSharingGroup
context_object_name = 'groups'
template_name = 'map/list_groups.html'
def get_queryset(self):
return self.request.user.location_sharing_groups.all()
def get_quick_actions(self):
return [{
'url': reverse_lazy('add-group'),
'category': 'primary',
'display': 'Create a group'
}]
class CreateGroupView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
model = models.LocationSharingGroup
context_object_name = 'group'
template_name = 'map/change_group.html'
success_url = reverse_lazy('manage-groups')
fields = [
'name'
]
def get_success_url(self):
self.object.friends.add(self.request.user)
messages.success(self.request, 'The group has been successfully created')
return super().get_success_url()
class LeaveGroupView(LoginRequiredMixin, generic.UpdateView):
model = models.LocationSharingGroup
context_object_name = 'group'
template_name = 'map/leave_group.html'
fields = []
success_url = reverse_lazy('manage-groups')
def get_success_url(self):
self.object.friends.remove(self.request.user)
if self.object.friends.count() == 0:
self.object.delete()
messages.success(self.request, 'You successfully left the group')
return super().get_success_url()