def post(self, request, *args, **kwargs): summary_attribute_form = SummaryAttributeForm(request.POST) if summary_attribute_form.is_valid(): summary_attribute_form.save() return_url = request.POST.get('return_url', reverse("h1ds-summary-homepage")) return HttpResponseRedirect(return_url) else: return render_to_response('h1ds_summary/form.html', {'form': summary_attribute_form, 'submit_url':reverse('add-summary-attribute')}, context_instance=RequestContext(request))
def post(self, request, *args, **kwargs): summary_attribute_form = SummaryAttributeForm(request.POST) if summary_attribute_form.is_valid(): summary_attribute_form.save() return_url = request.POST.get('return_url', reverse("h1ds-summary-homepage")) return HttpResponseRedirect(return_url) else: return render_to_response( 'h1ds_summary/form.html', { 'form': summary_attribute_form, 'submit_url': reverse('add-summary-attribute') }, context_instance=RequestContext(request))
def post(self, request, *args, **kwargs): device = Device.objects.get(slug=kwargs['device']) summary_attribute_form = SummaryAttributeForm(request.POST) if summary_attribute_form.is_valid(): summary_attribute = summary_attribute_form.save(commit=False) summary_attribute.device = device summary_attribute.save() return_url = request.POST.get('return_url', reverse("h1ds-summary-homepage")) return HttpResponseRedirect(return_url) else: return render_to_response('h1ds_summary/form.html', {'form': summary_attribute_form, 'device': device, 'submit_url': reverse('add-summary-attribute', args=args, kwargs=kwargs)}, context_instance=RequestContext(request))
def get_summary_attribute_form_from_url(request): """Take a H1DS web URL and return a SummaryAttributeForm. The request should contain a 'url' POST query with a URL pointing to a data node (can include filters). The URL must point to a path in the same django instance. We first call the URL and check what datatype is returned so we know which SQL datatype to use, then we pre-fill a form to be complete by the user. """ # Get the requested URL from the POST data attr_url = request.POST['url'] # Split URL into [scheme, netloc, path, params, query, fragments] parsed_url = urlparse(attr_url) # parsed_url is an immutable ParseResult instance, copy it to a (mutable) list parsed_url_list = [i for i in parsed_url] # Now we can update the URL query string to enforce the JSON format. parsed_url_list[4] = '&'.join([parsed_url[4], 'format=json']) # And here is our original URL with format=json query added attr_url_json = urlunparse(parsed_url_list) # Get the django view function corresponding to the URL path view, args, kwargs = resolve(parsed_url_list[2]) node_ancestry = kwargs['nodepath'].split("/") node = Node.datatree.get_node_from_ancestry(node_ancestry) #url_processor = URLProcessor(url=kwargs['url']) # Create a new query dict from the queries in the requested URL, # i.e. data filters, etc... new_query = QueryDict(parsed_url_list[4]).copy() # Insert our new query dict into the request sent to this view... request.GET = new_query # use HTTP GET, not POST request.method = "GET" request.POST = None # ...and use this request to call the view function and get the data # for the requested URL. kwargs['request'] = request url_response = view(*args, **kwargs) # url_response content is a string, let's convert it to a dictionary # using the json parser. json_data = json.loads(url_response.content) # Now we generalise the URL for any shot, replacing the shot number # with __shot__ general_url = attr_url_json.replace(str(node.shot.number), "__shot__") # Create a SummaryAttributeForm with URL and data type entries pre-filled summary_attribute_form = SummaryAttributeForm( initial={'source': general_url}) # If the request is from AJAX, return form in JSON format # TODO: provide ajax / sidebar attribute adding. # if request.is_ajax(): # Otherwise, forward user to form entry page return render_to_response('h1ds_summary/form.html', { 'form': summary_attribute_form, 'submit_url': reverse('add-summary-attribute') }, context_instance=RequestContext(request))