def contact_list_new(request): data = {} if request.method == "POST": form = ContactListForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.created_by = request.user instance.save() tags = request.POST['tags'].split( ',') if request.POST['tags'] else [] for each in tags: tag, _ = Tag.objects.get_or_create(name=each, created_by=request.user) instance.tags.add(tag) if request.FILES.get('contacts_file'): upload_csv_file.delay(form.validated_rows, form.invalid_rows, request.user.id, [instance.id]) return JsonResponse({ 'error': False, 'data': form.data }, status=status.HTTP_201_CREATED) else: # return JsonResponse({'error': True, 'errors': form.errors}, # status=status.HTTP_400_BAD_REQUEST) return JsonResponse({ 'error': True, 'errors': form.errors }, status=status.HTTP_200_OK) else: return render(request, 'marketing/lists/new.html', data)
def edit_contact_list(request, pk): user = request.user try: contact_list = ContactList.objects.get(pk=pk) except ContactList.DoesNotExist: contact_list = ContactList.objects.none() return JsonResponse({}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': # contact_lists = ContactList.objects.filter(company=request.company) if (user.is_superuser): contact_lists = ContactList.objects.all() else: contact_lists = ContactList.objects.filter(created_by=request.user) data = {'contact_list': contact_list, 'contact_lists': contact_lists} return render(request, 'marketing/lists/new.html', data) else: form = ContactListForm(request.POST, request.FILES, instance=contact_list) if form.is_valid(): instance = form.save() instance.tags.clear() tags = request.POST['tags'].split( ',') if request.POST['tags'] else [] for each in tags: tag, _ = Tag.objects.get_or_create(name=each, created_by=request.user) instance.tags.add(tag) if request.FILES.get('contacts_file'): upload_csv_file.delay(form.validated_rows, form.invalid_rows, request.user.id, [instance.id]) return JsonResponse({ 'error': False, 'data': form.data }, status=status.HTTP_200_OK) return JsonResponse({ 'error': True, 'errors': form.errors }, status=status.HTTP_200_OK)