Changeset 2520

Show
Ignore:
Timestamp:
02/02/10 12:15:28 (6 weeks ago)
Author:
JensDiemer
Message:

sending wbo works better

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • PyLucidPlugins/weave/views.py

    r2519 r2520  
    22 
    33import time 
     4import datetime 
     5import traceback 
    46 
    57try: 
     
    2830  # influence the "newer" and "older" modifiers 
    2931  return round(time.time(), 2) 
     32 
     33 
     34def _datetime2epochtime(dt): 
     35    assert isinstance(dt, datetime.datetime) 
     36    timestamp = time.mktime(dt.timetuple()) # datetime -> time since the epoch 
     37    # Add microseconds. FIXME: Is there a easier way? 
     38    timestamp += (dt.microsecond / 1000000.0) 
     39    return round(timestamp, 2) 
     40 
    3041 
    3142def _debug_request(request): 
     
    7687                raise AssertionError(msg) 
    7788 
    78             data_string = json.dumps(data, sort_keys=True) 
    79             response = http.HttpResponse(data_string, content_type='application/json') 
     89            try: 
     90                data_string = json.dumps(data, sort_keys=True) 
     91            except Exception, err: 
     92                print traceback.format_exc() 
     93                raise 
     94 
     95            response = http.HttpResponse(data_string, 
     96#                content_type='application/json' 
     97                content_type='text/plain' 
     98            ) 
    8099            response["X-Weave-Timestamp"] = _timestamp() 
    81100 
     
    100119 
    101120    return "weave plugin, use this url: %s" % request.build_absolute_uri() 
     121 
    102122 
    103123 
     
    130150    for wbo in wbos: 
    131151        lastupdatetime = wbo["lastupdatetime"] 
    132         # FIXME: Milliseconds 
    133         timestamp = time.mktime(lastupdatetime.timetuple()) # datetime -> time since the epoch 
     152        timestamp = _datetime2epochtime(lastupdatetime) # datetime -> time since the epoch 
    134153        timestamps[wbo["wboid"]] = str(timestamp) 
    135154 
     
    195214 
    196215        # The server will return the timestamp associated with the modification. 
    197         data = {wboid: wbo.lastupdatetime} 
     216        data = {wboid: _datetime2epochtime(wbo.lastupdatetime)} 
    198217        return data 
    199218    elif request.method == 'GET': 
    200219        # Returns a list of the WBO ids contained in a collection. 
    201         collection = Collection.on_site.get(user=user, name=col_name) 
     220        try: 
     221            collection = Collection.on_site.get(user=user, name=col_name) 
     222        except Collection.DoesNotExist: 
     223            raise http.Http404 
     224 
    202225        wbos = Wbo.objects.all().filter(collection=collection) 
    203226        print "+++", wbos 
     
    205228        for wbo in wbos: 
    206229            lastupdatetime = wbo.lastupdatetime 
    207             # FIXME: Milliseconds 
    208             timestamp = time.mktime(lastupdatetime.timetuple()) # datetime -> time since the epoch 
     230            timestamp = _datetime2epochtime(lastupdatetime) # datetime -> time since the epoch 
    209231            timestamps[wbo.wboid] = str(timestamp) 
    210232