Пример #1
0
class SpotAdmin(admin.ModelAdmin):
    """ The admin model for a Spot.
    The ETag is excluded because it is generated on Spot save.
    """
    form = SpotForm.implementation()

    list_display = ("name", "building_name", "floor", "room_number",
                    "capacity", "organization", "manager")
    list_filter = ["spottypes", "building_name", "organization", "manager"]

    actions = ['delete_model']

    def get_actions(self, request):
        actions = super(SpotAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    def delete_model(self, request, spots):
        if type(spots) is Spot:
            spots.delete()
        else:
            for spot in spots.all():
                spot.delete()

    delete_model.short_description = "Delete selected spots"
Пример #2
0
    def GET(self, request):
        """ Json data that should contain every single piece of information that any spot might contain.
            The keys will be what info spots might contains, and the values will be what the possible
            types are for the actual values. If there is a list of values (even only a list of size 1)
            those are the only values that will pass validations.
        """
        schema = {
            "uri": "auto",
            "available_hours": "hours_string",
            "location": {},
            "type": [],
            "extended_info": {},
            "images": [],
        }

        location_descriptors = [
            "latitude", "longitude", "height_from_sea_level", "building_name",
            "floor", "room_number"
        ]

        internal_type_map = {
            "AutoField": "int",
            "BigIntegerField": "int",
            "BooleanField": "boolean",
            "CharField": "unicode",
            "CommaSeparatedIntegerField": "unicode",
            "DateField": "date",
            "DateTimeField": "datetime",
            "DecimalField": "decimal",
            "EmailField": "unicode",
            "FileField": "unicode",
            "FilePathField": "unicode",
            "FloatField": "float",
            "ForeignKey": "unicode",
            "ImageField": "unicode",
            "IntegerField": "int",
            "IPAddressField": "unicode",
            "GenericIPAddressField": "unicode",
            "ManyToManyField": "unicode",
            "NullBooleanField": "boolean",
            "OneToOneField": "unicode",
            "PositiveIntegerField": "int",
            "PositiveSmallIntegerField": "int",
            "SlugField": "unicode",
            "SmallIntegerField": "int",
            "TextField": "unicode",
            "TimeField": "time",
            "URLField": "url",
        }

        # To grab regular spot info
        spot_field_array = models.get_model('spotseeker_server',
                                            'Spot')._meta.fields
        for field in spot_field_array:
            if field.auto_created or not field.editable or field.name == "etag":  # pk (id), auto_now=True, auto_now_add=True, and "etag"
                schema.update({field.name: "auto"})
            elif field.get_internal_type() in internal_type_map:
                if field.name in location_descriptors:
                    schema["location"].update({
                        field.name:
                        internal_type_map[field.get_internal_type()]
                    })
                else:
                    schema.update({
                        field.name:
                        internal_type_map[field.get_internal_type()]
                    })
            else:
                if field.name in location_descriptors:
                    schema["location"].update(
                        {field.name: field.get_internal_type()})
                else:
                    schema.update({field.name: field.get_internal_type()})

        # To grab spot image info
        spot_image_field_array = models.get_model('spotseeker_server',
                                                  'SpotImage')._meta.fields
        schema_image = {}
        for field in spot_image_field_array:
            if field.auto_created or not field.editable or field.name == "etag":  # pk (id), auto_now=True, auto_now_add=True, and "etag"
                schema_image.update({field.name: "auto"})
            elif field.get_internal_type() in internal_type_map:
                schema_image.update(
                    {field.name: internal_type_map[field.get_internal_type()]})
            else:
                schema_image.update({field.name: field.get_internal_type()})
        schema["images"].append(schema_image)

        # To grab all of the different spot types
        for spot_type in SpotType.objects.all():
            schema["type"].append(spot_type.name)

        # To grab all of the extended info
        try:
            validated_ei = SpotForm.implementation().validated_extended_info
            org_form_exists = True
        except:
            org_form_exists = False
        for key_dict in SpotExtendedInfo.objects.values("key").distinct():
            key = key_dict["key"]
            if org_form_exists and key in validated_ei:
                schema["extended_info"].update({key: validated_ei[key]})
            else:
                schema["extended_info"].update({key: "unicode"})

        return JSONResponse(schema)
Пример #3
0
    def build_and_save_from_input(self, request, spot):
        body = request.read()
        try:
            json_values = json.loads(body)
        except Exception as e:
            raise RESTException("Unable to parse JSON", status_code=400)

        partial_update = False
        stash = {}
        is_new = spot is None

        spot_pre_build.send(
            sender=SpotForm.implementation(),
            request=request,
            json_values=json_values,
            spot=spot,
            partial_update=partial_update,
            stash=stash
        )

        self._build_spot_types(json_values, spot, partial_update)
        self._build_spot_location(json_values)

        spot_pre_save.send(
            sender=SpotForm.implementation(),
            request=request,
            json_values=json_values,
            spot=spot,
            partial_update=partial_update,
            stash=stash
        )

        # Remve excluded fields
        excludefields = set(SpotForm.implementation().Meta.exclude)
        for fieldname in excludefields:
            if fieldname in json_values:
                del json_values[fieldname]

        if spot is not None and partial_update:
            # Copy over the existing values
            for field in spot._meta.fields:
                if field.name in excludefields:
                    continue
                if field.name not in json_values:
                    json_values[field.name] = getattr(spot, field.name)

            # spottypes is not included in the above copy, do it manually
            if 'spottypes' not in json_values:
                json_values['spottypes'] = [t.pk for t in spot.spottypes.all()]

        form = SpotForm(json_values, instance=spot)
        if not form.is_valid():
            raise RESTFormInvalidError(form)

        spot = form.save()

        spot_post_save.send(
            sender=SpotForm.implementation(),
            request=request,
            spot=spot,
            partial_update=partial_update,
            stash=stash
        )

        # gets the current etag
        spot = Spot.get_with_external(spot.pk)

        if is_new:
            response = HttpResponse(status=201)
            response['Location'] = spot.rest_url()
        else:
            response = JSONResponse(spot.json_data_structure(), status=200)
        response["ETag"] = spot.etag

        spot_post_build.send(
            sender=SpotForm.implementation(),
            request=request,
            response=response,
            spot=spot,
            partial_update=partial_update,
            stash=stash
        )

        return response
Пример #4
0
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver

from geoposition import Geoposition
from geoposition.forms import GeopositionField
import logging
from spotseeker_server.admin import SpotAdmin
from spotseeker_server.forms.spot import SpotForm as SSSpotForm, SpotExtendedInfoForm as SSSpotExtendedInfoForm
from spotseeker_server.models import SpotAvailableHours, SpotImage, SpaceReview
import re

from .models import UIUCSpot, UIUCSpaceReview, HostAuthRule

logger = logging.getLogger(__name__)

SpotForm = SSSpotForm.implementation()
SpotExtendedInfoForm = SSSpotExtendedInfoForm.implementation()

SPOT_BOOLEAN_FIELDS = (
    'has_whiteboards',
    'has_outlets',
    'has_printing',
    'has_scanner',
    'has_displays',
    'has_projector',
    'has_computers',
    'has_natural_light',
    'reservable',
    )

SPOT_INTEGER_FIELDS = (
Пример #5
0
    def GET(self, request):
        """ Json data that should contain every single piece of information
            that any spot might contain.

            The keys will be what info spots might contains, and the values
            will be what the possible types are for the actual values. If
            there is a list of values (even only a list of size 1)
            those are the only values that will pass validations.
        """
        schema = {
            "uri": "auto",
            "available_hours": "hours_string",
            "location": {},
            "type": [],
            "extended_info": {},
            "images": [],
        }

        location_descriptors = [
            "latitude",
            "longitude",
            "height_from_sea_level",
            "building_name",
            "floor",
            "room_number"
        ]

        internal_type_map = {
            "AutoField": "int",
            "BigIntegerField": "int",
            "BooleanField": "boolean",
            "CharField": "unicode",
            "CommaSeparatedIntegerField": "unicode",
            "DateField": "date",
            "DateTimeField": "datetime",
            "DecimalField": "decimal",
            "EmailField": "unicode",
            "FileField": "unicode",
            "FilePathField": "unicode",
            "FloatField": "float",
            "ForeignKey": "unicode",
            "ImageField": "unicode",
            "IntegerField": "int",
            "IPAddressField": "unicode",
            "GenericIPAddressField": "unicode",
            "ManyToManyField": "unicode",
            "NullBooleanField": "boolean",
            "OneToOneField": "unicode",
            "PositiveIntegerField": "int",
            "PositiveSmallIntegerField": "int",
            "SlugField": "unicode",
            "SmallIntegerField": "int",
            "TextField": "unicode",
            "TimeField": "time",
            "URLField": "url",
        }

        # To grab regular spot info
        spot_field_array = \
            models.get_model('spotseeker_server', 'Spot')._meta.fields
        for field in spot_field_array:
            if is_auto_field(field):
                schema[field.name] = 'auto'
            else:
                field_itype = field.get_internal_type()
                if field_itype in internal_type_map:
                    field_itype = internal_type_map[field_itype]

                if field.name in location_descriptors:
                    schema['location'][field.name] = field_itype
                else:
                    schema[field.name] = field_itype

        # To grab spot image info
        spot_image_field_array = models.get_model('spotseeker_server',
                                                  'SpotImage')._meta.fields
        schema_image = {}
        for field in spot_image_field_array:
            if is_auto_field(field):
                schema_image[field.name] = 'auto'
            else:
                itype = field.get_internal_type()
                if itype in internal_type_map:
                    itype = internal_type_map[itype]
                schema_image[field.name] = itype
        schema['images'].append(schema_image)

        # To grab all of the different spot types
        for spot_type in SpotType.objects.all():
            schema["type"].append(spot_type.name)

        # To grab all of the extended info
        try:
            validated_ei = SpotForm.implementation().validated_extended_info
            org_form_exists = True
        except:
            org_form_exists = False
        for key_dict in SpotExtendedInfo.objects.values("key").distinct():
            key = key_dict["key"]
            if org_form_exists:
                schema['extended_info'][key] = validated_ei.get(key, 'unicode')

        return JSONResponse(schema)
Пример #6
0
    def build_and_save_from_input(self, request, spot):
        body = request.read()
        try:
            json_values = json.loads(body)
        except Exception as e:
            raise RESTException("Unable to parse JSON", status_code=400)

        partial_update = False
        stash = {}
        is_new = spot is None

        spot_pre_build.send(sender=SpotForm.implementation(),
                            request=request,
                            json_values=json_values,
                            spot=spot,
                            partial_update=partial_update,
                            stash=stash)

        self._build_spot_types(json_values, spot, partial_update)
        self._build_spot_location(json_values)

        spot_pre_save.send(sender=SpotForm.implementation(),
                           request=request,
                           json_values=json_values,
                           spot=spot,
                           partial_update=partial_update,
                           stash=stash)

        # Remve excluded fields
        excludefields = set(SpotForm.implementation().Meta.exclude)
        for fieldname in excludefields:
            if fieldname in json_values:
                del json_values[fieldname]

        if spot is not None and partial_update:
            # Copy over the existing values
            for field in spot._meta.fields:
                if field.name in excludefields:
                    continue
                if not field.name in json_values:
                    json_values[field.name] = getattr(spot, field.name)

            # spottypes is not included in the above copy, do it manually
            if not 'spottypes' in json_values:
                json_values['spottypes'] = [t.pk for t in spot.spottypes.all()]

        form = SpotForm(json_values, instance=spot)
        if not form.is_valid():
            raise RESTFormInvalidError(form)

        spot = form.save()

        spot_post_save.send(sender=SpotForm.implementation(),
                            request=request,
                            spot=spot,
                            partial_update=partial_update,
                            stash=stash)

        # gets the current etag
        spot = Spot.get_with_external(spot.pk)

        if is_new:
            response = HttpResponse(status=201)
            response['Location'] = spot.rest_url()
        else:
            response = JSONResponse(spot.json_data_structure(), status=200)
        response["ETag"] = spot.etag

        spot_post_build.send(sender=SpotForm.implementation(),
                             request=request,
                             response=response,
                             spot=spot,
                             partial_update=partial_update,
                             stash=stash)

        return response