def bulk_email(request, state): ensure_permission(request.user, state, Permissions.CONTACT) recipients = [] if request.method == "POST": form = EmailForm(request.POST) recipients = request.POST.getlist("recipients") if form.is_valid() and recipients: state = State.objects.get(abbreviation=state.upper()) email = EmailMessage.objects.create( state=state, subject_template=form.cleaned_data["subject_template"], body_template=form.cleaned_data["body_template"], created_by=request.user, ) for recipient in recipients: EmailMessageInstance.objects.create( message=email, official_id=recipient ) return redirect("preview_email", email.id) elif not recipients: messages.error(request, "Must specify at least one recipient.") else: form = EmailForm() # populate form with some data if we're editing edit = request.GET.get("edit") if edit: msg = EmailMessage.objects.get(pk=edit) # if user didn't create the message, make sure they are admin if msg.created_by != request.user: ensure_permission(request.user, state, Permissions.ADMIN) form = EmailForm( dict( subject_template=msg.subject_template, body_template=msg.body_template, ) ) recipients = [o.id for o in msg.officials.all()] officials = ( Official.objects.filter( locality__state__abbreviation=state.upper(), active=True ) .exclude(email="") .annotate( times_contacted=Count("messages"), last_contacted=Max("messages__sent_at") ) ) # keep recipients marked recipients = [int(r) for r in recipients] for o in officials: if o.id in recipients: o.checked = True return render( request, "contact/bulk_email.html", {"form": form, "officials": officials} )
def alter_files(request): transformation = request.POST.get("transformation") alter_files = request.POST.get("alter_files") file_ids = request.POST.getlist("files") files = File.active_files.filter(pk__in=file_ids) assert len(file_ids) == len(files) # ensure permission for state state = files[0].cycle.state if transformation and alter_files: messages.error(request, "Cannot set transformation and file alteration.") elif not transformation and not alter_files: messages.error(request, "Must set transformation or file alteration.") elif transformation: # verify that all files have the same locality and cycle validate_files_for_transformation(files) ensure_permission(request.user, state, Permissions.ADMIN) transformation_func = getattr(tasks, transformation) transformation_func.delay(request.user.id, file_ids) elif alter_files: for f in files: ensure_permission(request.user, f.cycle.state, Permissions.ADMIN) if alter_files == "make_final": # can only move from intermediate to final if f.stage == "I": f.stage = "F" elif alter_files == "deactivate": f.active = False f.save() return redirect("state_admin", state.abbreviation.lower())
def upload_files(request): locality = Locality.objects.get(pk=request.POST["locality"]) ensure_permission(request.user, locality.state, "write") source_url = request.POST.get("source_url", "") stage = request.POST.get("stage", "S") for file in request.FILES.getlist("files"): upload_django_file( file, stage=stage, locality=locality, created_by=request.user, source_url=source_url, ) return redirect("locality_overview", locality.id)
def alter_files(request): transformation = request.POST.get("transformation") alter_files = request.POST.get("alter_files") elections = request.POST.getlist("elections") delete_elections = request.POST.getlist("delete_elections") file_ids = request.POST.getlist("files") files = File.active_files.filter(pk__in=file_ids) assert len(file_ids) == len(files) # ensure permission for state state = files[0].locality.state if transformation and alter_files and (elections or delete_elections): messages.error(request, "Cannot set transformation and file alteration.") elif not transformation and not alter_files and not elections and not delete_elections: messages.error(request, "Must set transformation or file alteration.") elif transformation: # verify that all files have the same locality validate_files_for_transformation(files) ensure_permission(request.user, state, Permissions.ADMIN) transformation_func = getattr(tasks, transformation) transformation_func.delay(request.user.id, file_ids) messages.info(request, f"Sent transformation {transformation} to queue.") elif alter_files or elections or delete_elections: for f in files: ensure_permission(request.user, f.locality.state, Permissions.ADMIN) if alter_files == "make_final": # can only move from intermediate to final if f.stage == "I": f.stage = "F" elif alter_files == "deactivate": f.active = False elif elections: for election in elections: f.statewide_elections.add( StatewideElection.objects.get(id=election)) elif delete_elections: for delete_election in delete_elections: f.statewide_elections.remove( StatewideElection.objects.get(id=delete_election)) f.save() return redirect("state_admin", state.abbreviation.lower())