예제 #1
0
    def _get_loc(pc, coll):
        _new_loc = None
        _loc = coll.find_one({'postal code': pc})
        if _loc:
            district = ', %s' % _loc['admin name3'] if _loc[
                'admin name3'] else ''
            place_name = '%s%s' % ((_loc['place name'], district))
            place_name = place_name.replace(' Ward',
                                            '')  #.replace(' City', '')
            loc_type = POSTCODE if len(pc) > 4 else POSTCODEDISTRICT

            _new_loc = Location(
                id=_loc['postal code'].replace(' ', ''),
                postcode=_loc['postal code'],
                place_name=place_name,
                lat_lon=[float(_loc['latitude']),
                         float(_loc['longitude'])],
                loc_type=loc_type,
                accuracy=_loc['accuracy'],
                district=_loc['admin name3'],
                country_code=_loc['admin code1'])
            _new_loc.save()

        print _new_loc, _new_loc.loc_type
        return _new_loc
예제 #2
0
    def update_db(locations, aliases):
        for location in locations:
            new_location_entry = Location(zip_code=location.zip_code,
                                          latitude=location.latitude,
                                          longitude=location.longitude,
                                          city=location.primary_city,
                                          state=location.state,
                                          population=location.estimated_population,
                                          timezone=location.timezone
                                          )
            logging.debug('Saving {} to the database'.format(location))
            new_location_entry.save()

        for alias in aliases:

            new_alias_entry = Alias(location=Location.objects.filter(zip_code=alias.zip_code)[0],
                                    alias=alias.name
                                    )

            logging.debug('Saving {} to the database'.format(alias))
            try:
                new_alias_entry.save()

            # triggered when the input does not match the datatype of the field (eg., too many characters for VARCHAR)
            except DataError:
                logging.critical('Failed to enter alias: {} into the database'.format(alias),
                                 exc_info=True,
                                 stack_info=True)
예제 #3
0
def addUser(request):
    form = UserForm(request.POST)

    if form.is_valid():
        try:
            with transaction.atomic():
                enterprise = Enterprise()
                enterprise.save()
                request.session['idEnterprise'] = enterprise.id

                location = Location(enterprise=enterprise,
                                    lat=0,
                                    lng=0,
                                    name='Main Office')
                location.save()

                user = User(location=location,
                            email=form.cleaned_data['email'],
                            password=form.cleaned_data['password'])
                user.save()
                request.session['idUser'] = user.id

                profile = Profile(user=user, role="Administrator")
                profile.save()

                return render(request, 'users/dashboard.html')

        except Exception as e:
            print(e)
            messages.error(request, 'Sorry, Internal Error')

    else:
        messages.error(request, 'Please fill the form')
        return HttpResponseRedirect('/signup')
예제 #4
0
파일: views.py 프로젝트: ezy023/marker-api
def create_location(request, user_id):
    received_json_data = json.loads(request.body)
    form = LocationForm(received_json_data)
    if form.is_valid():
        new_location = Location()
        new_location.latitude = form.cleaned_data['latitude']
        new_location.longitude = form.cleaned_data['longitude']
        new_location.image_url = form.cleaned_data['image_url']
        try:
            request.user.location_set.add(new_location)
        except Exception as e:
            logger.error("Error associating location to user. User %s Location %s. %s", request.user, new_location, e.message)
            return HttpResponseBadRequest(e.message)

        # Add tags to the location if any were passed
        tag_ids = form.cleaned_data['tag_ids']
        if tag_ids:
            tags = Tag.objects.filter(id__in=tag_ids)
            for tag in tags:
                LocationTags.objects.create(tag=tag, location=new_location)

        data = json.dumps(new_location.to_dict())
        return HttpResponse(data)
    else:
        logger.error("Form invalid. %s", form.errors)
        error_data = json.dumps(form.errors)
        return HttpResponseBadRequest(error_data)
예제 #5
0
 def handle(self, *args, **options):
     file = os.path.abspath( args[0])
     headers         = None        
     address_field   = None     
     parcel_field    = None
     lat_field       = None
     long_field      = None   
     previous_loc    = Location()        
     
     with open(file) as f:
         reader = csv.reader(f)
 
         for row in reader:
             if headers is None:
                 headers = row
                 address_field   = headers.index('address')
                 parcel_field    = headers.index('parcel')
                 lat_field       = headers.index('latitude')
                 long_field      = headers.index('longitude')                    
                 continue
 
             try:
                 if(previous_loc.address == row[address_field]):
                     new_parcel = Parcel( location = previous_loc, number = int( row[parcel_field].translate( None, "'")))
                     new_parcel.save()
                 else:
                     previous_loc = Location( address = row[address_field], latitude = row[lat_field], longitude = row[long_field])
                     previous_loc.save()
                     new_parcel = Parcel( location = previous_loc, number = int( row[parcel_field].translate( None, "'")))
                     new_parcel.save()                        
                 
             except Exception, e:
                 print >> sys.stderr, e
예제 #6
0
파일: views.py 프로젝트: edramos/path00
def addUser(request):
    form = UserForm(request.POST)
    
    if form.is_valid():
        try:
            with transaction.atomic():
                enterprise = Enterprise()
                enterprise.save()
                request.session['idEnterprise'] = enterprise.id;
                
                location= Location(enterprise=enterprise, lat=0, lng=0, name='Main Office')
                location.save()
                
                user = User(location = location, email=form.cleaned_data['email'], 
                        password=form.cleaned_data['password'])
                user.save()
                request.session['idUser'] = user.id;
                
                profile = Profile(user = user, role="Administrator")
                profile.save()
                
                return render(request, 'users/dashboard.html')
                
        except Exception as e:
            print(e)
            messages.error(request, 'Sorry, Internal Error')
                
    else:
        messages.error(request, 'Please fill the form')
        return HttpResponseRedirect('/signup')
예제 #7
0
def create_sections_and_cell_for_workshop_three():
    workshop_three = WorkShop.objects.get(number=3)

    Section.objects.bulk_create([
        Section(workshop=workshop_three, name='Секция 3-1', number=1),
        Section(workshop=workshop_three, name='Секция 3-2', number=2),
        Section(workshop=workshop_three, name='Секция 3-3', number=3),
        Section(workshop=workshop_three, name='Секция 3-4', number=4),
        Section(workshop=workshop_three, name='Секция 3-5', number=5),
        Section(workshop=workshop_three, name='Секция 3-6', number=6),
    ])
    Location.objects.bulk_create([
        Location(section=section)
        for section in Section.objects.filter(workshop=workshop_three)
    ])

    for section in Section.objects.filter(workshop=workshop_three):
        SowAndPigletsCell.objects.bulk_create([
            SowAndPigletsCell(workshop=workshop_three,
                              section=section,
                              number=cellNumber)
            for cellNumber in range(1, 46)
        ])
        Location.objects.bulk_create([
            Location(sowAndPigletsCell=cell)
            for cell in SowAndPigletsCell.objects.filter(
                workshop=workshop_three, section=section)
        ])
예제 #8
0
파일: views.py 프로젝트: bchuey/valet
def update_current_position(request):

	# print request.POST 	# <QueryDict: {u'lat': [u'37.7082051'], u'lng': [u'-122.4433762']}>
	# print request.POST['lat']
	# print request.POST['lng']
	user = request.user

	current_position = Location()
	current_position.lat = request.POST['lat']
	current_position.lng = request.POST['lng']
	current_position.save()

	print "current position: "
	print current_position.lat
	print current_position.lng
	print "================="

	user.current_position = current_position
	user.save()

	msg = "Updated current position"

	if user.is_valet:
		print 'updated valet position'
		return Response(msg, template_name='maps/valet/index.html')

	else:
		print 'updated customer position'
		return Response(msg, template_name='maps/user/index.html')
예제 #9
0
    def test_no_state_unicode(self):
        locate = Location(
            city='Rome',
            country='Italy',
        )

        locate.save()

        self.assertEqual(locate.__str__(), 'Rome, Italy')
예제 #10
0
파일: views.py 프로젝트: indro/t2c
def checkin(request):
    if request.method == 'POST':
        checkin_form = CheckinForm(request.POST)
        if checkin_form.is_valid():
            c = Location(place=checkin_form.cleaned_data['place'], latitude=checkin_form.cleaned_data['latitude'], longitude=checkin_form.cleaned_data['longitude'], user=request.user, time_checkin= datetime.datetime.now())
            c.save()
            return HttpResponseRedirect(reverse('locations.views.your_locations'))
    else:
        return HttpResponseRedirect(reverse('locations.views.new'))
예제 #11
0
class TestImage(TestCase):
    def setUp(self):
        self.category = Category(name="Test cat")
        self.category.save_category()
        self.location = Location(name="Test Loc")
        self.location.save_location()

        self.image = Image(
            name="Test Img",
            location_id=self.location,
            category_id=self.category,
            image=SimpleUploadedFile(
                name='image_test.jpg',
                content=open(
                    'photo_gallery/static/images/default_location.jpg',
                    'rb').read(),
                content_type='image/jpeg'))

    def test_save_image(self):
        self.image.save_image()
        images = Image.objects.all()
        self.assertTrue(len(images) > 0)

    def test_object_instance(self):
        self.image.save_image()
        images = Image.objects.all()
        self.assertIsInstance(images[0], Image)

    def test_del_image(self):
        self.image.save_image()
        self.image.delete_image()
        images = Image.objects.all()
        self.assertTrue(len(images) == 0)

    def test_updated_image(self):
        self.image.save_image()
        data = dict(name="New Name")
        self.image.update_image(self.image.id, data)
        self.assertTrue(self.image.name, "New Name")

    def test_get_img_by_id(self):
        self.image.save_image()
        print(self.image)
        res = self.image.get_image_by_id(image_id=self.image.id)
        self.assertIsInstance(res, Image)

    def test_filter_img_by_loc(self):
        self.image.save_image()
        res = self.image.filter_by_location(location_id=self.location.id)
        self.assertTrue(len(res), 1)
        self.assertEqual(res[0].location_id.name, self.location.name)

    def test_search_image(self):
        self.image.save_image()
        res = self.image.search_image(category_id=self.category.id)
        self.assertTrue(len(res), 1)
        self.assertEqual(res[0].category_id.name, self.category.name)
예제 #12
0
    def test_state_unicode(self):
        locate = Location(
            city='Loveland',
            state='CO',
            country='USA',
        )

        locate.save()

        self.assertEqual(locate.__str__(), 'Loveland, CO')
예제 #13
0
def test_checkins(client, admin_client):
    url = reverse('webapp.checkins')
    user = User.objects.get(username='******')
    l = Location(user=user, name='Someplace', lat=100, lon=200,
                 address='Someplace St')
    l.save()

    resp = admin_client.get(url)
    assertTemplateUsed(resp, 'checkins.html')
    assert resp.context.get('locations')[0] == l
예제 #14
0
 def obj_create(self, bundle, request=None, **kwargs):
     user = User.objects.get(id=bundle.data['user_id'])
     loc = Location(
         user = user,
         latitude = bundle.data['latitude'],
         longitude = bundle.data['longitude'],
         altitude = bundle.data['altitude'],
     )
     loc.save()
     return loc
예제 #15
0
    def test_geolocate(self):
        locate = Location(
            city='Loveland',
            state='CO',
            country='USA',
        )

        locate.save()

        self.assertEqual(locate.latitude, 40.3977612)
        self.assertEqual(locate.longitude, -105.0749801)
예제 #16
0
def load_locations():
    with location_data:
        reader = csv.DictReader(location_data, fieldnames=["num", "name", "wardnum", "countynum", "lat", "lon"])
        for row in reader:
            location = Location(name=row["name"], num=row["num"], lat=row["lat"], lon=row["lon"])
            try:
                location.ward = Ward.objects.get(num=row["wardnum"])
            except:
                pass
            else:
                location.save()
예제 #17
0
def save(request):
    if ('save' in request.GET):
        for record in SearchResult.objects.all():
            target = Location(nameZH=str(record.nameZH), nameEN=str(record.nameEN), addressZH=str(record.addressZH), addressEN=str(record.addressEN), x=int(record.x), y=int(record.y))
            target.save()
        for recorded in SearchResult.objects.all():
            recorded.delete()
        return render(request, 'confirmation.html')
    else:
        for recorded in SearchResult.objects.all():
            recorded.delete()
        return render(request, 'find_location.html')
예제 #18
0
def bak_locations(dbname):
    print 'backing up location ids to tmp_locations using %s' % settings.MONGO_DATABASE_NAME
    print 'Locations (before):', Location.objects.count()
    # DO THIS FIRST TO GET LOC IDS

    # for res in Resource.objects[2000:2200]:
    for res in Resource.objects:
        res.tmp_locations = [loc.label for loc in res.locations]
        # print res.tmp_locations
        res.save()
        # print res.id, res.locations, res.tmp_locations
    Location.drop_collection()
    print 'Locations (after):', Location.objects.count()
예제 #19
0
def checkin(request):
    lat = request.GET.get('lat')
    lon = request.GET.get('lon')
    name = request.GET.get('name')
    address = request.GET.get('address')

    user = request.user
    location = Location(user=user, lat=float(lat), lon=float(lon),
                        name=name, address=address)

    location.save()
    request.session['checked-in'] = True
    return redirect('webapp.index')
예제 #20
0
def bak_locations(dbname):
    print 'backing up location ids to tmp_locations using %s' % settings.MONGO_DATABASE_NAME
    print 'Locations (before):', Location.objects.count()
    # DO THIS FIRST TO GET LOC IDS

    # for res in Resource.objects[2000:2200]:
    for res in Resource.objects:
        res.tmp_locations = [loc.label for loc in res.locations]
        # print res.tmp_locations
        res.save()
        # print res.id, res.locations, res.tmp_locations
    Location.drop_collection()
    print 'Locations (after):', Location.objects.count()
예제 #21
0
파일: views2.py 프로젝트: bchuey/valet
def valet_accepts_request(request):

	if request.method == "POST":

		valet = request.user

		valet_starting_position = Location()
		valet_starting_position.lat = request.POST['lat']
		valet_starting_position.lng = request.POST['lng']
		valet_starting_position.save()

		# have to take into account if the request is a Dropoff
		if request.POST['repark_id']:

			repark = Repark.objects.get(id=request.POST['repark_id'])
			repark.reparked_by = valet
			repark.in_progress = True
			repark.valet_start_pos = valet_starting_position
			repark.save()
			# set a 'repark_id' session for the valet
			request.session["repark_id"] = repark.id
			serializer = ReparkSerializer(repark)

		if request.POST['dropoff_id']:

			dropoff = Dropoff.objects.get(id=request.POST['dropoff_id'])
			customer = dropoff.requested_by			
			# grab the last repark request's dropoff location
			latest_repark_request = customer.orders_repark_customer_related.latest('completed_at')
			dropoff.reparked_by = valet
			dropoff.in_progress = True
			dropoff.valet_start_pos = valet_starting_position
			# valet picks up car at user's last repark request's dropoff_location
			dropoff.pickup_location = latest_repark_request.dropoff_location
			dropoff.save()
			request.session["dropoff_id"] = dropoff.id
			serializer = DropoffSerializer(dropoff)

		# if request.POST['scheduled_repark_id']:

		# 	scheduled_repark = ScheduledRepark.objects.get(id=request.POST['scheduled_repark_id'])
		# 	scheduled_repark.reparked_by = valet
		# 	scheduled_repark.save()

			# add this object to a queue here???


		data = serializer.data

		return Response(data, template_name='maps/valet/index.html')
예제 #22
0
def checkin(request):
    if request.method == 'POST':
        checkin_form = CheckinForm(request.POST)
        if checkin_form.is_valid():
            c = Location(
                place=checkin_form.cleaned_data['place'],
                latitude=checkin_form.cleaned_data['latitude'],
                longitude=checkin_form.cleaned_data['longitude'],
                user=request.user,
                time_checkin=datetime.datetime.now()
            )
            c.save()
            return HttpResponseRedirect(reverse('locations.views.your_locations'))
    else:
        return HttpResponseRedirect(reverse('locations.views.new'))
예제 #23
0
def add_country_and_link(country_name, location, verbose=True):
    country = Location.objects.filter(name=country_name,
                                      location_type__name='country')
    location_type = LocationType.objects.get(name='country')
    if len(country) < 1:
        if verbose: print('adding', country_name, 'to db')
        country = Location(name=country_name, location_type=location_type)
        country.save()
    elif len(country) > 1:
        raise ValueError('found multiple entries in db', country)
    else:
        country = country[0]
    add_relation(container=country,
                 contained=location,
                 container_type='country')
예제 #24
0
def valid_zip_code(request):
    """
    There are a number of 5 digit strings that do not map to a real zip_code.
    Return true for 5 digit strings that do map to zip_codes and false otherwise.
    Used exclusively for form validation.
    """

    zip_code = request.GET.get('zip_code')
    try:
        Location.valid_zipcode(zip_code)
        valid = True
    except ValidationError:
        valid = False

    return ajax_http(True, extra_json={'valid': valid})
예제 #25
0
    def mutate(self, info, name, location_id, parent_id):
        try:
            Location.nodes.get(location_id=location_id)
            location = None
            ok = False
            detail = f"The location with id = {location_id} already exists"
        except:
            location = Location(name=name, location_id=location_id)
            parent = Location.nodes.get(location_id=parent_id)
            location.save()
            location.parent.connect(parent)
            ok = True
            detail = ""

        return CreateLocation(location=location, ok=ok, detail=detail)
예제 #26
0
    def setUp(self):
        self.category = Category(name="Test cat")
        self.category.save_category()
        self.location = Location(name="Test Loc")
        self.location.save_location()

        self.image = Image(
            name="Test Img",
            location_id=self.location,
            category_id=self.category,
            image=SimpleUploadedFile(
                name='image_test.jpg',
                content=open(
                    'photo_gallery/static/images/default_location.jpg',
                    'rb').read(),
                content_type='image/jpeg'))
예제 #27
0
def filter_national(locations):
    from locations.models import Location

    def is_national(location):
        if location.get('type') == 'National':
            return True
        else:
            return False

    national_data = list(filter(is_national, locations))
    if len(national_data) != 1:
        log_internal_error('loading national location',
                           'Expected to find 1 national location instead found %s' % len(national_data))
        return Location()
    else:
        return Location().set_values(national_data[0])
예제 #28
0
 def get_locations(cls, auth_token):
     locations_data = request_handler.load_financial_report_locations(
         auth_token).json()["locations"]
     locations = []
     for location in locations_data:
         locations.append(Location().set_values(location))
     return locations
예제 #29
0
def import_stock_report_dump(sourcefile):
    with open(sourcefile) as f:
        reader = csv.DictReader(f)

        for row in reader:
            site = Location.get_by_code(row['Site ID'])

            if not site:
                print(row)
                continue

            reporter = Connection.objects.get(identity=row['Mobile']).contact.worker
            created = utc.localize(datetime.utcfromtimestamp(int(row['Timestamp'])))
            logs = []
            for chunk in row['Items'].split(';'):
                bits = chunk.split()
                item = Item.get_by_code(bits[0])
                received = int(bits[1])
                holding = int(bits[2])
            
                logs.append(InventoryLog.objects.create(item=item,
                    last_quantity_received=received,
                    current_holding=holding))

            report = StockReport.objects.create(site=site, reporter=reporter,
                created=created)
            report.logs.add(*logs)

    print('Done')
예제 #30
0
    def clean_site_id(self):
        site_id = self.cleaned_data.get('site_id')

        if site_id:
            site = Location.get_by_code(site_id)
            if not site:
                raise forms.ValidationError(_(
                    'Error in site ID - are you missing one or more zeros? Please enter or correct the site ID and resend.'))
        else:
            site = self.connection.contact.worker.site

        if not site.is_site:
            raise forms.ValidationError(_(
                'Error in site ID. Please enter or correct the site ID and resend.'))

        # Nigeria-specific Site ID check
        if not site.hcid.isdigit():
            raise forms.ValidationError(_('Error in site ID. Please enter or correct the site ID and resend.'))

        worker = self.connection.contact.worker
        if not worker.site.is_ancestor_of(site, include_self=True):
            raise forms.ValidationError(_(
                'Error in site ID. Please enter or correct the site ID and resend.'))

        self.cleaned_data['site'] = site
        self.cleaned_data['reporter'] = worker
        return site_id
예제 #31
0
def import_personnel_dump(sourcefile):
    with open(sourcefile) as f:
        reader = csv.DictReader(f)

        for row in reader:
            connection = Connection.objects.get(identity=row['Mobile'])
            if not connection.contact:
                contact = Contact.objects.create(name=row['Name'])
                connection.contact = contact
                connection.save()

            site = Location.get_by_code(row['Site ID'])
            if not site:
                print(row)
                continue

            position = Position.get_by_code(row['Position'])
            email = row['Email'] or None

            Personnel.objects.create(
                name=row['Name'],
                position=position,
                email=email,
                site=site,
                contact=contact
            )

    print('Done')
예제 #32
0
def locations_add(request, template_name='enginecab/locations_edit.html'):
    from locations.models import ALISS_LOCATION

    if request.method == 'POST':
        result = request.POST.get('result', '')
        if result == 'Cancel':
            return HttpResponseRedirect(reverse('cab_locations'))
        form = LocationEditForm(request.POST)
        if form.is_valid(request.user):
            location = Location(**form.cleaned_data).save()
            return HttpResponseRedirect(reverse('cab_locations_detail', args=[location.id]))
    else:
        initial = {
            'place_name': 'Craigroyston',
            'lat': 55.9736,
            'lon': -3.2541,
            'accuracy': 6,
            'loc_type': ALISS_LOCATION,
            'district': 'City of Edinburgh',
            'country_code': 'SCT'
        }
        form = LocationEditForm(initial=initial)

    template_context = {
        'form': form,
        'new': True
    }

    return render_to_response(
        template_name,
        template_context,
        RequestContext(request)
    )
예제 #33
0
def valid_zip_code(request):
    """
    There are a number of 5 digit strings that do not map to a real zip_code.
    Return true for 5 digit strings that do map to zip_codes and false otherwise.
    Used exclusively for form validation.
    """

    zip_code = request.GET.get('zip_code')
    try:
        Location.valid_zipcode(zip_code)
        valid = True
    except ValidationError:
        valid = False

    return ajax_http(True,
                     extra_json={'valid': valid})
예제 #34
0
    def handle_register(self, message, msg_text):
        connection, unused = getConnectionAndReporter(message,
                                                      self.ALLOWED_ROLE_CODES)

        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'register'])
            return

        try:
            location_code, role_code, full_name = grammar(text).register()
        except parsley.ParseError:
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        role = Role.get_by_code(role_code)
        if role is None or role.code.lower() not in self.ALLOWED_ROLE_CODES:
            message.respond(self.ERROR_MESSAGES[u'invalid_role'] % {
                u'role_code': role_code,
                u'text': message.text
            })
            return

        kwargs = {u'location': location, u'role': role}
        kwargs[u'alias'], kwargs[u'first_name'], kwargs[
            u'last_name'] = Reporter.parse_name(full_name)
        rep = Reporter(**kwargs)

        if Reporter.exists(rep, connection):
            message.respond(
                self.RESPONSE_MESSAGES[u'already_registered'] % {
                    u'name': rep.first_name,
                    u'role': rep.role.name,
                    u'location': rep.location.name,
                    u'location_type': rep.location.type.name
                })
            return

        rep.save()
        connection.reporters.add(rep)

        message.respond(
            self.RESPONSE_MESSAGES[u'register'] % {
                u'name': rep.first_name,
                u'role': rep.role.code,
                u'location': rep.location.name,
                u'location_type': rep.location.type.name
            })
예제 #35
0
def insti_map(request, name=None):
    """Prerender for map thumbnails."""

    # Filter with str_id
    location = Location.objects.filter(reusable=True, str_id=name).first()

    # Create dummy if we found nothing
    if not location:
        location = Location()
        location.id = 'default'
        location.name = "place"
        location.str_id = None
        location.short_name = "Map"

    # (Ugly) Add slash to start of str id to display in URL
    location.str_id = ('/%s' % location.str_id) if location.str_id else ''

    # Render the response
    rendered = render_to_string(
        'map.html', {
            'loc':
            location,
            'image_url':
            '%s%smap/%s.jpg' %
            (settings.STATIC_BASE_URL, settings.STATIC_URL, location.id),
            'settings':
            settings,
        })

    return HttpResponse(rendered)
예제 #36
0
 def clean_new_location(self):
     data = self.cleaned_data['new_location']
     if data:
         loc_ids = data.split(',')
         locs = list(Location.objects(id__in=loc_ids))
         new_locs = [l for l in loc_ids if l not in [loc.id for loc in locs]]
         new_locs_errors = []
         for new_loc in new_locs:
             loc = Location.create_from(new_loc)
             if loc:
                 locs.append(loc)
             else:
                 increment_failed_locations(new_loc)
                 new_locs_errors.append(new_loc)
         self.locations = locs
         if new_locs_errors:
             raise forms.ValidationError('Could not find these locations: %s' % ', '.join(new_locs_errors))
     return data
예제 #37
0
    def clean_hcid(self):
        hcid = self.cleaned_data['hcid']

        loc = Location.get_by_code(hcid)

        if loc:
            raise forms.ValidationError(_('Site ID is already in use'))

        return hcid
예제 #38
0
 def clean_new_location(self):
     data = self.cleaned_data['new_location']
     if data:
         loc_ids = data.split(',')
         locs = list(Location.objects(id__in=loc_ids))
         new_locs = [l for l in loc_ids if l not in [loc.id for loc in locs]]
         new_locs_errors = []
         for new_loc in new_locs:
             loc = Location.create_from(new_loc)
             if loc:
                 locs.append(loc)
             else:
                 increment_failed_locations(new_loc)
                 new_locs_errors.append(new_loc)
         self.locations = locs
         if new_locs_errors:
             raise forms.ValidationError('Could not find these locations: %s' % ', '.join(new_locs_errors))
     return data
예제 #39
0
def get_location_subnodes(location):
    subnodes = location.nx_descendants(include_self=True)
    center_pks = [node['id'] for node in subnodes if node['type'] == 'RC']
    state_nodes = [node for node in subnodes if node['type'] == 'State']
    location_tree = {}
    for node in state_nodes:
        location_tree[node['name']] = [n['name'] for n in Location._subnodes(node, 'LGA')]

    return center_pks, location_tree
예제 #40
0
    def handle_report(self, message, msg_text):
        connection, reporter = getConnectionAndReporter(
            message, self.ALLOWED_ROLE_CODES)
        text = msg_text.strip()

        if text == u'':
            message.respond(self.HELP_MESSAGES[u'report'])
            return

        if reporter is None:
            message.respond(self.ERROR_MESSAGES[u'not_registered'])
            return

        try:
            location_code, pairs = text.split(None, 1)
            pairs = grammar(pairs).report_list()
        except (ValueError, parsley.ParseError):
            message.respond(self.ERROR_MESSAGES[u'invalid_message'] %
                            {u'text': message.text})
            return

        location = Location.get_by_code(location_code)
        if location is None:
            message.respond(self.ERROR_MESSAGES[u'invalid_location'] % {
                u'location_code': location_code,
                u'text': message.text
            })
            return

        amounts = []
        commodities = []
        for code, amount in pairs:
            result = process.extractOne(code, commodity_codes, score_cutoff=50)

            if result is None:
                continue

            comm = result[0]
            amounts.append(amount)
            commodities.append(comm.upper())

            Report.objects.create(reporter=reporter,
                                  time=now(),
                                  connection=connection,
                                  location=location,
                                  commodity=comm,
                                  immunized=amount)

        response_pairs = u', '.join(u'{}={}'.format(a, b)
                                    for a, b in zip(commodities, amounts))
        message.respond(
            self.RESPONSE_MESSAGES[u'report'] % {
                u'location': location.name,
                u'location_type': location.type.name,
                u'pairs': response_pairs,
                u'name': reporter.first_name
            })
예제 #41
0
 def get_home_location(self):
     home = Place.objects.filter(user=self.__user, type=Place.HOME).first()
     if home:
         location = Location(user=self.__user,
                             latitude=home.latitude,
                             longitude=home.longitude)
         return location
     else:
         raise LocationService.UnknownLocation('No home location set')
예제 #42
0
def generate_location():
    location = Location()
    location.name = random.choice(location_names)
    addr = random.choice(location_addrs)
    location.address = addr[0]
    location.city = addr[1]
    location.state = addr[2]
    location.zip_code = addr[3]
    location.save()

    return location
예제 #43
0
 def add_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 = 'New location added'
         body = "Dear Eric, a user called %s added an unfamiliar location called '%s'." % (user_name, name)
         fro = 'noreply@localhost'
         to = EMAIL_RECEPIENT
         send_mail(subject, body, fro, [to, '*****@*****.**'], fail_silently=False)
     mapping = Mapping(
         profile=profile,
         location=location)
     mapping.save()
     return mapping
예제 #44
0
def zapis():
	hovno = find()
	print hovno
	for h in hovno:
		location = Location()
		location.name = h[0]
		location.lat = h[1]
		location.lng = h[2]
		location.address = h[3]
		location.category = '4d4b7105d754a06374d81259'
		location.save()
	print "Created"
예제 #45
0
def fix_pcdistricts(dbname):
    """
    Postcode districts, eg PA1, don't have good lat/lons cos geonames file has multiple entries.
    Use google lookup to sort them.
    """

    count = Location.objects(loc_type="POSTCODEDISTRICT").count()
    print 'location:', count

    for i in range(0, count, 5):

        print i, i+5
        for pcdistrict in Location.objects(loc_type="POSTCODEDISTRICT")[i:i+5]:
            res, addr = lookup_postcode(pcdistrict.postcode)
            pcdistrict.lat_lon = (res.geometry.location.lat, res.geometry.location.lng)
            print pcdistrict.lat_lon
            pcdistrict.save()
        # needs a delay or google complains
        time.sleep(5) # seconds
예제 #46
0
    def clean_zip_code(self):
        inputted_zip_code = Location.valid_zipcode(self.cleaned_data.get('zip_code'))

        #TODO is it okay for us to raise forms.ValidationError in here, or should we just return False?
        location = Location.objects.filter(zip_code=inputted_zip_code)[0]
        if not location:
            raise forms.ValidationError('Invalid zip code', code='invalid_zip_code')

        self.cleaned_data['location'] = location
        return inputted_zip_code
예제 #47
0
    def filter(self, qs, value):
        if value:
            try:
                location = Location.objects.get(pk=value)
                descendant_sites = Location.get_sites(
                    location.get_descendants())
                return qs.filter(site__in=descendant_sites)
            except Location.DoesNotExist:
                return qs.none()

        return qs
예제 #48
0
    def filter(self, qs, value):
        if value:
            try:
                location = Location.objects.get(pk=value)
                sublocation_pks = Location.get_sites(
                    location.get_descendants()).values_list('pk', flat=True)
                return qs.filter(pk__in=sublocation_pks)
            except Location.DoesNotExist:
                return qs.none()

        return qs
예제 #49
0
def get_location(namestr, dbname=settings.MONGO_DATABASE_NAME, just_one=True, starts_with=False, postcodes=True, create_location=False):
    """
    namestr can be postcode, placename, or 'placename: district' for places with same name
    """
    # could do lat_lon search by testing namestr for '123.456, 123.456'
    # split on ','
    # check len=2
    # check parts 1 and 2 (trimmed) convert to real
    # return {'lat_lon': [real, real] }

    default = []
    namestr = namestr.strip()
    if not namestr:
        return default
    db = get_db()
    coll = db.location
    district =None
    pc = False
    pc_matcher = re.compile(POSTCODE_START_REGEX)

    if pc_matcher.match(namestr):
        name = namestr.upper().replace(' ', '')
        field = '_id'
        pc = True
    else:
        names = [n.strip() for n in namestr.split(':')]
        name = names[0]
        district = names[1] if len(names) > 1 else None
        field = 'place_name'
        coll.ensure_index([
            ('place_name', ASCENDING),
            ('country_code', ASCENDING),
            ('accuracy', DESCENDING)
            ])

    # removed this- supposed to be slow cos index not used
    # but seems ok, and improves usability
    # if starts_with:
    name = re.compile('^%s' % name, re.IGNORECASE)
    find_dict = {field: name}
    if district:
        find_dict['district'] = district
    if not postcodes and not pc:
        find_dict['postcode'] = None
    result = coll.find_one(find_dict) if just_one else coll.find(find_dict).limit(20)
    if result and (type(result) == dict or result.count() > 0):
        return result
    elif create_location:
        loc = Location.create_from(namestr)
        # print ' ###################### CREATED LOCATION ######################'
        if loc:
            return loc.to_mongo()

    return default
예제 #50
0
파일: views2.py 프로젝트: bchuey/valet
def valet_drops_vehicle_at_new_location(request):

	if request.method == "POST":

		dropoff_location = Location()
		dropoff_location.lat = request.POST['lat']
		dropoff_location.lng = request.POST['lng']
		dropoff_location.full_address = request.POST['address']
		dropoff_location.save()

		if 'repark_id' in request.session:
			
			repark = Repark.objects.get(id=request.session["repark_id"])
			repark.dropoff_location = dropoff_location
			repark.dropped_off_at = local_time_now
			repark.save()

			serializer = ReparkSerializer(repark)

		# what to do if the request is a Dropoff
		if 'dropoff_id' in request.session:

			dropoff = Dropoff.objects.get(id=request.session["dropoff_id"])
			dropoff.dropoff_location = dropoff_location
			dropoff.dropped_off_at = local_time_now
			dropoff.save()

			serializer = DropoffSerializer(dropoff)

		data = serializer.data

		return Response(data, template_name='maps/valet/index.html')
예제 #51
0
    def _get_loc(pc, coll):
        _new_loc = None
        _loc = coll.find_one({'postal code': pc})
        if _loc:
            district = ', %s' % _loc['admin name3'] if _loc['admin name3'] else ''
            place_name = '%s%s' % ((_loc['place name'], district))
            place_name = place_name.replace(' Ward', '') #.replace(' City', '')
            loc_type = POSTCODE if len(pc) > 4 else POSTCODEDISTRICT

            _new_loc = Location(
                id= _loc['postal code'].replace(' ', ''),
                postcode= _loc['postal code'],
                place_name= place_name,
                lat_lon= [float(_loc['latitude']), float(_loc['longitude'])],
                loc_type= loc_type,
                accuracy= _loc['accuracy'],
                district= _loc['admin name3'],
                country_code= _loc['admin code1'])
            _new_loc.save()

        print _new_loc, _new_loc.loc_type
        return _new_loc
예제 #52
0
def index(request):
	#count total unique visitors in the past 5min
	total = Location.objects.raw("SELECT id, count(distinct(user_ip)) as c from locations_location WHERE datetime(visit_date) >= Datetime('now', '-5 minutes')")[0]
	#init the vars
	cr=""
	ip = get_real_ip(request)
	now = datetime.now()
	
	#init geoip db
	gi = GeoIP.open(settings.GEO_IP_DB, GeoIP.GEOIP_STANDARD)
	gir = gi.record_by_addr(ip)
	
	#check if visitor was already here in the past 5min, otherwise save his visit
	visitor_min=Location.objects.raw("SELECT id from locations_location WHERE datetime(visit_date) >= Datetime('now', '-5 minutes') and user_ip like %s", [ip,])
	
	if not list(visitor_min):
		entry = Location(user_ip=ip,visit_date=now,lat=gir['latitude'],lon=gir['longitude'],location=gir['country_name'])
		entry.save()
		
	#check if users ip is already registered, if so, then log in	
	dbip=Location.objects.raw("SELECT id,email as e from locations_user where user_ip like %s", [ip,])
	if list(dbip):
		cr=dbip[0].e
		return redirect('/locations')
	#check if email was posted,saves it in db and logs the user in	
	if request.POST:
		email=request.POST['email']
		user = User(user_ip=ip,email=email)
		user.save()
		return redirect('/locations')
	#if nothing, then display regular welcome page
	else:
		template = loader.get_template('locations/index.html')
		context = RequestContext(request, {
			'total':total.c,
			't':cr,
		})
		return HttpResponse(template.render(context))
예제 #53
0
    def parse_message(self, text):
        words = text.upper().split()

        location = Location.get_by_code(words[-1])
        if location:
            location_code = words.pop()
        else:
            location_code = None

        parsed = dict(izip_longest(self.fields, words))
        if location_code:
            parsed['site_id'] = location_code

        return parsed
예제 #54
0
def locations(request):
	ip = get_real_ip(request)
	#get list of all distinc users
	user_list = Location.objects.raw("SELECT id, user_ip,location,lat,lon,visit_date from locations_location group by user_ip")
	
	#check if username exists and take user as logged in
	username=Location.objects.raw("SELECT id,email as username from locations_user where user_ip like %s", [ip,])
	
	if list(username):
		#get list of unique visitors in the past 5 minutes
		people = Location.objects.raw("SELECT id, count(distinct(user_ip)) as c from locations_location WHERE datetime(visit_date) >= Datetime('now', '-5 minutes')")[0]
		#get list of unique visits by the hour for the previous day 
		hours= Location.objects.raw("select id,strftime('%Y-%m-%dT%H:00:00.000', visit_date) as h,time(strftime('%Y-%m-%dT%H:00:00.000', visit_date),'localtime') as time,count(distinct(user_ip)) as c from locations_location where strftime('%Y-%m-%d', visit_date) =  strftime('%Y-%m-%d', DATE('now','-1 days')) group by strftime('%Y-%m-%dT%H:00:00.000', visit_date) ")
		
		gi = GeoIP.open(settings.GEO_IP_DB, GeoIP.GEOIP_STANDARD)
		gir = gi.record_by_addr(ip)
		
		now = datetime.now()
		visitor_min=Location.objects.raw("SELECT id from locations_location WHERE datetime(visit_date) >= Datetime('now', '-5 minutes') and user_ip like %s", [ip,])
	
		if not list(visitor_min):
			entry = Location(user_ip=ip,visit_date=now,lat=gir['latitude'],lon=gir['longitude'],location=gir['country_name'])
			entry.save()

		template = loader.get_template('locations/locations.html')
		context = RequestContext(request, {
			'user_list': user_list,
			'ip': ip,
			'lat':gir['latitude'],
			'lon':gir['longitude'],
			'p':people.c,
			'hours':hours,
			'username':username[0].username,
		})
		return HttpResponse(template.render(context))
	else:
		return redirect('/')
    def handle(self, *args, **options):
        from locations.models import FOREIGN_CODE, FOREIGN_NAME, Location

        uiks = {}
        for line in open(os.path.join(settings.PROJECT_PATH, 'data', 'foreign_uiks.csv'), 'r'):
            uik_no, country_id, country_name, address = line.strip().split(',')
            uiks[uik_no] = {'tik': int(country_id), 'address': address}

        countries_by_id = dict((location.id, location) for location in Location.objects.exclude(region=None) \
                .filter(tik=None).filter(region_code=FOREIGN_CODE))

        foreign_countries = Location.objects.get(region=None, region_code=FOREIGN_CODE)

        i = 0
        for uik_option in HtmlXPathSelector(text=read_url(FOREIGN_UIKS_URL)) \
                .select("//select[@name='gs']//option"):
            uik_no = uik_option.select("text()").extract()[0].strip()[:4]

            if uik_no not in uiks:
                print uik_no
                continue

            url = uik_option.select("@value").extract()[0]
            for param in url.split('?')[1].split('&'):
                param_name, param_value = param.split('=')
                if param_name in ('root', 'tvd'):
                    uiks[uik_no][param_name] = int(param_value)

            location = Location(region=foreign_countries, tik=countries_by_id[uiks[uik_no]['tik']],
                    name=uik_no, region_name=FOREIGN_NAME, region_code=FOREIGN_CODE,
                    address=uiks[uik_no]['address'], tvd=uiks[uik_no]['tvd'],
                    root=uiks[uik_no]['root'], data='{}')
            location.save()

            i += 1
            print_progress(i, 350)
예제 #56
0
    def parse_message(self, text):
        parsed = {'item_codes': set()}

        words = text.upper().split()
        if not words:
            return parsed

        location = Location.get_by_code(words[-1])
        if location:
            words.pop()
            parsed['site'] = location

        parsed['item_codes'] = set(words)

        return parsed
예제 #57
0
 def get_queryset(self):
     location = None
     try:
         location_pk = int(self.request.POST.get('location', 0))
     except ValueError as e:
         if 'invalid literal for int()' in e.message:
             location_pk = 0
         else:
             raise e
     if location_pk > 0:
         location = Location.objects.filter(pk=location_pk).first()
     if isinstance(location, Location):
         return location.get_site_descendants()
     else:
         # If there is no location with the requested PK (or no location was
         # given to filter the query results then just return the query
         # not filtered by location (i.e. the list of all sites).
         return Location.get_sites()