我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用django.views.generic.base.View()。
def get(self, request, template_id=None): """Handling GET method. Args: request: Request to View. template_id: id of retrieved template. Returns: if template_id is None returns all templates. otherwise returns single template by given template_id. """ if not template_id: xml_templates = XmlTemplate.get_all() data = [model_to_dict(i) for i in xml_templates] return HttpResponse(json.dumps(data)) xml_template = XmlTemplate.get_by_id(template_id) data = model_to_dict(xml_template) return HttpResponse(json.dumps(data))
def post(self, request): """ Handling POST method. :param request: Request to View. :return: Http response with status code 201 """ data = json.loads(request.body) template = XmlTemplate.get_by_id(data['template_id']).template_content tree = ElementTree.fromstring(template) for area in data['areas']: template_area = tree.find(".//area[@id=\"%s\"]" % (area['id'])) for clip in area['clips']: clip_tag = ElementTree.SubElement(template_area, 'clip') clip_tag.set('id', str(clip['pk'])) clip_tag.set('src', clip['fields']['url']) clip_tag.set('mimetype', clip['fields']['mimetype']) clip_tag.text = clip['fields']['name'] result_template = ElementTree.tostring(tree, encoding="us-ascii", method="xml") project_hash = hashlib.md5(result_template) AdviserProject.update(data['project_id'],project_hash=project_hash, project_template = result_template) return HttpResponse(status=201)
def head(self, request, mac): """ Handling HEAD method. :args request: Request to View. mac: mac-address of player. :return: last modified template's hash sum of player, gotten by mac-address. """ player = Player.objects.get(mac_address=mac) if player.project: hashsum = player.project.project_hash response = HttpResponse('') response['Last-Modified'] = hashsum return response return HttpResponse(status=204)
def put(self, request, company_id): """ Handling put method. :args request: Request to View. company_id: id of company to be updated. :return: HttpResponse with code 201 if company is updated or HttpResponseBadRequest if request contain incorrect data also if user is not superuser . """ if (not request.user.is_superuser) and (Company.get_company(company_id).administrator != request.user.adviseruser): return HttpResponseBadRequest("Permission denied") data = json.loads(request.body) if data.get("administrator"): data["administrator"] = AdviserUser.objects.get(id=data.get("administrator").get("id")) company = Company.get_company(data["id"]) company_form = CompanyForm(data, company) if not company_form.is_valid(): return HttpResponseBadRequest(str(company_form.errors)) company.set_company(data) return HttpResponse(status=201)
def dispatch(self, request, *args, **kwargs): """ HARD OVERRIDE OF View """ handler = self.get_handler(request, *args, **kwargs) # normal behavior if handler is None: if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), None) if handler is None: handler = self.http_method_not_allowed # common work moment before dispatch handler = self.handle_common(handler, request, *args, **kwargs) # dispatch return handler(request, *args, **kwargs)
def post(self, request): """Handling POST method. Args: request: Request to View. Returns: HttpResponse with code 201 if company is added. """ template_name = request.POST.get('templateName') xml_file = request.FILES['file'].read() xml_template = XmlTemplate() xml_template.set(template_name, xml_file) xml_template.save() return HttpResponse(status=201)
def delete(self, request, template_id): """Handling DELETE method. Args: request: Request to View. template_id: id of deleted template. Returns: HttpResponse with code 204 if template is deleted. """ XmlTemplate.delete_by_id(template_id) return HttpResponse(status=204)
def get(self, request, project_id): """ Handling GET method. :param request: Request to View. :param project_id: id of project for which players will be returned :return: Http response with list of players that have current project """ project = AdviserProject.objects.get(id=project_id) if (not request.user.is_superuser) and (project.id_company.id != request.user.adviseruser.id_company.id): return HttpResponseBadRequest("Permission denied") players = Player.objects.filter(project=project_id) data = [model_to_dict(i) for i in players] return HttpResponse(json.dumps(data))
def get(self, request): """ Handling GET method. :param request: Request to View. :return: rendered Monitor page. """ return render_to_response('monitor.html')
def get(self, request, mac): """ Handling GET method. :args request: Request to View. mac: mac-address of player. :return: HttpResponse with project's template and template's hashsum of player, gotten by mac-address. """ player = Player.objects.get(mac_address=mac) template = player.project.project_template hashsum = player.project.project_hash return HttpResponse(json.dumps({"template" : template, 'hashsum': hashsum}))
def get(self, request): """ Handling GET method. :param request: Request to View. :return: HttpResponse with company fields and values by id_company of user which is logined. If user is superuser returns all companies with their fields and values. """ if request.user.is_superuser: company = [model_to_dict(company) for company in Company.get_company()] return HttpResponse(json.dumps(company)) user = request.user.adviseruser company = [model_to_dict(company) for company in Company.filter_company(user.id_company.id)] return HttpResponse(json.dumps(company))
def post(self, request): """ Handling POST method. :param request: Request to View. :return: HttpResponse with code 201 if company is added or HttpResponseBadRequest if request contain incorrect data or user is not superuser. """ company = Company() data = json.loads(request.body) company_form = CompanyForm(data) if not company_form.is_valid(): return HttpResponseBadRequest(str(company_form.errors)) company.set_company(data) return HttpResponse(status=201)
def delete(self, request, company_id): """ Handling DELETE method. args request: Request to View. company_id: id of company to be deleted. :return: HttpResponse with code 201 if company is deleted. HttpResponseBadRequest with 'Permission denied' if user is not superuser. """ Company().delete_company(company_id) return HttpResponse(status=201)
def __init__(self, controller, **kwargs): # model and fields exist on View itself and need to be overwritten super(ControllerMixin, self).__init__(view=self, controller=controller, model=controller.model, fields=controller.fields, **kwargs)
def caseruns(request, templ='report/caseruns.html'): '''View that search caseruns.''' queries = request.GET r_form = RunForm(queries) r_form.populate(queries) context = {} if r_form.is_valid(): runs = SmartDjangoQuery(r_form.cleaned_data, TestRun.__name__) runs = runs.evaluate() caseruns = get_caseruns_of_runs(runs, queries) context['test_case_runs'] = caseruns context['runs'] = runs return render(request, templ, context)