def push_facilities(request): states = DBSession.query(State).order_by("name asc").all() header = ['CNES', 'state', 'municipality', 'facility_name'] rows = [] for state in states: municipalities = state.children() for municipality in municipalities: clinics = municipality.children() for clinic in clinics: if clinic is not None: clinic_name = normalizeString(clinic.name) clinic_cnes = clinic.code municipality_name = normalizeString(municipality.name) state_name = normalizeString(state.name) rows.append([ clinic_cnes, fmt(state_name), fmt(municipality_name), fmt(clinic_name) ]) filename = 'clinics.csv' request.response.content_disposition = 'attachment;filename=' + filename return {'header': header, 'rows': rows}
def get_active_periods(cls): # today = get_current_date() # return DBSession.query(ReportingPeriod).filter( # ReportingPeriod.start_date <= today).order_by( # "start_date desc").all() return DBSession.query(ReportingPeriod).order_by( "start_date asc").all()
def __getitem__(self, item): # try to retrieve the user whose id matches item try: user_id = int(item) user = DBSession.query(User).filter_by(id=user_id).one() except (ValueError, NoResultFound): raise KeyError else: user.__parent__ = self user.__name__ = item return user
def __getitem__(self, item): # try to retrieve the clinic whose id matches item try: clinic_id = int(item) clinic = DBSession.query(Clinic).filter_by(id=clinic_id).one() except (ValueError, NoResultFound): raise KeyError except OperationalError: pass else: clinic.__parent__ = self clinic.__name__ = item return clinic
def edit(self): period = self.request.context form = Form(ReportingPeriodForm().bind(request=self.request, period=period), buttons=('Save', ), css_class='form-horizontal', appstruct=period.appstruct) if self.request.method == "POST": post = self.request.POST.items() try: payload = form.validate(post) except ValidationFailure: self.request.session.flash( _(u"Please fix the errors indicated below."), 'error') else: if payload['form_xpath'] not in ALLOWED_XPATHS and \ valid_year(payload['form_xpath']) is None: self.request.session.flash( _(u"Form Xpath Only allows numeric values 1-5 and" " calendar years (YYYY)."), 'error') elif DBSession.query(ReportingPeriod).filter_by( form_xpath=payload['form_xpath']).first(): self.request.session.flash(_(u"Form Xpath Exists"), 'error') else: period.update(**payload) self.request.session.flash( _(u"Your changes have been saved"), 'success') return HTTPFound( self.request.route_url('periods', traverse=('list'))) # render form return {'form': form, 'period': period}
def create(self): form = Form(ReportingPeriodForm().bind(), buttons=('Save', ), css_class='form-horizontal') if self.request.method == "POST": post = self.request.POST.items() try: payload = form.validate(post) except ValidationFailure: pass else: if payload['form_xpath'] not in ALLOWED_XPATHS and \ valid_year(payload['form_xpath']) is None: self.request.session.flash( _(u"Only numeric values 1-5 and calendar " "years (YYYY) allowed For Form Xpath"), 'error') elif DBSession.query(ReportingPeriod).filter_by( form_xpath=payload['form_xpath']).first(): self.request.session.flash(_(u"Form Xpath Exists"), 'error') else: period = ReportingPeriod(title=payload['title'], form_xpath=payload['form_xpath'], start_date=payload['start_date'], end_date=payload['end_date']) DBSession.add(period) DBSession.flush() self.request.session.flash(_(u"Reporting period created"), 'success') return HTTPFound( self.request.route_url('periods', traverse=('list'))) # render form return {'form': form, 'period': ReportingPeriod.get_current_period()}
def get_num_responses_per_tool(self, period): from whoahqa.models import ClinicSubmission responses_per_tool = {} sample_frames = {} if period == '2017': sample_frames = constants.RECOMMENDED_SAMPLE_FRAMES else: sample_frames = constants.RECOMMENDED_SAMPLE_FRAMES_V2 for tool, sample_frame in sample_frames.iteritems(): responses_per_tool[tool] = { 'sample_frame': sample_frame, 'responses': (DBSession.query(ClinicSubmission).filter( and_(ClinicSubmission.clinic_id == self.id, ClinicSubmission.valid == true(), ClinicSubmission.xform_id == tool, ClinicSubmission.period == period)).distinct( ClinicSubmission.submission_id).count()) } return responses_per_tool
def get_available_periods(cls): today = get_current_date() return DBSession.query(ReportingPeriod).filter( ReportingPeriod.end_date >= today).order_by( "start_date asc").all()
def get_current_period(cls): today = get_current_date() return DBSession.query(ReportingPeriod).filter( ReportingPeriod.start_date <= today).order_by( "start_date desc").limit(1).one()
def get_clinics(self): from whoahqa.models import Clinic clinics = DBSession.query(Clinic).join(user_clinics).filter( user_clinics.columns.user_id == self.id).all() return clinics
def get_active_characteristics(self, period): clinic_characteristics = DBSession.query(ClinicCharacteristics)\ .filter( ClinicCharacteristics.clinic_id == self.id, ClinicCharacteristics.period_id == period.id).all() return clinic_characteristics
def get_unassigned(cls): clinics = DBSession.query(Clinic).outerjoin(user_clinics).filter( user_clinics.columns.clinic_id.is_(None)).all() return clinics