コード例 #1
0
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/')
コード例 #2
0
ファイル: views.py プロジェクト: bot242/djangocrm
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})
コード例 #3
0
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})
コード例 #4
0
ファイル: views.py プロジェクト: bot242/djangocrm
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})
コード例 #5
0
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})