def team_edit(request, team_id): if not (request.user.role == 'ADMIN' or request.user.is_superuser): raise PermissionDenied team_obj = get_object_or_404(Teams, pk=team_id) context = {} if request.method == 'GET': form = TeamForm(instance=team_obj) context['form'] = form return render(request, 'team_create.html', context) if request.method == 'POST': form = TeamForm(request.POST, instance=team_obj) if form.is_valid(): task_obj = form.save(commit=False) if 'users' in form.changed_data: update_team_users.delay(team_id) # pass # print('celery task') task_obj.save() form.save_m2m() return JsonResponse({ 'error': False, 'success_url': reverse('teams:teams_list') }) else: return JsonResponse({'error': True, 'errors': form.errors})
def team_create(request): context = {} if request.user.is_superuser: teams = Teams.objects.all() context['teams'] = teams else: raise PermissionDenied if request.method == 'GET': context['form'] = TeamForm() return render(request, 'teams/new.html', context) if request.method == 'POST': form = TeamForm(request.POST, request.FILES) print(form) if form.is_valid(): obj = form.save(commit=False) obj.created_by = request.user # obj.picture = request.FILES['picture'] # obj.picture = request.POST['picture'] # file_type = obj.picture.url.split('.')[-1] # if file_type.lower() not in IMAGE_FILE_TYPES: # return render(request, 'profile_maker/error.html') obj.save() obj.users.set( User.objects.filter(id__in=request.POST.getlist('users'))) form.save_m2m() return redirect('/teams/')
def team_create(request): context = {} if request.user.role == 'ADMIN' or request.user.is_superuser: print('enter') teams = Teams.objects.all() context['teams'] = teams else: raise PermissionDenied if request.method == 'GET': form = TeamForm() context['form'] = TeamForm() return render(request, 'team_create.html', context) if request.method == 'POST': form = TeamForm(request.POST) if form.is_valid(): task_obj = form.save(commit=False) print('full value',task_obj) task_obj.created_by=request.user task_obj.save() form.save_m2m() return JsonResponse({'error': False, 'success_url': reverse('teams:teams_list')}) else: return JsonResponse({'error': True, 'errors': form.errors})
def team_create(request): context = {} if request.user.role == "ADMIN" or request.user.is_superuser: teams = Teams.objects.filter(company=request.company) context["teams"] = teams else: raise PermissionDenied if request.method == "GET": form = TeamForm(request_obj=request) context["form"] = form return render(request, "team_create.html", context) if request.method == "POST": form = TeamForm(request.POST, request_obj=request) if form.is_valid(): task_obj = form.save(commit=False) task_obj.created_by = request.user task_obj.company = request.company task_obj.save() form.save_m2m() return JsonResponse({ "error": False, "success_url": reverse("teams:teams_list") }) else: return JsonResponse({"error": True, "errors": form.errors})
def create_team_view(request, *args, ** kwargs): if request.method == 'GET': context = { 'form': TeamForm(), } return render(request, 'create_team.html', context) else: team_form = TeamForm(request.POST, request.FILES) if team_form.is_valid(): name = team_form.cleaned_data['name'] image = team_form.cleaned_data['image'] team = Team.objects.create( name=name, image=image, created_by=request.user) team.save() return redirect('home view') context = { 'form': team_form, } return render(request, 'create-team/', context)
def teamEdit(request, pk=None): if not request.user.is_authenticated: return redirect('/401/') instance = get_object_or_404(Team, pk=pk) print(instance) if (request.method == 'POST'): form = TeamForm(request.POST, instance=instance) if (form.is_valid): instance = form.save(commit=False) instance.save() messages.success(request, "Successfully updated team") return HttpResponseRedirect(instance.get_absolute_url()) else: messages.error(request, "Error") else: form = TeamForm(instance=instance) context = { "team": instance, "form": form, } return render(request, "teams/team_form.html", context)
def team_edit(request, team_id): if not (request.user.role == "ADMIN" or request.user.is_superuser): raise PermissionDenied team_obj = get_object_or_404(Teams, pk=team_id) if team_obj.company != request.company: raise PermissionDenied actual_users = team_obj.get_users() context = {} if request.method == "GET": form = TeamForm(instance=team_obj, request_obj=request) context["form"] = form return render(request, "team_create.html", context) if request.method == "POST": removed_users = [] form = TeamForm(request.POST, instance=team_obj, request_obj=request) if form.is_valid(): task_obj = form.save(commit=False) if "users" in form.changed_data: update_team_users.delay(team_id) # pass task_obj.save() form.save_m2m() latest_users = task_obj.get_users() for user in actual_users: if user in latest_users: pass else: removed_users.append(user) remove_users.delay(removed_users, team_id) return JsonResponse({ "error": False, "success_url": reverse("teams:teams_list") }) else: return JsonResponse({"error": True, "errors": form.errors})
def post(self, request): form = TeamForm(request.POST) errors = '' if form.is_valid(): authorization = {'authorization': request.session.get('tsuru_token')} response = requests.post('{0}/teams'.format(settings.TSURU_HOST), data=json.dumps(form.data), headers=authorization) if response.status_code == 200: return redirect(reverse("team-list")) errors = response.content return TemplateResponse(request, 'auth/team.html', {'form': form, 'errors': errors})
def get(self, request): return TemplateResponse(request, 'auth/team.html', {"form": TeamForm()})