Skip to content

Commit

Permalink
Fixes #227 - Return contact details and a closed space when there is …
Browse files Browse the repository at this point in the history
…no space object
  • Loading branch information
proffalken committed Nov 14, 2023
1 parent 2d5af2a commit 97abf2b
Showing 1 changed file with 96 additions and 70 deletions.
166 changes: 96 additions & 70 deletions memberportal/api_spacedirectory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,82 +41,108 @@ def get(self, request):
"issue_report_channels": ["email"],
}

# Get the default data
spaceapi_data = SpaceAPI.objects.get()

# Get a list of all the sensors
spaceapi_sensors = SpaceAPISensor.objects.all()

# Create an empty dict to add the sensor data to
sensor_data = {}

# Iterate over the sensors and update the dict as appropriate
for sensor in spaceapi_sensors:
sensor_details = {}
# Do we already have a sensor of this type? If not, create it now
if sensor.sensor_type not in sensor_data:
sensor_data[sensor.sensor_type] = []

## Setup the basic details
sensor_details = {
"name": sensor.name,
"description": sensor.description,
"location": sensor.location,
try:
# Get the default data
spaceapi_data = SpaceAPI.objects.get()

# Get a list of all the sensors
spaceapi_sensors = SpaceAPISensor.objects.all()

# Create an empty dict to add the sensor data to
sensor_data = {}

# Iterate over the sensors and update the dict as appropriate
for sensor in spaceapi_sensors:
sensor_details = {}
# Do we already have a sensor of this type? If not, create it now
if sensor.sensor_type not in sensor_data:
sensor_data[sensor.sensor_type] = []

## Setup the basic details
sensor_details = {
"name": sensor.name,
"description": sensor.description,
"location": sensor.location,
}

### Do we have properties? If so, let's add them
if len(sensor.properties.all()) > 0:
sensor_details["properties"] = {}
for prop in sensor.properties.all():
properties = {
prop.name: {"value": prop.value, "unit": prop.unit}
}
sensor_details["properties"].update(properties)
else:
sensor_details.update(
{"value": sensor.value, "unit": sensor.unit}
)

sensor_data[sensor.sensor_type].append(sensor_details)

## Add the user count and members on site count to the sensors
spaceapi_user_count = (
Profile.objects.all().filter(state="active").count()
)
spaceapi_members_on_site = SiteSession.objects.filter(
signout_date=None
).order_by("-signin_date")

sensor_data["total_member_count"] = {"value": spaceapi_user_count}

sensor_data["people_now_present"] = {
"value": spaceapi_members_on_site.count()
}

### Do we have properties? If so, let's add them
if len(sensor.properties.all()) > 0:
sensor_details["properties"] = {}
for prop in sensor.properties.all():
properties = {
prop.name: {"value": prop.value, "unit": prop.unit}
}
sensor_details["properties"].update(properties)
else:
sensor_details.update({"value": sensor.value, "unit": sensor.unit})

sensor_data[sensor.sensor_type].append(sensor_details)

## Add the user count and members on site count to the sensors
spaceapi_user_count = Profile.objects.all().filter(state="active").count()
spaceapi_members_on_site = SiteSession.objects.filter(
signout_date=None
).order_by("-signin_date")

sensor_data["total_member_count"] = {"value": spaceapi_user_count}

sensor_data["people_now_present"] = {
"value": spaceapi_members_on_site.count()
}

# Is the camera array empty? If not, add them
if not config.SPACE_DIRECTORY_CAMS:
spaceapi["cameras"] = config.SPACE_DIRECTORY_CAMS
# Is the camera array empty? If not, add them
if not config.SPACE_DIRECTORY_CAMS:
spaceapi["cameras"] = config.SPACE_DIRECTORY_CAMS

# Set the STATE part of the schema, the icons, and the schema version
spaceapi["state"] = {
"open": spaceapi_data.space_is_open,
"message": spaceapi_data.space_message,
"lastchange": spaceapi_data.status_last_change.timestamp(),
}
spaceapi["icon"] = {
"open": config.SPACE_DIRECTORY_ICON_OPEN,
"closed": config.SPACE_DIRECTORY_ICON_CLOSED,
}
spaceapi["api_compatibility"] = ["0.14"]
# Set the STATE part of the schema, the icons, and the schema version
spaceapi["state"] = {
"open": spaceapi_data.space_is_open,
"message": spaceapi_data.space_message,
"lastchange": spaceapi_data.status_last_change.timestamp(),
}
spaceapi["icon"] = {
"open": config.SPACE_DIRECTORY_ICON_OPEN,
"closed": config.SPACE_DIRECTORY_ICON_CLOSED,
}
spaceapi["api_compatibility"] = ["0.14"]

## Add the sensor data to the main body of the schema
spaceapi["sensors"] = sensor_data
## Add the sensor data to the main body of the schema
spaceapi["sensors"] = sensor_data

## Add the location data based on the values in Constance
spaceapi["location"] = {
"address": config.SPACE_DIRECTORY_LOCATION_ADDRESS,
"lat": config.SPACE_DIRECTORY_LOCATION_LAT,
"lon": config.SPACE_DIRECTORY_LOCATION_LON,
}
## Add the location data based on the values in Constance
spaceapi["location"] = {
"address": config.SPACE_DIRECTORY_LOCATION_ADDRESS,
"lat": config.SPACE_DIRECTORY_LOCATION_LAT,
"lon": config.SPACE_DIRECTORY_LOCATION_LON,
}

# Return the JSON document
return Response(spaceapi)
# Return the JSON document
return Response(spaceapi)
except SpaceAPI.DoesNotExist:
spaceapi = {
"space": config.SITE_OWNER,
"logo": config.SITE_LOGO,
"url": config.MAIN_SITE_URL,
"state": "closed",
"contact": {
"email": config.SPACE_DIRECTORY_CONTACT_EMAIL,
"twitter": config.SPACE_DIRECTORY_CONTACT_TWITTER,
"phone": config.SPACE_DIRECTORY_CONTACT_PHONE,
"facebook": config.SPACE_DIRECTORY_CONTACT_FACEBOOK,
},
"spacefed": {
"spacenet": config.SPACE_DIRECTORY_FED_SPACENET,
"spacesaml": config.SPACE_DIRECTORY_FED_SPACESAML,
"spacephone": config.SPACE_DIRECTORY_FED_SPACEPHONE,
},
"projects": json.loads(config.SPACE_DIRECTORY_PROJECTS),
"issue_report_channels": ["email"],
}
return Response(spaceapi)


class SpaceDirectoryUpdate(APIView):
Expand Down

0 comments on commit 97abf2b

Please sign in to comment.