Exemplo n.º 1
0
    def send_datapoint_update_to_client(self, sender, **kwargs):
        """
        Sends a JSON version of a changed datapoint via Websocket to the
        lient.

        TODO: Use a proper serializer.
        """
        logger.debug(
            "DatapointUpdate consumer got datapoint update %s",
            kwargs
        )

        # Be aware that this datapoint is not the object from the
        # database but the object that was created by the method that
        # updated or created the datapoint. I.e. if the method wrote a
        # number to a text field it will still be a number here. If that is an
        # issue you can circumvent this by reloading the object from the DB.
        # Only then the number will be converter to a text.
        datapoint = kwargs["instance"]

        if datapoint.id not in self.permitted_datapoint_ids:
            return
        dp_field_values = {}
        for field in datapoint._meta.fields:
            field_value = getattr(datapoint, field.name)
            # JSON can't carry datetime objects.
            if isinstance(field_value, datetime):
                field_value = datetime_to_pretty_str(field_value)

            dp_field_values[field.name] = field_value
        self.send_json(dp_field_values)
Exemplo n.º 2
0
 def last_schedule_timestamp_pretty(self, obj):
     """
     Displays a prettier timestamp format.
     """
     dt = obj.last_schedule_timestamp
     if dt is None:
         return "-"
     return datetime_to_pretty_str(dt)
Exemplo n.º 3
0
 def timestamp_pretty(self, obj):
     """
     Displays a prettier timestamp format.
     """
     ts = obj.timestamp
     if ts is None:
         return "-"
     return datetime_to_pretty_str(ts)