Changeset 2522
- Timestamp:
- 02/03/10 11:47:39 (6 weeks ago)
- Location:
- PyLucidPlugins/weave
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
PyLucidPlugins/weave/admin.py
r2519 r2522 29 29 30 30 31 31 32 class WboAdmin(VersionAdmin): 32 """ 33 """ 34 # list_display = ("id", "headline", "is_public", "view_on_site_link", "site_info", "lastupdatetime", "lastupdateby") 35 # list_display_links = ("headline",) 36 # list_filter = ("is_public", "sites", "createby", "lastupdateby",) 37 # date_hierarchy = 'lastupdatetime' 33 def payload_cutout(self, obj): 34 MAX = 100 35 payload = obj.payload 36 if len(payload) > MAX: 37 payload = payload[:MAX] + "..." 38 return payload 39 payload_cutout.short_description = "Payload cutout" 40 # view_on_site_link.allow_tags = True 41 42 list_display = ("id", "lastupdatetime", "user", "wboid", "parentid", "sortindex", "lastupdateby", "payload_cutout") 43 list_display_links = ("wboid",) 44 list_filter = ("user", "collection") 45 date_hierarchy = 'lastupdatetime' 38 46 # search_fields = ("headline", "content") 39 47 … … 44 52 """ 45 53 """ 46 # list_display = ("id", "headline", "is_public", "view_on_site_link", "site_info", "lastupdatetime", "lastupdateby")47 # list_display_links = ("headline",)48 # list_filter = ("is_public", "sites", "createby", "lastupdateby",)49 #date_hierarchy = 'lastupdatetime'54 list_display = ("id", "lastupdatetime", "user", "name", "lastupdateby") 55 list_display_links = ("name",) 56 list_filter = ("user", "createby", "lastupdateby",) 57 date_hierarchy = 'lastupdatetime' 50 58 # search_fields = ("headline", "content") 51 59 -
PyLucidPlugins/weave/models.py
r2519 r2522 28 28 return u"weave collection %r for user %r" % (self.name, self.user.username) 29 29 30 class Meta: 31 ordering = ("-lastupdatetime",) 32 30 33 31 34 class Wbo(UpdateInfoBaseModel): … … 40 43 lastupdateby -> ForeignKey to user who has edited this entry 41 44 """ 42 collection = models.ForeignKey(Collection) 45 collection = models.ForeignKey(Collection, blank=True, null=True) 46 user = models.ForeignKey(User) 43 47 wboid = models.CharField(max_length=64, blank=True, 44 48 help_text="wbo identifying string" 49 ) 50 parentid = models.CharField(max_length=64, blank=True, null=True, 51 help_text="wbo parent identifying string" 45 52 ) 46 53 sortindex = models.IntegerField(null=True, blank=True, … … 58 65 def __unicode__(self): 59 66 return u"weave wbo %r (%r)" % (self.wboid, self.collection) 67 68 class Meta: 69 ordering = ("-lastupdatetime",) -
PyLucidPlugins/weave/urls.py
r2519 r2522 20 20 21 21 22 #/1.0/test1/storage/keys/pubkey23 22 url(r'^/(?P<version>.*?)/(?P<username>.*?)/storage/(?P<col_name>.*?)/(?P<wboid>.*?)$', views.storage), 23 url(r'^/(?P<version>.*?)/(?P<username>.*?)/storage/(?P<wboid>.*?)$', views.storage_wboid), 24 24 25 25 #/1.0/UserName/info/collections -
PyLucidPlugins/weave/views.py
r2520 r2522 15 15 from django.utils.functional import wraps # Python 2.3, 2.4 fallback. 16 16 17 from django import http17 from django.http import HttpResponse 18 18 from django.conf import settings 19 19 from django.contrib.auth.models import User … … 41 41 42 42 def _debug_request(request): 43 MAX = 120 44 def cut(s): 45 s = repr(s) 46 if len(s) > MAX: 47 return s[:MAX] + "..." 48 return s 49 43 50 print "request.META['CONTENT_LENGTH']: %r" % request.META['CONTENT_LENGTH'] 44 51 print "request.GET: %r" % request.GET 45 print "request.POST: % r" % request.POST52 print "request.POST: %s" % cut(request.POST) 46 53 print "request.FILES: %r" % request.FILES 47 print "request.raw_post_data: % r" % request.raw_post_data54 print "request.raw_post_data: %s" % cut(request.raw_post_data) 48 55 49 56 … … 81 88 data = function(request, *args, **kwargs) 82 89 90 if isinstance(data, HttpResponse): 91 if debug: 92 print "render debug for %r:" % function.__name__ 93 print "data: %r" % data 94 print "response.content:", data.content 95 return data 96 83 97 if not isinstance(data, dict): 84 98 msg = ( 85 " renter_toinfo: %s has not return a dict, has return: %r (%r)"99 "json_response info: %s has not return a dict, has return: %r (%r)" 86 100 ) % (function.__name__, type(data), function.func_code) 87 101 raise AssertionError(msg) … … 93 107 raise 94 108 95 response = http.HttpResponse(data_string,109 response = HttpResponse(data_string, 96 110 # content_type='application/json' 97 111 content_type='text/plain' … … 112 126 113 127 128 class RecordNotFoundResponse(HttpResponse): 129 status_code = 404 130 def __init__(self, content='"record not found"'): 131 HttpResponse.__init__(self) 132 self._container = [content] 133 114 134 115 135 116 136 def root_view(request): 117 print " root_view!"137 print " *** root_view! ***" 118 138 _debug_request(request) 119 139 … … 129 149 https://wiki.mozilla.org/Labs/Weave/Sync/1.0/API#GET 130 150 """ 151 print "_" * 79 131 152 print "info_collections:" 132 153 _debug_request(request) … … 157 178 return timestamps 158 179 159 180 @assert_username(debug=True) 181 @check_permissions(superuser_only=False, permissions=(u'weave.add_collection', u'weave.add_wbo')) 182 @json_response(debug=True) 183 def storage_wboid(request, version, username, wboid): 184 """ 185 get items from storage 186 e.g.: 187 GET /1.0/UserName/storage/history?newer=1265189444.85&full=1&sort=index&limit=1500 188 """ 189 print "_" * 79 190 print "storage_wboid", wboid 191 _debug_request(request) 192 193 user = request.user 194 if request.method == 'POST': 195 payload = request.raw_post_data 196 if not payload: 197 # If the WBO does not contain a payload, it will only update 198 # the provided metadata fields on an already defined object. 199 raise NotImplemented 200 201 collection, created = Collection.on_site.get_or_create(user=user, name=wboid) 202 if created: 203 print "Collection %r created" % collection 204 else: 205 print "Collection %r exists" % collection 206 207 data = json.loads(payload) 208 if not isinstance(data, list): 209 raise NotImplemented 210 211 for item in data: 212 wbo, created = Wbo.objects.get_or_create( 213 collection=collection, 214 user=user, 215 wboid=item["id"], 216 defaults={ 217 "parentid": item.get("parentid", None), # FIXME: must wboid + parentid be unique? 218 "sortindex": item.get("sortindex", None), 219 "payload": item["payload"], 220 } 221 ) 222 if created: 223 print "New wbo created: %r" % wbo 224 else: 225 wbo.parentid = item.get("parentid", None) 226 wbo.sortindex = item.get("sortindex", None) 227 wbo.payload = item["payload"] 228 wbo.save() 229 print "Existing wbo updated: %r" % wbo 230 231 # assert val["id"] == wboid, "wrong wbo id: %r != %r" % (val["id"], wboid) 232 233 return {} 234 elif request.method == 'GET': 235 try: 236 wbo = Wbo.objects.filter(user=user).get(wboid=wboid) 237 except Wbo.DoesNotExist: 238 print "Wbo %r not exist for user %r" % (wboid, user) 239 return RecordNotFoundResponse() 240 241 payload = wbo.payload 242 data = json.loads(payload) 243 data["modified"] = _datetime2epochtime(wbo.lastupdatetime) 244 return data 245 else: 246 247 raise NotImplemented 160 248 161 249 @assert_username(debug=True) … … 179 267 'meta': '1265103193.99'}} 180 268 """ 269 print "_" * 79 181 270 print "storage", col_name, wboid 182 271 _debug_request(request) … … 207 296 wbo = Wbo( 208 297 collection=collection, 298 user=user, 209 299 wboid=wboid, 210 300 sortindex=sortindex, … … 221 311 collection = Collection.on_site.get(user=user, name=col_name) 222 312 except Collection.DoesNotExist: 223 raise http.Http404 224 225 wbos = Wbo.objects.all().filter(collection=collection) 226 print "+++", wbos 227 timestamps = {} 228 for wbo in wbos: 229 lastupdatetime = wbo.lastupdatetime 230 timestamp = _datetime2epochtime(lastupdatetime) # datetime -> time since the epoch 231 timestamps[wbo.wboid] = str(timestamp) 232 233 print timestamps 234 235 return timestamps 313 print "Collection %r for user %r not found" % (col_name, user) 314 return RecordNotFoundResponse() 315 316 try: 317 wbo = Wbo.objects.all().filter(collection=collection).get(wboid=wboid) 318 except Wbo.DoesNotExist: 319 print "Wbo %r not exist for collection %r" % (wboid, collection) 320 return RecordNotFoundResponse() 321 322 payload = wbo.payload 323 data = json.loads(payload) 324 data["modified"] = _datetime2epochtime(wbo.lastupdatetime) 325 return data 326 # print "+++", wbos 327 # timestamps = {} 328 # for wbo in wbos: 329 # lastupdatetime = wbo.lastupdatetime 330 # timestamp = _datetime2epochtime(lastupdatetime) # datetime -> time since the epoch 331 # timestamps[wbo.wboid] = str(timestamp) 332 333 # val = json.loads(raw_post_data) 334 # print "val1: %r" % val 335 # val['modified'] = _timestamp() 336 # print "val2: %r" % val 337 # val = json.dumps(val, sort_keys=True) 338 339 # if not timestamps: 340 # print "wboid %r not found for collection %r" % (wboid, collection) 341 # return RecordNotFoundResponse() 342 # 343 # return timestamps 236 344 else: 237 345 raise NotImplemented … … 249 357 250 358 251 try: 252 collection = Collection.on_site.get(user=request.user, name=col_name) 253 except Collection.DoesNotExist: 254 raise http.Http404("collection not found.") 255 256 257 raw_post_data = request.raw_post_data 258 if raw_post_data: 259 print "raw_post_data: %r" % raw_post_data 260 val = json.loads(raw_post_data) 261 print "val1: %r" % val 262 val['modified'] = _timestamp() 263 print "val2: %r" % val 264 val = json.dumps(val, sort_keys=True) 265 266 # self.collections.setdefault(col, {})[key] = val 267 # self.ts_col(col) 268 269 270 response = http.HttpResponse("{}", content_type='application/json') 271 response["X-Weave-Timestamp"] = _timestamp() 272 return response 273 274 359 # try: 360 # collection = Collection.on_site.get(user=user, name=col_name) 361 # except Collection.DoesNotExist: 362 # print "Collection %r for user %r not found" % (col_name, user) 363 # return RecordNotFoundResponse() 364 # 365 # 366 # raw_post_data = request.raw_post_data 367 # if raw_post_data: 368 # print "raw_post_data: %r" % raw_post_data 369 370 # 371 ## self.collections.setdefault(col, {})[key] = val 372 ## self.ts_col(col) 373 # 374 # 375 # response = HttpResponse("{}", content_type='application/json') 376 # response["X-Weave-Timestamp"] = _timestamp() 377 # return response 378 379 380 @assert_username(debug=True) 275 381 def sign_in(request, version, username): 382 """ 383 finding cluster for user -> return 404 -> Using serverURL as data cluster (multi-cluster support disabled) 384 """ 385 print "_" * 79 276 386 print "sign_in %r" % username 277 387 print "request.user: %r" % request.user 278 388 _debug_request(request) 279 raise http.Http404 280 281 389 390 # absolute_uri = request.build_absolute_uri() 391 392 # response = HttpResponse("", 393 ## content_type='application/json' 394 # content_type='text/plain' 395 # ) 396 # response["X-Weave-Timestamp"] = _timestamp() 397 # 398 # return response 399 400 return RecordNotFoundResponse(content="404 Not Found") 401 # 402 # response = HttpResponse("", content_type='text/html') 403 # response["X-Weave-Timestamp"] = _timestamp() 404 # return response 405 406 407 @assert_username(debug=True) 408 @json_response(debug=True) 282 409 def exist_user(request, version, username): 283 410 """ 284 411 https://wiki.mozilla.org/Labs/Weave/User/1.0/API 285 Returns 1 if the username is in use, 0 if it is available. 286 """ 412 Returns 1 if the username is in use, 0 if it is available. 413 414 e.g.: https://auth.services.mozilla.com/user/1/UserName 415 """ 416 print "_" * 79 287 417 print "exist_user:" 288 418 _debug_request(request) … … 294 424 response_content = "0" 295 425 296 response = http.HttpResponse(response_content, content_type='application/json')426 response = HttpResponse(response_content, content_type='text/html') 297 427 response["X-Weave-Timestamp"] = _timestamp() 298 428 return response 299 429 430 300 431 def captcha_html(request, version): 432 print "_" * 79 301 433 print "captcha_html:" 302 434 _debug_request(request) 303 435 304 # raise http.Http404305 #response = http.HttpResponse("11", status=400, content_type='application/json')306 response = http.HttpResponse("not supported")436 # raise Http404 437 #response = HttpResponse("11", status=400, content_type='application/json') 438 response = HttpResponse("not supported") 307 439 response["X-Weave-Timestamp"] = _timestamp() 308 440 return response … … 310 442 311 443 def setup_user(request, version, username): 444 print "_" * 79 312 445 print "setup_user", version, username 313 446 _debug_request(request)