Ejemplo n.º 1
0
 def to_python(self, value):
     if value == '':
         return Extras()
     elif type(value) == Extras:
         return value
     else:
         extras = Extras()
         try:
             values = json.loads(value)
             assert type(values) == dict, 'expected a dictionary object, found a %s' % type(values).__name__
             for key in values.keys():
                 assert type(key) == unicode, 'expected unicode keys, found a %s' % type(key).__name__
             extras.__dict__ = values
         except (ValueError, AssertionError), e:
             raise ValidationError, 'Invalid JSON data in ExtrasField: %s' % e
         return extras
Ejemplo n.º 2
0
 def submitData(self, jstr):
     obj = json.loads(jstr)
     # for now, assume it's plan status, that's all we have...
     planStat = obj['planStatus']
     for k in planStat.keys():
         if k != 'timestamp':
             try:
                 statusInfo = StatusboardPlanStatus.objects.get(keyword=k)
                 statusInfo.value = json.dumps(planStat[k])
                 statusInfo.timestamp = \
                     datetime.datetime.\
                     fromtimestamp(float(planStat['timestamp']))
                 statusInfo.save()
             except ObjectDoesNotExist:
                 StatusboardPlanStatus.objects.\
                     create(timestamp=(datetime.datetime.
                                       fromtimestamp(float(planStat['timestamp']))),
                            keyword=k,
                            value=json.dumps(planStat[k]))
Ejemplo n.º 3
0
def postPosition(request):
    if request.method == "GET":
        return HttpResponseNotAllowed("Please post a resource position as a GeoJSON Feature.")
    else:
        try:
            featureDict = json.loads(request.raw_post_data)
        except ValueError:
            return HttpResponse("Malformed request, expected resources position as a GeoJSON Feature", status=400)

        # create or update Resource
        properties = featureDict["properties"]
        featureUserName = properties["userName"]
        matchingUsers = User.objects.filter(username=featureUserName)
        if matchingUsers:
            user = matchingUsers[0]
        else:
            user = User.objects.create_user(featureUserName, "*****@*****.**" % featureUserName, "12345")
            user.first_name = featureUserName
            user.is_active = False
            user.save()
        resource, created = Resource.objects.get_or_create(uuid=featureDict["id"], defaults=dict(user=user))
        if resource.user.username != featureUserName:
            resource.user = user
            resource.save()

        # create or update ResourcePosition
        coordinates = featureDict["geometry"]["coordinates"]
        timestamp = iso8601.parse_date(properties["timestamp"]).replace(tzinfo=None)
        attrs = dict(timestamp=timestamp, longitude=coordinates[0], latitude=coordinates[1])
        if len(coordinates) >= 3:
            attrs["altitude"] = coordinates[2]
        rp, created = ResourcePosition.objects.get_or_create(resource=resource, defaults=attrs)
        if not created:
            for field, val in attrs.iteritems():
                setattr(rp, field, val)
            rp.save()

        # add a PastResourcePosition historical entry
        PastResourcePosition(resource=resource, **attrs).save()

        return HttpResponse(dumps(dict(result="ok")), mimetype="application/json")
Ejemplo n.º 4
0
 def getGeometry(self):
     return json.loads(self.json)