Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# https://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficient-reverse-lookups/
prefetch_tables = [
'status_updates',
'status_updates__status_type',
]
if organization.can_transfer_applications:
prefetch_tables += [
'incoming_transfers',
'incoming_transfers__new_application__organization',
'incoming_transfers__status_update',
'incoming_transfers__status_update__application',
'incoming_transfers__status_update__author__profile__organization',
'incoming_transfers__status_update__author',
'incoming_transfers__status_update__author__profile',
]
qset = models.Application.objects.filter(
organization=organization
).select_related(*preselect_tables).prefetch_related(*prefetch_tables)
return qset.order_by('-created').distinct()
def handle(self, *args, **kwargs):
now = timezone.now()
oldest_allowed_submission_date = now - timedelta(days=30)
for org in Organization.objects.all():
latest_login = User.objects.filter(
profile__organization__id=org.id,
profile__organization__is_live=True,
last_login__isnull=False
).values_list('last_login', flat=True).first()
if not latest_login:
continue
unread_applications = Application.objects.filter(
has_been_opened=False,
was_transferred_out=False,
organization__id=org.id,
status_updates__isnull=True
)
unread_applications_count = unread_applications.count()
if unread_applications_count <= 0:
continue
submission_dates = [
app.form_submission.get_local_date_received()
for app in unread_applications
]
oldest_submission_date = min(submission_dates)
fs_count=Count('form_submission_id', distinct=True)
).values('yr', 'wk', 'fs_count').order_by('yr', 'wk')
# Convert the db results to a dict keyed by the week of the year in
# datetime format. The '-1' at the end indicates weeks starting on Mondays.
total_app_data_by_week = {
'%d-%02d-1' % (row['yr'], row['wk']): row['fs_count'] for row in
total_app_data}
# Transform the totals data into something formatted for charting purposes
# with the grand total and weekly time-series data and add it to the
# results we will return for display.
display_data = [total_display_data(total_app_data_by_week, year_weeks)]
# Get counts of applications by organization and week of year
org_app_data = models.Application.objects.annotate(
wk=ExtractWeek('form_submission__date_received',
tzinfo=timezone('US/Pacific')),
yr=ExtractYear('form_submission__date_received',
tzinfo=timezone('US/Pacific'))
).values('organization_id', 'yr', 'wk').annotate(
fs_count=Count('form_submission_id')
).values('organization_id', 'yr', 'wk', 'fs_count').order_by('yr', 'wk')
# Convert the db results to a dict of dicts keyed by org_id and week of the
# year in datetime format. The '-1' at the end indicates weeks starting on
# Mondays.
org_app_data_by_week = {}
for row in org_app_data:
current_org_app_data_by_week = org_app_data_by_week.setdefault(
row['organization_id'], {})
current_org_app_data_by_week['%d-%02d-1' % (row['yr'], row['wk'])] = \
def log_opened(cls, submission_ids, user, time=None):
intake.models.Application.objects.filter(
form_submission_id__in=submission_ids,
organization__profiles__user=user
).distinct().update(
has_been_opened=True)
return cls.log_multiple(cls.OPENED, submission_ids, user, time)
from django import forms
from intake import models
from django.contrib.auth.models import User
from formation.display_form_base import DisplayForm
from formation.fields import (
EmailField, PhoneNumberField, AddressField)
class StatusUpdateForm(forms.ModelForm):
author = forms.ModelChoiceField(
widget=forms.HiddenInput,
queryset=User.objects.all())
application = forms.ModelChoiceField(
widget=forms.HiddenInput,
queryset=models.Application.objects.all())
status_type = forms.ModelChoiceField(
widget=forms.RadioSelect,
queryset=models.StatusType.objects.filter(
is_active=True, is_a_status_update_choice=True
).order_by('display_order'),
empty_label=None)
next_steps = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple,
required=False,
queryset=models.NextStep.objects.filter(is_active=True))
class Meta:
model = models.StatusUpdate
fields = [
'author', 'application', 'status_type',
'additional_information', 'next_steps', 'other_next_step']
def fill_pdf_for_application(application_id):
"""Returns a Filled PDF for the given application_id
Raises an error if no fillable pdf exists or or if it has no file loaded.
"""
app = models.Application.objects.get(id=application_id)
fillable_pdf = models.FillablePDF.objects.filter(
organization_id=app.organization_id).first()
if not fillable_pdf or not fillable_pdf.pdf:
raise exceptions.MissingFillablePDFError(
"{org_name} lacks a pdf to fill for {app}".format(
org_name=app.organization.name, app=app))
return models.FilledPDF.create_with_pdf_bytes(
pdf_bytes=fillable_pdf.fill(app.form_submission),
original_pdf=fillable_pdf,
submission=app.form_submission)
def get_all_unhandled_cnl_apps():
return models.Application.objects.filter(
organization__slug='cfa').exclude(
form_submission__tags__name=constants.COUNTY_NOT_LISTED_HANDLED_TAG)
def add_orgs_to_sub(self, *orgs):
sub = self.extract_sub()
if sub and orgs:
applications = [
intake_models.Application(
form_submission=sub,
organization_id=org_id)
for org_id in coerce_to_ids(orgs)
]
intake_models.Application.objects.bulk_create(applications)
def set_request_scoping_properties(self, request, submission_id, **kwargs):
self.request = request
submission_id = int(submission_id)
self.application = models.Application.objects.filter(
form_submission=submission_id,
organization=request.user.profile.organization).first()
if self.application:
self.application.latest_status = \
models.StatusUpdate.objects.filter(
application_id=self.application.id
).order_by('-created').first()
self.submission = models.FormSubmission.objects.filter(
id=submission_id).first()