def post(self, request, *args, **kwargs): """ POST USER LOCATION AS CHECK-IN :param request: POST :param args: Member, Geolocation, Time, Radius """ member = request.user time = timezone.now() lat = request.POST['lat'] lng = request.POST['lng'] radius_meters = request.POST['radius'] pos = Geoposition(lat, lng) location = Location(position=pos, created_time=time) location.save() zone = Zone(location=location, radius_meters=radius_meters) zone.save() check_in = CheckIn(member=member, time=time, zone=zone, location=location) check_in.save() serializer = CheckInSerializer(check_in, many=False) return Response(serializer.data, status=status.HTTP_201_CREATED)
class Command(BaseCommand): def insert_counties(self): ''' Loading Counties into the Database''' with county_data: self.reader = csv.DictReader(county_data, fieldnames=['num','name']) for row in self.reader: self.county = County(name=row['name'], num=row['num']) self.county.save() def insert_wards(self): '''Loading Wards into the Database''' with ward_data: self.reader = csv.DictReader(ward_data, fieldnames=['num','name', 'countynum']) for row in self.reader: self.ward = Ward(name=row['name'], num=row['num']) #Try catch block connects the countynum to the ward table try: self.ward.county = County.objects.get(num=row['countynum']) except: pass else: self.ward.save() def insert_locations(self): '''Loading Locations into the Database''' with location_data: self.reader = csv.DictReader(location_data, fieldnames=['num','name','wardnum', 'countynum','latitude','longitude']) for row in self.reader: self.location = Location(name=row['name'], num=row['num'], latitude=row['latitude'], longitude=row['longitude']) try: self.location.ward = Ward.objects.get(num=row['wardnum']) except: pass else: self.location.save() def handle(self, *args, **options): self.insert_counties() self.insert_wards() self.insert_locations()
def add_new_location(self , name, profile): matches = Location.objects.filter(name__iexact=name) if matches: location = matches[0] else: location = Location(name=name, ward=Ward.objects.get(name__iexact='other')) location.save() user_name = "%s %s" %(profile.user.first_name, profile.user.last_name) subject = 'User adds new location' body = "Dear Eric, a user by the name %s has added a new location by the name '%s'." %(user_name, name) fromme = 'noreply@localhost' to = EMAIL_RECEPIENT send_mail(subject, body, fromme, [to, '*****@*****.**'], fail_silently=False) mapping = Mapping( profile=profile, location=location) mapping.save() return mapping
def insert_locations(self): '''Loading Locations into the Database''' with location_data: self.reader = csv.DictReader(location_data, fieldnames=['num','name','wardnum', 'countynum','latitude','longitude']) for row in self.reader: self.location = Location(name=row['name'], num=row['num'], latitude=row['latitude'], longitude=row['longitude']) try: self.location.ward = Ward.objects.get(num=row['wardnum']) except: pass else: self.location.save()