Beispiel #1
0
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})
Beispiel #2
0
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', ''),
			})
Beispiel #3
0
def contributor(request, username, species_wid=None):
	queryset = objectToQuerySet(get_object_or_404(User, username = username), model = User)
	return render_queryset_to_response(
		species_wid = species_wid,
		request = request,
		models = [User],
		queryset = queryset,
		templateFile = 'public/contributor.html')	
Beispiel #4
0
def contributor(request, username, species_wid=None):
    queryset = objectToQuerySet(get_object_or_404(User, username=username),
                                model=User)
    return render_queryset_to_response(species_wid=species_wid,
                                       request=request,
                                       models=[User],
                                       queryset=queryset,
                                       templateFile='public/contributor.html')
Beispiel #5
0
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
			}
		)
Beispiel #6
0
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,
			}
		)
Beispiel #7
0
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,
        })
Beispiel #8
0
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', ''),
                                       })
Beispiel #9
0
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', ''),
			})