def delete(request, species_wid, wid): #retrieve object obj = getEntry(species_wid=species_wid, wid=wid) if obj is None: raise Http404 model = obj.__class__ qs = objectToQuerySet(obj, model=model) #delete if request.method == 'POST': obj.delete() return HttpResponseRedirect( reverse('public.views.list', kwargs={ 'species_wid': species_wid, 'model_type': model.__name__ })) #confirmation message return render_queryset_to_response( species_wid=species_wid, request=request, models=[model], queryset=qs, templateFile='public/delete.html', data={'model_verbose_name': model._meta.verbose_name})
def detail(request, species_wid, wid): obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ model_type = model.__name__ fieldsets = deepcopy(model._meta.fieldsets) #filter out type, metadata fieldset_names = [x[0] for x in fieldsets] if 'Type' in fieldset_names: idx = fieldset_names.index('Type') del fieldsets[idx] #filter out empty fields rmfieldsets = [] for idx in range(len(fieldsets)): rmfields = [] for idx2 in range(len(fieldsets[idx][1]['fields'])): if isinstance(fieldsets[idx][1]['fields'][idx2], dict): field_name = fieldsets[idx][1]['fields'][idx2]['name'] verbose_name = fieldsets[idx][1]['fields'][idx2]['verbose_name'] else: field_name = fieldsets[idx][1]['fields'][idx2] field = model._meta.get_field_by_name(field_name)[0] if isinstance(field, RelatedObject): verbose_name = capfirst(field.get_accessor_name()) else: verbose_name = field.verbose_name data = format_field_detail_view(obj, field_name, request.user.is_anonymous()) if (data is None) or (data == ''): rmfields = [idx2] + rmfields fieldsets[idx][1]['fields'][idx2] = {'verbose_name': verbose_name.replace(" ", ' ').replace("-", "‑"), 'data': data} for idx2 in rmfields: del fieldsets[idx][1]['fields'][idx2] if len(fieldsets[idx][1]['fields']) == 0: rmfieldsets = [idx] + rmfieldsets for idx in rmfieldsets: del fieldsets[idx] #form query set qs = objectToQuerySet(obj, model = model) #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [model], queryset = qs, templateFile = 'public/detail.html', data = { 'model_type': model_type, 'model': model, 'fieldsets': fieldsets, 'message': request.GET.get('message', ''), })
def delete(request, species_wid, wid): #retrieve object obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ qs = objectToQuerySet(obj, model = model) #delete if request.method == 'POST': obj.delete() return HttpResponseRedirect(reverse('public.views.list', kwargs={'species_wid':species_wid, 'model_type': model.__name__})) #confirmation message return render_queryset_to_response( species_wid = species_wid, request = request, models = [model], queryset = qs, templateFile = 'public/delete.html', data = { 'model_verbose_name': model._meta.verbose_name } )
def edit(request, wid=None, model_type=None, species_wid=None, action='edit'): #retrieve object if action == 'edit': obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ else: model = getModel(model_type) obj = model() #save object error_messages = {} if request.method == 'POST': submitted_data = get_edit_form_data(model, request.POST) data = submitted_data data['id'] = obj.id data['species'] = species_wid data['model_type'] = model.__name__ try: #validate is WID unique if issubclass(model, SpeciesComponent): qs = SpeciesComponent.objects.values('wid', 'model_type').filter(species__wid=species_wid) else: qs = model.objects.values('wid', 'model_type').all() if action == 'edit': qs = qs.exclude(id=obj.id) wids = {} for x in qs: wids[x['wid']] = x['model_type'] if data['wid'] in wids.keys(): raise ValidationError({'wid': 'Value must be unique'}) wids[data['wid']] = model.__name__ #validate data = validate_object_fields(model, data, wids, species_wid, data['wid']) validate_model_objects(model, data) validate_model_unique(model, [data]) #save obj = save_object_data(species_wid, obj, data, {}, request.user, save=False, save_m2m=False) obj = save_object_data(species_wid, obj, data, {data['wid']: obj}, request.user, save=True, save_m2m=False) obj = save_object_data(species_wid, obj, data, {data['wid']: obj}, request.user, save=True, save_m2m=True) #redirect to details page return HttpResponseRedirect(obj.get_absolute_url()) except ValidationError as error: error_messages = error.message_dict #form query set if action == 'edit': obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 qs = objectToQuerySet(obj, model = model) else: obj = None qs = model.objects.none() #display form fields, initial_values = get_edit_form_fields(species_wid, model, obj=obj) if request.method == 'POST': initial_values = submitted_data return render_queryset_to_response( species_wid = species_wid, request = request, models = [model], queryset = qs, templateFile = 'public/edit.html', data = { 'model_verbose_name': model._meta.verbose_name, 'action': action, 'fields': fields, 'references_choices': Reference.objects.filter(species__wid = species_wid).values_list('wid'), 'initial_values': initial_values, 'error_messages': error_messages, } )
def edit(request, wid=None, model_type=None, species_wid=None, action='edit'): #retrieve object if action == 'edit': obj = getEntry(species_wid=species_wid, wid=wid) if obj is None: raise Http404 model = obj.__class__ else: model = getModel(model_type) obj = model() #save object error_messages = {} if request.method == 'POST': submitted_data = get_edit_form_data(model, request.POST) data = submitted_data data['id'] = obj.id data['species'] = species_wid data['model_type'] = model.__name__ try: #validate is WID unique if issubclass(model, SpeciesComponent): qs = SpeciesComponent.objects.values( 'wid', 'model_type').filter(species__wid=species_wid) else: qs = model.objects.values('wid', 'model_type').all() if action == 'edit': qs = qs.exclude(id=obj.id) wids = {} for x in qs: wids[x['wid']] = x['model_type'] if data['wid'] in wids.keys(): raise ValidationError({'wid': 'Value must be unique'}) wids[data['wid']] = model.__name__ #validate data = validate_object_fields(model, data, wids, species_wid, data['wid']) validate_model_objects(model, data) validate_model_unique(model, [data]) #save obj = save_object_data(species_wid, obj, data, {}, request.user, save=False, save_m2m=False) obj = save_object_data(species_wid, obj, data, {data['wid']: obj}, request.user, save=True, save_m2m=False) obj = save_object_data(species_wid, obj, data, {data['wid']: obj}, request.user, save=True, save_m2m=True) #redirect to details page return HttpResponseRedirect(obj.get_absolute_url()) except ValidationError as error: error_messages = error.message_dict #form query set if action == 'edit': obj = getEntry(species_wid=species_wid, wid=wid) if obj is None: raise Http404 qs = objectToQuerySet(obj, model=model) else: obj = None qs = model.objects.none() #display form fields, initial_values = get_edit_form_fields(species_wid, model, obj=obj) if request.method == 'POST': initial_values = submitted_data return render_queryset_to_response( species_wid=species_wid, request=request, models=[model], queryset=qs, templateFile='public/edit.html', data={ 'model_verbose_name': model._meta.verbose_name, 'action': action, 'fields': fields, 'references_choices': Reference.objects.filter( species__wid=species_wid).values_list('wid'), 'initial_values': initial_values, 'error_messages': error_messages, })
def detail(request, species_wid, wid): obj = getEntry(species_wid=species_wid, wid=wid) if obj is None: raise Http404 model = obj.__class__ model_type = model.__name__ fieldsets = deepcopy(model._meta.fieldsets) #filter out type, metadata fieldset_names = [x[0] for x in fieldsets] if 'Type' in fieldset_names: idx = fieldset_names.index('Type') del fieldsets[idx] #filter out empty fields rmfieldsets = [] for idx in range(len(fieldsets)): rmfields = [] for idx2 in range(len(fieldsets[idx][1]['fields'])): if isinstance(fieldsets[idx][1]['fields'][idx2], dict): field_name = fieldsets[idx][1]['fields'][idx2]['name'] verbose_name = fieldsets[idx][1]['fields'][idx2][ 'verbose_name'] else: field_name = fieldsets[idx][1]['fields'][idx2] field = model._meta.get_field_by_name(field_name)[0] if isinstance(field, RelatedObject): verbose_name = capfirst(field.get_accessor_name()) else: verbose_name = field.verbose_name data = format_field_detail_view(obj, field_name, request.user.is_anonymous()) if (data is None) or (data == ''): rmfields = [idx2] + rmfields fieldsets[idx][1]['fields'][idx2] = { 'verbose_name': verbose_name.replace(" ", ' ').replace("-", "‑"), 'data': data } for idx2 in rmfields: del fieldsets[idx][1]['fields'][idx2] if len(fieldsets[idx][1]['fields']) == 0: rmfieldsets = [idx] + rmfieldsets for idx in rmfieldsets: del fieldsets[idx] #form query set qs = objectToQuerySet(obj, model=model) #render response return render_queryset_to_response(species_wid=species_wid, request=request, models=[model], queryset=qs, templateFile='public/detail.html', data={ 'model_type': model_type, 'model': model, 'fieldsets': fieldsets, 'message': request.GET.get('message', ''), })
def viewParametersInSimulation(request, species_wid, wid): obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ verbose_class_name = model._meta.verbose_name verbose_property_name = '%s parameters' % obj.name #get associated simulation property qs = EmptyQuerySet() for field in model._meta.get_all_related_objects() + model._meta.get_all_related_many_to_many_objects(): accessor_name = field.get_accessor_name() if accessor_name == '+': continue if isinstance(field, ForeignKey) and issubclass(field.rel.to, Parameter): qs._result_cache.append(getattr(obj, accessor_name)) if (isinstance(field, ManyToManyField) and issubclass(field.rel.to, Parameter)) or \ (isinstance(field, RelatedObject) and issubclass(field.model, Parameter)): qs2 = getattr(obj, accessor_name).all() if len(qs2) > 0: qs._result_cache.extend(qs2._result_cache) classes = {} for o in qs: if not o.state is None: sim_class_name = 'edu.stanford.covert.cell.sim.state.%s' % o.state.wid.replace('State_', '') else: sim_class_name = 'edu.stanford.covert.cell.sim.process.%s' % o.process.wid.replace('Process_', '') if not classes.has_key(sim_class_name): classes[sim_class_name] = [] classes[sim_class_name].append(o.name) #highlight code for simulation class objects = [] for sim_class_name in classes: sim_property_names = classes[sim_class_name] sim_property_names.sort() pathParts = sim_class_name.split('.') codePath = "%s/src/+%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1]) if not os.path.isfile(codePath): codePath = "%s/src/+%s/@%s/%s.m" % (MODEL_CODE_BASE_DIR, '/+'.join(pathParts[0:-1]), pathParts[-1], pathParts[-1]) if not os.path.isfile(codePath): continue with open (codePath, "r") as codeFile: code = codeFile.read() lexer = MatlabLexer() lexer.add_filter(PropertyDefinitionFilter(property_names = sim_property_names, tokentype=Token.Name.Variable)) tokens = lexer.get_tokens(code) objects.append({ 'class_name': sim_class_name, 'property_names': sim_property_names, 'code': pygments.format(tokens, PygmentsFormatter(linenos='inline', linenostep=1, style=PygmentsStyle, noclasses=True)), }) #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [Parameter], queryset = qs, templateFile = 'public/viewPropertyInSimulation.html', data = { 'object_list': objects, 'verbose_class_name': verbose_class_name, 'verbose_property_name': verbose_property_name, })
def detail(request, species_wid, wid): obj = getEntry(species_wid = species_wid, wid = wid) if obj is None: raise Http404 model = obj.__class__ model_type = model.__name__ fieldsets = deepcopy(model._meta.fieldsets) #filter out type, metadata fieldset_names = [x[0] for x in fieldsets] if 'Type' in fieldset_names: idx = fieldset_names.index('Type') del fieldsets[idx] #filter out empty fields rmfieldsets = [] for idx in range(len(fieldsets)): rmfields = [] for idx2 in range(len(fieldsets[idx][1]['fields'])): if isinstance(fieldsets[idx][1]['fields'][idx2], dict): field = fieldsets[idx][1]['fields'][idx2] field_name = field['name'] verbose_name = field['verbose_name'] else: field_name = fieldsets[idx][1]['fields'][idx2] field = model._meta.get_field_by_name(field_name)[0] if isinstance(field, RelatedObject): verbose_name = capfirst(field.get_accessor_name()) else: verbose_name = field.verbose_name data = format_field_detail_view(obj, field_name, request.user.is_anonymous()) if (data is None) or (data == ''): rmfields = [idx2] + rmfields if issubclass(model, Parameter) and field_name == 'value': is_modeled_url = reverse('public.views.viewParameterInSimulation', kwargs = { 'species_wid':species_wid, 'wid': wid, }) elif (isinstance(field, (ForeignKey, ManyToManyField)) and issubclass(field.rel.to, Parameter)) or \ (isinstance(field, (RelatedObject)) and issubclass(field.model, Parameter)): is_modeled_url = reverse('public.views.viewParametersInSimulation', kwargs = { 'species_wid':species_wid, 'wid': wid, }) else: try: if ModelProperty.objects.get(species__wid = species_wid, class_name = model_type, property_name = field_name).simulation_properties.exists(): is_modeled_url = reverse('public.views.viewPropertyInSimulation', kwargs = { 'species_wid':species_wid, 'class_name': model_type, 'property_name': field_name, }) except ObjectDoesNotExist: is_modeled_url = '' fieldsets[idx][1]['fields'][idx2] = { 'name': field_name, 'verbose_name': verbose_name.replace(" ", ' ').replace("-", "‑"), 'data': data, 'is_modeled_url': is_modeled_url, } for idx2 in rmfields: del fieldsets[idx][1]['fields'][idx2] if len(fieldsets[idx][1]['fields']) == 0: rmfieldsets = [idx] + rmfieldsets for idx in rmfieldsets: del fieldsets[idx] #form query set qs = objectToQuerySet(obj, model = model) #render response return render_queryset_to_response( species_wid = species_wid, request = request, models = [model], queryset = qs, templateFile = 'public/detail.html', data = { 'model_type': model_type, 'model': model, 'fieldsets': fieldsets, 'message': request.GET.get('message', ''), })