def document_update(request, pk): template_name = "doc_create.html" users = User.objects.filter(is_active=True).order_by('email') form = DocumentForm(users=users) document = Document.objects.filter(id=pk).first() if request.POST: form = DocumentForm(request.POST, request.FILES, instance=document, users=users) if form.is_valid(): doc = form.save(commit=False) doc.save() doc.shared_to.clear() if request.POST.getlist('shared_to'): doc.shared_to.add(*request.POST.getlist('shared_to')) data = {'success_url': reverse_lazy( 'common:doc_list'), 'error': False} return JsonResponse(data) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["doc_obj"] = document context["doc_form"] = form context["doc_file_name"] = context["doc_obj"].document_file.name.split( "/")[-1] context["users"] = users context["sharedto_list"] = [ int(i) for i in request.POST.getlist('shared_to', []) if i] context["errors"] = form.errors return render(request, template_name, context)
def document_create(request): template_name = "doc_create.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser: users = User.objects.filter(is_active=True).order_by('email') elif request.user.google.all(): users = [] else: users = User.objects.filter(role='ADMIN').order_by('email') form = DocumentForm(users=users) if request.POST: form = DocumentForm(request.POST, request.FILES, users=users) if form.is_valid(): doc = form.save(commit=False) doc.created_by = request.user doc.save() if request.POST.getlist('shared_to'): doc.shared_to.add(*request.POST.getlist('shared_to')) data = { 'success_url': reverse_lazy('common:doc_list'), 'error': False } return JsonResponse(data) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["doc_form"] = form context["users"] = users context["sharedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i ] context["errors"] = form.errors return render(request, template_name, context)
def document_update(request, pk): template_name = "doc_create.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser or request.user in Document.objects.get( id=pk).shared_to.all(): users = User.objects.filter(is_active=True).order_by('email') else: users = User.objects.filter(role='ADMIN').order_by('email') document = Document.objects.filter(id=pk).first() form = DocumentForm(users=users, instance=document) if request.POST: form = DocumentForm(request.POST, request.FILES, instance=document, users=users) if form.is_valid(): doc = form.save(commit=False) doc.updated_by = request.user #doc.updated_on = datetime.datetime.now() doc.save() doc.shared_to.clear() if request.POST.getlist('shared_to'): doc.shared_to.add(*request.POST.getlist('shared_to')) if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = doc.shared_to.all().values_list( 'id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: doc.shared_to.add(user_id) if request.POST.getlist('teams', []): doc.teams.clear() doc.teams.add(*request.POST.getlist('teams')) else: doc.teams.clear() data = { 'success_url': reverse_lazy('common:doc_list'), 'error': False } return JsonResponse(data) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["doc_obj"] = document context["doc_form"] = form context["doc_file_name"] = context["doc_obj"].document_file.name.split( "/")[-1] context["users"] = users context["teams"] = Teams.objects.all() context["sharedto_list"] = [ int(i) for i in request.POST.getlist('shared_to', []) if i ] context["errors"] = form.errors return render(request, template_name, context)
def document_create(request): template_name = "doc_create.html" users = [] if request.user.role == 'ADMIN' or request.user.is_superuser: users = User.objects.filter(is_active=True).order_by('email') else: users = User.objects.filter(role='ADMIN').order_by('email') form = DocumentForm(users=users) if request.POST: form = DocumentForm(request.POST, request.FILES, users=users) if form.is_valid(): doc = form.save(commit=False) doc.created_by = request.user doc.save() if request.POST.getlist('shared_to'): doc.shared_to.add(*request.POST.getlist('shared_to')) if request.POST.getlist('teams', []): user_ids = Teams.objects.filter( id__in=request.POST.getlist('teams')).values_list( 'users', flat=True) assinged_to_users_ids = doc.shared_to.all().values_list( 'id', flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: doc.shared_to.add(user_id) if request.POST.getlist('teams', []): doc.teams.add(*request.POST.getlist('teams')) data = { 'success_url': reverse_lazy('common:doc_list'), 'error': False } return JsonResponse(data) return JsonResponse({'error': True, 'errors': form.errors}) context = {} context["TITLE"] = settings.TITLE ## Added for easilt Changeable Title context["doc_form"] = form context["users"] = users context["teams"] = Teams.objects.all() context["sharedto_list"] = [ int(i) for i in request.POST.getlist('assigned_to', []) if i ] context["errors"] = form.errors return render(request, template_name, context)
def post(self, request, format=None): params = request.query_params if len( request.data) == 0 else request.data context = {} users = [] if request.user.role == "ADMIN" or request.user.is_superuser: users = User.objects.filter( is_active=True, company=request.company).order_by("email") else: users = User.objects.filter( role="ADMIN", company=request.company).order_by("email") form = DocumentForm(params, request.FILES, users=users, request_obj=request) if form.is_valid(): doc = form.save(commit=False) doc.created_by = request.user doc.company = request.company doc.save() if params.getlist("shared_to"): doc.shared_to.add(*params.getlist("shared_to")) if params.getlist("teams", []): user_ids = Teams.objects.filter( id__in=params.getlist("teams")).values_list("users", flat=True) assinged_to_users_ids = doc.shared_to.all().values_list( "id", flat=True) for user_id in user_ids: if user_id not in assinged_to_users_ids: doc.shared_to.add(user_id) if params.getlist("teams", []): doc.teams.add(*params.getlist("teams")) data = {"error": False} return Response(data) return Response({"error": True, "errors": form.errors}) context["errors"] = form.errors return Response(context)