Ejemplo n.º 1
0
    def post(self, request):
        try:
            username = bleach.clean(request.data.get('username'))
            password = bleach.clean(request.data.get('password'))
            firstname = bleach.clean(request.data.get('firstname'))
            lastname = bleach.clean(request.data.get('lastname'))
            email = bleach.clean(request.data.get('email'))
            location = bleach.clean(request.data.get('location'))

            #GetCoordinates returns the coordinates based on the users input location - provided by gooogle api
            coordinates = getCoordinates(location)
            user = User.objects.create_user(username=username,
                                            password=password)
            user.save()
            profile = Profile(user=user,
                              FirstName=firstname,
                              LastName=lastname,
                              EmailAddress=email,
                              Location=location,
                              coordinates=coordinates)
            profile.save()
            serializer = ProfileSerializer(profile)
            return Response({
                'user': serializer.data,
                "token": AuthToken.objects.create(user)
            })
        except:
            return HttpResponseBadRequest()
Ejemplo n.º 2
0
 def post(self, request):
     #gets coordinates for all members of a group.
     group = Groups.get_object_or_404(
         groupName=bleach.clean(request.data.get('groupname')))
     members = MemberShip.objects.filter(group=group)
     memberUsers = members.values_list('user', flat=True)
     users = Profile.objects.filter(user__in=memberUsers)
     locations = users.values_list('Location', flat=True)
     coords = []
     for loc in location:
         if loc != (0, 0) and loc != "null":
             coords.append((getCoordinates(location)))
     jsonRes = serializers.serialize('json', coords)
     return Response({'coords': jsonRes})
Ejemplo n.º 3
0
 def post(self, request):
     profile = Profile.objects.filter(user=request.user)
     profile.FirstName = request.data.get('firstname')
     profile.LastName = request.data.get('lastname')
     profile.EmailAddress = request.data.get('email')
     profile.Location = request.data.get('location')
     #reget their coordinates if they change
     profile.coordinates = getCoordinates(request.data.get('location'))
     profile.save()
     serializer = ProfileSerializer(profile)
     if not serializer.is_valid():
         return Response({'serializer': serializer, 'user': profile})
     serializer.save()
     return Response({'user': serializer.data})
Ejemplo n.º 4
0
    def post(self, request):

        if request.data.get('type') == 'get':
            print("inside views post ", request.data)
            #For getting an individual group that a user is part of
            g = Group.objects.get(groupName=request.data.get('groupname'))
            m = MemberShip.objects.filter(group=g)
            m = m.values_list('person', flat=True)
            profiles = Profile.objects.filter(pk__in=m)
            jsonRes = serializers.serialize('json', profiles)
            #locations - all nearby locations to the midpoint of the group.
            locations = Location.objects.filter(group=g)
            locationsJson = serializers.serialize('json', locations)
            print('locations:', locationsJson)
            return Response({
                'members': jsonRes,
                'locations': locationsJson,
                'midpoint': g.midpoint
            })
        else:
            g = Group()
            #For creating a new group.
            print("inside views post ", request.data)
            g.groupName = request.data.get('groupname')
            g.save()
            p = Profile.objects.get(user=request.user)
            g.midpoint = getCoordinates(p.Location)
            m1 = MemberShip(group=g, person=p)
            m1.save()
            g.save()
            print('membership', m1, g.midpoint)
            #midpoint created of the creating users location.
            places = getPlaces(g.midpoint)
            print(places)
            #get locations close to them
            for place in places:
                l = Location(locationName=place['name'],
                             latLong=place['coordinates'],
                             rating=place['rating'],
                             types=place['types'],
                             group=g)
                l.save()
            serializer = GroupSerializer(g)
            l = Location.objects.filter(group=g)
            jsonRes = serializers.serialize('json', l.all())
            print("locations:", jsonRes)
            print('gorupL', serializer.data)
            return Response({'group': serializer.data, 'locations': jsonRes})
Ejemplo n.º 5
0
    def put(self, request):
        #for adding a user to a group
        try:
            p = Profile.objects.get(EmailAddress=request.data.get('email'))
            g = Group.objects.get(groupName=request.data.get('groupname'))
            m1 = MemberShip(group=g, person=p)
            m1.save()

            #update the midpoint
            members = MemberShip.objects.filter(group=g)
            locationtexts = []
            #get the coordinates of all members in the group - need to recalculate midpoint based on new coords.
            for m in members:
                coord = getCoordinates(m.person.Location)
                if coord != (0, 0):
                    locationtexts.append(m.person.Location)
            g.midpoint = finalmidpoint(locationtexts)
            g.save()

            #delete all old places.
            Location.objects.filter(group=g).delete()

            #get new locations
            places = getPlaces(g.midpoint)
            for place in places:
                l = Location(locationName=place['name'],
                             latLong=place['coordinates'],
                             rating=place['rating'],
                             types=place['types'],
                             group=g)
                l.save()
            locations = Location.objects.filter(group=g)
            jsonRes = serializers.serialize('json', locations.all())
            serializer = GroupSerializer(g)
            print('locations:', jsonRes)
            return Response({
                'group': serializer.data,
                'success': 'success',
                'locations': jsonRes
            })
        except Exception as e:
            print('not saved', e)
            return Response({'success': 'fail'})
Ejemplo n.º 6
0
 def get(self, request):
     #gets the coordinates of a given user
     lat, longi = getCoordinates(request.user.Location)
     return Response({'lat': lat, 'long': longi})