def test_instance(self): """ Test for data returned by restframework :return: """ u = User.objects.get(username="******") c = Category.objects.get(name="general") loc = Location.objects.get(lid="Other") #Create an issue data = { 'short_desc': 'Test1', 'desc': 'Test 1 desc', 'submitted_by': u.id, 'category': c.id, 'location': loc.lid } form = IssueForm(data) self.assertTrue(form.is_valid(), form.errors.as_json()) form.save() issue = Issue.objects.get(short_desc="Test1") self.assertTrue(issue.created_date) self.assertTrue(issue.modified_date) self.assertEqual(issue.get_absolute_url(), f'/issues/{issue.id}/')
def store(request): if request.method == "POST": form = IssueForm(request.POST) if form.is_valid(): try: form.save() except Exception as e: pass return redirect('/issues')
def item(request, issue_id): issue = get_object_or_404(Issue, id = issue_id) form = IssueForm(instance = issue) if request.method == 'POST': form = IssueForm(data = request.POST, instance = issue) if form.is_valid(): form.save() return redirect('issues:index') return render(request, 'issues/item.html', { 'form': form, 'issue': issue })
def item(request, issue_id): issue = get_object_or_404( Issue, id = issue_id ) if request.method == 'GET': form = IssueForm( instance = issue ) elif request.method == 'POST': form = IssueForm( data = request.POST, instance = issue ) if form.is_valid(): form.save() return redirect('issue_index') return render( request, 'issues/item.html', { 'form': form, 'issue': issue })
def _test_view_issues_list(self): """ This test the /issues/list view. 1. Non managers (is_staff, is_superuser, in engineers group) list view should only show those items submitted by the logged in non manager. 2. If manager is logged in /issues/list should show list of all open issues :return: """ c = Category.objects.get(name="general") u = User.objects.get(username="******") #login as oscar login = self.client.login(username='******', password='******') #new issue data = {'short_desc': 'Test1', 'desc': 'Test 1 desc', 'submitted_by': u.id, 'category': c.id} form = IssueForm(data) form.save() #Non Manger logged in response=self.client.get('/issues/list/') #print(f'VIEW ISSUES (NON MGR): {response.content}') self.assertContains(response,'<h2>My Issues</h2>',html=True) self.assertContains(response, f'ajax": "/issues/list/data/?submitted_by={u.id}"') response=self.client.get(f'/issues/list/data/?assigned_to=2&submitted_by={u.id}') self.assertContains(response, '"recordsTotal": 1, "recordsFiltered": 1') #Manager Logged in u = User.objects.get(username="******") login = self.client.login(username='******', password='******') #check My Issues which is default response=self.client.get('/issues/list/') #print(f'VIEW ISSUES (MGR): {response.content}') self.assertContains(response,'<h2>My Issues</h2>',html=True) self.assertContains(response, f'"ajax": "/issues/list/data/?assigned_to=2&submitted_by={u.id}"') #Check unassigned issues response=self.client.get('/issues/list/?f=ua') self.assertContains(response,'<h2>Unassigned Issues</h2>',html=True) self.assertContains(response, f'"ajax": "/issues/list/data/?assigned_to=0&completed=0"') #Check open issues response=self.client.get('/issues/list/?f=oi') self.assertContains(response,'<h2>Open Issues</h2>',html=True) self.assertContains(response, f'"ajax": "/issues/list/data/?completed=0"')
def new_issue(request, slug): project = Project.objects.get(slug=slug) if request.POST.get('milestone_date'): # call method that modifies post data for our custom milestone handling post_data = append_new_milestone(request.POST.copy(), project, None) request.POST = post_data request.POST = append_new_tags(request.POST.copy(), None) form = IssueForm(request.POST) if form.is_valid(): issue = form.save(commit=False) issue.priority = 0 issue.creator = request.user issue.project = project if (issue.assigned_to): issue.status = 'AS' else: issue.status = 'UA' issue.save() form.save_m2m() return HttpResponse(json.dumps({'status': 'success', 'url': project.get_absolute_url()}), mimetype='application/json') else: return HttpResponse(json.dumps({'status': 'error', 'errors': form.errors}), mimetype='application/json')
def update(self, request, project_number, issue_id): """ Update the issue """ log.debug("PUT request from user %s for issue id %s" % ( request.user, issue_id)) proj = Project.objects.get(project_number=project_number) issue = Issue.objects.get(id=issue_id) log.debug("Fetched object from database %s" % issue) if not check_project_write_acl(proj, request.user): log.debug("Refusing PUT request for project %s from user %s" % ( project_number, request.user )) return rc.FORBIDDEN form = IssueForm(request.POST, instance=issue) if form.is_valid(): t = form.save(commit=False) if request.POST['update'] != '': if not request.user.get_full_name(): update_name = request.user.username else: update_name = request.user.get_full_name() t.history = '''\n\n------Updated by %s on %s------\n\n%s\n\n%s''' % ( update_name, time.strftime("%Y-%m-%d %H:%M"), form.cleaned_data.get('update'), issue.history ) log.debug('Saving %s back to database' % t) t.save() return t else: resp = rc.BAD_REQUEST resp.write(form.errors) log.debug('Validation errors with %s' % t) t.save() return resp
def new_issue(request, slug): project = Project.objects.get(slug=slug) if request.POST.get('milestone_date'): # call method that modifies post data for our custom milestone handling post_data = append_new_milestone(request.POST.copy(), project, None) request.POST = post_data request.POST = append_new_tags(request.POST.copy(), None) form = IssueForm(request.POST) if form.is_valid(): issue = form.save(commit=False) issue.priority = 0 issue.creator = request.user issue.project = project if (issue.assigned_to): issue.status = 'AS' else: issue.status = 'UA' issue.save() form.save_m2m() return HttpResponse(json.dumps({ 'status': 'success', 'url': project.get_absolute_url() }), mimetype='application/json') else: return HttpResponse(json.dumps({ 'status': 'error', 'errors': form.errors }), mimetype='application/json')
def issue_edit(request, pk): issue = issue_by_number(pk) form = IssueForm(request.POST or None, instance=issue) if form.is_valid(): obj = form.save(commit=False) obj.save() return HttpResponseRedirect(reverse("issues-view", args=[pk,])) return direct_to_template(request, "issue_edit.html", extra_context={ "form": form, "issue": issue, "nav": {"selected": "issues",}, })
def create_issue(request, template='issues/create_issue.html'): if request.method == 'POST' and request.POST: form = IssueForm(request.POST) if form.is_valid(): issue = form.save(commit=False) issue.submitter = request.user issue.save() messages.success(request, u"Issue created, pending approval.") return HttpResponseRedirect(reverse('issue-detail', args=[issue.id] )) else: form = IssueForm() return render_to_response(template, { 'form': form, }, context_instance=RequestContext(request))
def create(self, request, project_number): """ Create a new Issue """ log.debug("POST request from user %s to create a new issue" % request.user) proj = Project.objects.get(project_number=project_number) if not check_project_write_acl(proj, request.user): log.debug("Refusing POST request for project %s from user %s" % ( project_number, request.user )) return rc.FORBIDDEN # Go ahead and create the issue.... form = IssueForm(request.POST) t = form.save(commit=False) t.save() proj.issues.add(t) proj.save() return t
def issue_add(request): issue_form = IssueForm(request.POST or request.GET or None) group_form = GroupForm(request.POST or request.GET or None) if issue_form.is_valid() and group_form.is_valid(): obj = issue_form.save(commit=False) obj.creator = request.user obj.save() if group_form.cleaned_data.get("group"): group = Group.get(group_form.cleaned_data["group"]) IssueGroup(group=group, issue=obj).save() return HttpResponseRedirect(reverse("issues-list")) return direct_to_template(request, "issue_add.html", extra_context={ "issue_form": issue_form, "group_form": group_form, "nav": {"selected": "issues", "subnav": "add"}, })
def issue_edit(request, pk): issue = issue_by_number(pk) form = IssueForm(request.POST or None, instance=issue) if form.is_valid(): obj = form.save(commit=False) obj.save() return HttpResponseRedirect(reverse("issues-view", args=[ pk, ])) return direct_to_template(request, "issue_edit.html", extra_context={ "form": form, "issue": issue, "nav": { "selected": "issues", }, })
def index(request): if request.method == 'GET': if request.GET.get('fixed',False): issues = Issue.objects.filter(status__in = [2,4]).order_by('-date_closed','-date_created') else: issues = Issue.objects.exclude(status__in = [2,4]) return render(request, 'issues/index.html', { 'issues': issues, 'show_fixed': 'fixed' in request.GET.keys() }) elif request.method == 'POST': form = IssueForm(data = request.POST) if form.is_valid(): issue = form.save() issue.username = request.user.username issue.save() return redirect('issues:index') else: return render(request, 'issues/new.html', { 'form': form })
def index(request): if request.method == 'GET': if request.GET.get('fixed',False): issues = Issue.objects.filter( status__in = [2,4] ).order_by('-date_closed','-date_created') else: issues = Issue.objects.exclude( status__in = [2,4] ) return render( request, 'issues/index.html', { 'issues': issues, 'show_fixed': 'fixed' in request.GET.keys() }) elif request.method == 'POST': form = IssueForm( data = request.POST ) if form.is_valid(): issue = form.save() issue.username = request.user.username issue.save() return redirect('issue_index') else: return render( request, 'issues/new.html', { 'form': form })
def issue_add(request): issue_form = IssueForm(request.POST or request.GET or None) group_form = GroupForm(request.POST or request.GET or None) if issue_form.is_valid() and group_form.is_valid(): obj = issue_form.save(commit=False) obj.creator = request.user obj.save() if group_form.cleaned_data.get("group"): group = Group.get(group_form.cleaned_data["group"]) IssueGroup(group=group, issue=obj).save() return HttpResponseRedirect(reverse("issues-list")) return direct_to_template(request, "issue_add.html", extra_context={ "issue_form": issue_form, "group_form": group_form, "nav": { "selected": "issues", "subnav": "add" }, })