Example #1
0
def make_instance(name=None, is_public=False, url_name=None, point=None,
                  edge_length=600):
    if name is None:
        max_instance = Instance.objects.all().aggregate(
            Max('id'))['id__max'] or 0
        name = 'generated$%d' % (max_instance + 1)

    if url_name is None:
        max_instance = Instance.objects.all().aggregate(
            Max('id'))['id__max'] or 0
        url_name = 'generated-%d' % (max_instance + 1)

    p1 = point or Point(0, 0)

    instance = Instance(name=name, geo_rev=0,
                        is_public=is_public, url_name=url_name)

    instance.seed_with_dummy_default_role()

    d = edge_length / 2
    instance.bounds = InstanceBounds.create_from_point(p1.x, p1.y, d)
    instance.save()

    new_role = Role.objects.create(
        name='role-%s' % name, instance=instance,
        rep_thresh=0, default_permission_level=FieldPermission.READ_ONLY)

    instance.default_role = new_role
    instance.save()

    return instance
Example #2
0
def make_instance(name=None, is_public=False, url_name=None, point=None,
                  edge_length=600):
    if name is None:
        max_instance = Instance.objects.all().aggregate(
            Max('id'))['id__max'] or 0
        name = 'generated$%d' % (max_instance + 1)

    if url_name is None:
        max_instance = Instance.objects.all().aggregate(
            Max('id'))['id__max'] or 0
        url_name = 'generated-%d' % (max_instance + 1)

    p1 = point or Point(0, 0)

    instance = Instance(name=name, geo_rev=0,
                        is_public=is_public, url_name=url_name)

    instance.seed_with_dummy_default_role()

    d = edge_length / 2
    instance.bounds = InstanceBounds.create_from_point(p1.x, p1.y, d)
    instance.save()

    new_role = Role.objects.create(
        name='role-%s' % name, instance=instance,
        rep_thresh=0, default_permission=FieldPermission.READ_ONLY)

    instance.default_role = new_role
    instance.save()

    return instance
Example #3
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception(
                'Expected instance name as the first argument')

        name = args[0]

        if not options['user']:
            raise Exception('An admin user must be specified. with "--user"')

        if options.get('center', None) and options.get('geojson', None):
            raise Exception('You must specifiy only one of '
                            '"center" and "geojson"')
        elif (not options.get('center', None) and
              not options.get('geojson', None)):
            raise Exception('You must specifiy at least one of '
                            '"center" and "geojson"')

        if options['center']:
            center = options['center'].split(',')
            if len(center) != 2:
                raise Exception('Center should be a lon,lat pair')

            center_pt = Point(float(center[0]), float(center[1]), srid=4326)

            # Bounding box built in web mercator to have units in meters
            center_pt.transform(3857)
            x = center_pt.x
            y = center_pt.y
            instance_bounds = InstanceBounds.create_from_point(x, y)
        else:
            geom = GEOSGeometry(open(options['geojson'], srid=4326).read())
            instance_bounds = InstanceBounds.objects.create(geom=geom)

        if not options.get('url_name', None):
            raise Exception('You must specify a "url_name" starting with a '
                            'lowercase letter and containing lowercase '
                            'letters, numbers, and dashes ("-")')
        url_name = options.get('url_name')

        instance = Instance(
            config={},
            name=name,
            bounds=instance_bounds,
            is_public=True,
            url_name=url_name)

        instance.seed_with_dummy_default_role()
        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(
            geom__intersects=instance_bounds.geom)

        role = Role.objects.create(
            name='user', instance=instance, rep_thresh=0,
            default_permission=FieldPermission.WRITE_DIRECTLY)

        create_stewardship_udfs(instance)

        add_species_to_instance(instance)

        add_default_permissions(instance, roles=[role])

        eco_benefits_conversion = \
            BenefitCurrencyConversion.get_default_for_point(Point(x, y))
        if eco_benefits_conversion:
            eco_benefits_conversion.save()

        instance.eco_benefits_conversion = eco_benefits_conversion

        instance.default_role = role
        instance.save()

        user = User.objects.get(username=options['user'])
        InstanceUser(
            instance=instance,
            user=user,
            role=role,
            admin=True).save_with_user(user)
Example #4
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception("Expected instance name as the first argument")

        name = args[0]

        if not options["user"]:
            logger.warning(
                "An admin user was not specified. While not a "
                "problem initially, no users will be able to "
                "modify many parts of this instance. It is "
                "recommended that you create a user first and call "
                'this command with "--user"'
            )

        if options.get("center", None) and options.get("geojson", None):
            raise Exception("You must specifiy only one of " '"center" and "geojson"')
        elif not options.get("center", None) and not options.get("geojson", None):
            raise Exception("You must specifiy at least one of " '"center" and "geojson"')

        if options["center"]:
            center = options["center"].split(",")
            if len(center) != 2:
                raise Exception("Center should be an x,y pair in EPSG3857")

            x = int(center[0])
            y = int(center[1])
            offset = 50000
            bounds = Polygon(
                (
                    (x - offset, y - offset),
                    (x - offset, y + offset),
                    (x + offset, y + offset),
                    (x + offset, y - offset),
                    (x - offset, y - offset),
                )
            )

            bounds = MultiPolygon((bounds,))
        else:
            bounds = GEOSGeometry(open(options["geojson"]).read())

        if not options.get("url_name", None):
            raise Exception(
                'You must specify a "url_name" starting with a '
                "lowercase letter and containing lowercase "
                'letters, numbers, and dashes ("-")'
            )
        url_name = options.get("url_name")

        # Instances need roles and roles needs instances... crazy
        # stuff we're going to create the needed role below however,
        # we'll temporarily use a 'dummy role'. The dummy role has
        # no instance.
        dummy_roles = Role.objects.filter(instance__isnull=True)
        if len(dummy_roles) == 0:
            dummy_role = Role.objects.create(name="empty", rep_thresh=0)
        else:
            dummy_role = dummy_roles[0]

        instance = Instance(
            config={}, name=name, bounds=bounds, is_public=True, default_role=dummy_role, url_name=url_name
        )

        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(geom__intersects=bounds)

        role = Role.objects.create(
            name="user", instance=instance, rep_thresh=0, default_permission=FieldPermission.WRITE_DIRECTLY
        )

        add_default_permissions(instance, roles=[role])

        instance.default_role = role
        instance.save()

        user = User.objects.get(pk=options["user"])
        InstanceUser(instance=instance, user=user, role=role, admin=True).save_with_user(user)
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception('Expected instance name as the first argument')

        name = args[0]

        if not options['user']:
            raise Exception('An admin user must be specified. with "--user"')

        if options.get('center', None) and options.get('geojson', None):
            raise Exception('You must specifiy only one of '
                            '"center" and "geojson"')
        elif (not options.get('center', None)
              and not options.get('geojson', None)):
            raise Exception('You must specifiy at least one of '
                            '"center" and "geojson"')

        if options['center']:
            center = options['center'].split(',')
            if len(center) != 2:
                raise Exception('Center should be a lon,lat pair')

            center_pt = Point(float(center[0]), float(center[1]), srid=4326)

            # Bounding box built in web mercator to have units in meters
            center_pt.transform(3857)
            x = center_pt.x
            y = center_pt.y
            instance_bounds = InstanceBounds.create_from_point(x, y)
        else:
            geom = GEOSGeometry(open(options['geojson'], srid=4326).read())
            instance_bounds = InstanceBounds.objects.create(geom=geom)

        if not options.get('url_name', None):
            raise Exception('You must specify a "url_name" starting with a '
                            'lowercase letter and containing lowercase '
                            'letters, numbers, and dashes ("-")')
        url_name = options.get('url_name')

        instance = Instance(config={},
                            name=name,
                            bounds=instance_bounds,
                            is_public=True,
                            url_name=url_name)

        instance.seed_with_dummy_default_role()
        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(
            geom__intersects=instance_bounds.geom)

        role = Role.objects.create(
            name='user',
            instance=instance,
            rep_thresh=0,
            default_permission_level=FieldPermission.WRITE_DIRECTLY)

        create_stewardship_udfs(instance)

        add_species_to_instance(instance)

        add_default_permissions(instance, roles=[role])
        add_instance_permissions([role])

        eco_benefits_conversion = \
            BenefitCurrencyConversion.get_default_for_instance(instance)
        if eco_benefits_conversion:
            eco_benefits_conversion.save()
            instance.eco_benefits_conversion = eco_benefits_conversion

        instance.default_role = role
        instance.save()

        user = User.objects.get(username=options['user'])
        InstanceUser(instance=instance, user=user, role=role,
                     admin=True).save_with_user(user)
Example #6
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception('Expected instance name as the first argument')

        name = args[0]

        if not options['user']:
            logger.warning('An admin user was not specified. While not a '
                           'problem initially, no users will be able to '
                           'modify many parts of this instance. It is '
                           'recommended that you create a user first and call '
                           'this command with "--user"')

        if options.get('center', None) and options.get('geojson', None):
            raise Exception('You must specifiy only one of '
                            '"center" and "geojson"')
        elif (not options.get('center', None)
              and not options.get('geojson', None)):
            raise Exception('You must specifiy at least one of '
                            '"center" and "geojson"')

        if options['center']:
            center = options['center'].split(',')
            if len(center) != 2:
                raise Exception('Center should be an x,y pair in EPSG3857')

            x = int(center[0])
            y = int(center[1])
            offset = 50000
            bounds = Polygon(
                ((x - offset, y - offset), (x - offset, y + offset),
                 (x + offset, y + offset), (x + offset, y - offset),
                 (x - offset, y - offset)))

            bounds = MultiPolygon((bounds, ))
        else:
            bounds = GEOSGeometry(open(options['geojson']).read())

        if not options.get('url_name', None):
            raise Exception('You must specify a "url_name" starting with a '
                            'lowercase letter and containing lowercase '
                            'letters, numbers, and dashes ("-")')
        url_name = options.get('url_name')

        # Instances need roles and roles needs instances... crazy
        # stuff we're going to create the needed role below however,
        # we'll temporarily use a 'dummy role'. The dummy role has
        # no instance.
        dummy_roles = Role.objects.filter(instance__isnull=True)
        if len(dummy_roles) == 0:
            dummy_role = Role.objects.create(name='empty', rep_thresh=0)
        else:
            dummy_role = dummy_roles[0]

        instance = Instance(config={},
                            name=name,
                            bounds=bounds,
                            is_public=True,
                            default_role=dummy_role,
                            url_name=url_name)

        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(geom__intersects=bounds)

        role = Role.objects.create(
            name='user',
            instance=instance,
            rep_thresh=0,
            default_permission=FieldPermission.WRITE_DIRECTLY)

        add_default_permissions(instance, roles=[role])

        instance.default_role = role
        instance.save()

        user = User.objects.get(pk=options['user'])
        InstanceUser(instance=instance, user=user, role=role,
                     admin=True).save_with_user(user)
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception('Expected instance name as the first argument')

        name = args[0]

        if not options['user']:
            raise Exception('An admin user must be specified. with "--user"')

        if options.get('center', None) and options.get('geojson', None):
            raise Exception('You must specifiy only one of '
                            '"center" and "geojson"')
        elif (not options.get('center', None)
              and not options.get('geojson', None)):
            raise Exception('You must specifiy at least one of '
                            '"center" and "geojson"')

        if options['center']:
            center = options['center'].split(',')
            if len(center) != 2:
                raise Exception('Center should be a lat,lng pair in SRID 4326')

            center_pt = Point(float(center[0]), float(center[1]), srid=4326)

            # Bounding box built in web mercator to have units in meters
            center_pt.transform(3857)
            x = center_pt.x
            y = center_pt.y
            offset = 50000
            bounds = Polygon(
                ((x - offset, y - offset), (x - offset, y + offset),
                 (x + offset, y + offset), (x + offset, y - offset),
                 (x - offset, y - offset)))

            bounds = MultiPolygon((bounds, ))
        else:
            bounds = GEOSGeometry(open(options['geojson'], srid=4326).read())

        if not options.get('url_name', None):
            raise Exception('You must specify a "url_name" starting with a '
                            'lowercase letter and containing lowercase '
                            'letters, numbers, and dashes ("-")')
        url_name = options.get('url_name')

        instance = Instance(config={},
                            name=name,
                            bounds=bounds,
                            is_public=True,
                            url_name=url_name)

        instance.seed_with_dummy_default_role()
        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(geom__intersects=bounds)

        role = Role.objects.create(
            name='user',
            instance=instance,
            rep_thresh=0,
            default_permission=FieldPermission.WRITE_DIRECTLY)

        add_default_permissions(instance, roles=[role])

        instance.default_role = role
        instance.save()

        user = User.objects.get(username=options['user'])
        InstanceUser(instance=instance, user=user, role=role,
                     admin=True).save_with_user(user)
Example #8
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise Exception(
                'Expected instance name as the first argument')

        name = args[0]

        if not options['user']:
            raise Exception('An admin user must be specified. with "--user"')

        if options.get('center', None) and options.get('geojson', None):
            raise Exception('You must specifiy only one of '
                            '"center" and "geojson"')
        elif (not options.get('center', None) and
              not options.get('geojson', None)):
            raise Exception('You must specifiy at least one of '
                            '"center" and "geojson"')

        if options['center']:
            center = options['center'].split(',')
            if len(center) != 2:
                raise Exception('Center should be a lat,lng pair in SRID 4326')

            center_pt = Point(float(center[0]), float(center[1]), srid=4326)

            # Bounding box built in web mercator to have units in meters
            center_pt.transform(3857)
            x = center_pt.x
            y = center_pt.y
            offset = 50000
            bounds = Polygon(((x - offset, y - offset),
                              (x - offset, y + offset),
                              (x + offset, y + offset),
                              (x + offset, y - offset),
                              (x - offset, y - offset)))

            bounds = MultiPolygon((bounds, ))
        else:
            bounds = GEOSGeometry(open(options['geojson'], srid=4326).read())

        if not options.get('url_name', None):
            raise Exception('You must specify a "url_name" starting with a '
                            'lowercase letter and containing lowercase '
                            'letters, numbers, and dashes ("-")')
        url_name = options.get('url_name')

        instance = Instance(
            config={},
            name=name,
            bounds=bounds,
            is_public=True,
            url_name=url_name)

        instance.seed_with_dummy_default_role()
        instance.full_clean()
        instance.save()

        instance.boundaries = Boundary.objects.filter(
            geom__intersects=bounds)

        role = Role.objects.create(
            name='user', instance=instance, rep_thresh=0,
            default_permission=FieldPermission.WRITE_DIRECTLY)

        add_default_permissions(instance, roles=[role])

        instance.default_role = role
        instance.save()

        user = User.objects.get(username=options['user'])
        InstanceUser(
            instance=instance,
            user=user,
            role=role,
            admin=True).save_with_user(user)