def getModel(rq, model_id): try: model = Model.get(model_id) except CouchExceptions.ResourceNotFound as e: raise Http404("Model does not exist (%s)" % e) #return JsonResponse({'success': False, 'error': 'Ressource not found (%s)' % e}) try: lens = Lens.get(model.lens_id) except CouchExceptions.ResourceNotFound as e: raise Http404("Lens does not exist (%s)" % e) # use lists with tuples to conserve ordering model_attr = [ ('ID', model._id), ('User', model.created_by), ('Date', model.created_at.strftime('%Y-%m-%d %H:%M:%S')), ('PixelRadius', model.obj['pixrad']), ('nModels', model.obj['n_models']), ('z(lens/src)', (model.obj['z_lens'], model.obj['z_src'])), # ('', ), # ('', ), # ('', ), # ('', ), ] lens_attr = [ ('id' , lens._id[0:12]), ('name' , ''.join([_ for _ in lens.names if _.startswith('ASW')]) ), ] dataurl = os.path.join('/media', 'spaghetti', model_id[0:2], model_id[2:]) images = { 'lens' : '/media/lenses/%s/%s/COMPOSITE_PIXEL_IMAGE-0001-DEFAULT.PNG' % (lens._id[0:2], lens._id[2:]), 'input' : dataurl + '/input.png', 'synthetic': dataurl + '/img3_ipol.png', 'contour' : dataurl + '/img1.png', 'adv' : [ {'title':'Mass Distribution', 'url': dataurl+'/img2.png'}, {'title':'Org SrcDiffPlt', 'url': dataurl+'/img3.png'}, ], } #children = Model.view("spaghetti/Children") # get children (this functionality is buggy in cdbkit, so make my own) uri = Model.get_db().server_uri s = requests.Session() try: childs = s.get(uri+'/spaghetti/_design/spaghetti/_view/Children?group=true&startkey="%s"&endkey="%s"' % (model_id, model_id)).json()['rows'][0]['value'] except: childs = [] children = [] for c in childs: m = Model.get(c) children.append({ 'id': c, 'user': m.obj['author'], 'date': m.created_at }) #print children try: p = Model.get(model.parent) parent = { 'id': model.parent, 'user': p.obj['author'], 'date': p.created_at } except: parent = None links = { 'fork' : '/spaghetti/?model=' + model_id, 'new' : '/spaghetti/?lens=' + lens._id, 'prev' : parent, 'next' : children } files = [ ('Model JSON Object', '%s.json'%model_id, 'data:text/plain;base64,' + model.obj['jsonStr'].encode("base64")), ('Glass Config File', '%s.config.gls'%model_id, 'data:text/plain;base64,' + model.obj['gls'].encode("base64")), ('Glass State File (binary)', '%s.state'%model_id, dataurl + '/state.glass'), ('Glass Log File', '%s.log.txt'%model_id, dataurl + '/glass.log'), ] context = { 'lens': lens, 'model': model, 'lens_a': lens_attr, 'model_a': model_attr, 'refresh': False, # or int as nr of secs # 'dataurl': os.path.join(settings.MEDIA_ROOT, 'spaghetti', model_id[0:2], model_id[2:]) 'images': images, 'links':links, 'files':files, } #print context return render(rq, 'spaghetti/model.html', context)
def _saveModel(rq, lmtmodel, lens_id, parent, username, comment): #print "username:"******"eval and save" EASJobj = EvalAndSaveJSON(user_str = username, data_obj= lens, jsonStr = lmtmodel, is_final= False) obj = EASJobj.getDict() #print "after eval and save" #print '='*80 #print "EASJobj:" #for k, v in EASJobj.__dict__['_'].items(): #print "%-16s : %s" % (k, v) #print '='*80 # print '='*80 # print "parsed data obj:" # for k, v in obj.items(): # print "%-16s : %s" % (k, v) # print '='*80 #model_id = "model_"+str(random.randint(100,999)) # generate the id m = hashlib.sha256() m.update(lens_id) m.update(lmtmodel) model_id = base64.b32encode(m.digest())[0:10] #print 'model_id:', model_id # generate the checksum (used when marking as final to check wether is unchanged) mm = hashlib.md5() mm.update(lens_id) mm.update(lmtmodel) checksum_md5 = mm.hexdigest() now = datetime.datetime.utcnow() model = Model( lens_id = lens_id, parent = parent, created_by = username, created_at = now, comments = [[username, now, comment]], checksum = checksum_md5, obj = obj, glass = {}, task_id = None, rendered = False, isFinal = False, ) model._id = model_id model.obj['model_id'] = model_id try: model.save() except CouchExceptions.ResourceConflict: return JsonResponse({'success': False, 'error': 'Ressource already present ()'}) return JsonResponse({'success': True, 'model_id': model_id})