コード例 #1
0
    def get_last_meter_reading(cls, municipality, current_date):
        """
        Based on the specified month/year, get the meter reading for said
        municipality for the previous month, or None

        :param municipality: Municipality to check
        :param current_date: The current month/year with day set to 01
        :return: the ElectricityRegister record or None
        """
        # get the start end dates for the current month
        start, end = get_month_start_end(current_date)
        # determine the previous month/year
        previous_month_year = get_previous_month_year(current_date)

        # select the `newest` ElectricityRegister->meter_reading that falls
        # under previous_month_year
        result = DBSession.query(
            cls.json_data[cls.METER_READING_FIELD].astext)\
            .select_from(MunicipalitySubmission)\
            .join(MunicipalitySubmission.submission)\
            .filter(MunicipalitySubmission.municipality == municipality,
                    Submission.date >= previous_month_year,
                    Submission.date < start)\
            .order_by(desc(Submission.date))\
            .limit(1)\
            .first()
        return result and result[0] or None
コード例 #2
0
 def test_volume_calculation(self):
     self.setup_test_data()
     with transaction.manager:
         DBSession.query(MonthlyRejectsDensity).update({
             'status': Submission.APPROVED
         })
     municipality = Municipality.get(Municipality.name == "Mukono")
     rejects_landfilled = DailyRejectsLandfilled(
         date=date(2014, 6, 13),
         json_data={
             'barrows_number_frm_sieving': '10'
         },
         municipality_submission=MunicipalitySubmission(
             municipality=municipality
         )
     )
     self.assertEqual(rejects_landfilled.volume(), 6.25)
コード例 #3
0
 def test_get_monthly_rejects_density_returns_none_if_exists(self):
     self.setup_test_data()
     with transaction.manager:
         DBSession.query(MonthlyRejectsDensity).update({
             'status': Submission.APPROVED
         })
     municipality = Municipality.get(Municipality.name == "Mukono")
     rejects_landfilled = DailyRejectsLandfilled(
         date=date(2014, 6, 13),
         municipality_submission=MunicipalitySubmission(
             municipality=municipality
         )
     )
     monthly_rejects_landfilled = rejects_landfilled\
         .get_monthly_rejects_density()
     self.assertIsInstance(monthly_rejects_landfilled,
                           MonthlyRejectsDensity)
コード例 #4
0
 def test_create_or_update_report(self):
     self.setup_test_data()
     with transaction.manager:
         DBSession.query(MonthlyRejectsDensity).update({
             'status': Submission.APPROVED
         })
     municipality = Municipality.get(Municipality.name == "Mukono")
     rejects_landfilled = DailyRejectsLandfilled(
         date=date(2014, 6, 13),
         json_data={
             'barrows_number_frm_sieving': '10'
         },
         municipality_submission=MunicipalitySubmission(
             municipality=municipality
         )
     )
     report = rejects_landfilled.create_or_update_report()
     self.assertEqual(report.report_json, {
         'volume': 6.25,
         'tonnage': 1.7999999999999998
     })
コード例 #5
0
 def __call__(self, node, value):
     try:
         skip = DBSession.query(Skip)\
             .filter(
                 Skip.municipality_id == self.municipality_id,
                 Skip.skip_type == value)\
             .one()
     except NoResultFound:
         # no match
         pass
     else:
         if (self.skip_id is None
                 or (self.skip_id is not None and self.skip_id != skip.id)):
             raise colander.Invalid(node, self.msg.format(skip_type=value))
コード例 #6
0
    def average_distance_travelled(self, start_date, end_date):
        submission_subclass = CompostSalesRegister
        query = (DBSession.query(
            func.avg(submission_subclass.json_data[
                submission_subclass.DISTANCE_TRAVELLED].cast(Float))).join(
                    MunicipalitySubmission,
                    (MunicipalitySubmission.submission_id
                     == submission_subclass.id)).
                 filter(MunicipalitySubmission.municipality == self).filter(
                     submission_subclass.status == Submission.APPROVED).filter(
                         and_(Submission.date >= start_date,
                              Submission.date <= end_date)))

        return query.first()[0]
コード例 #7
0
 def __getitem__(self, item):
     from composting.models.municipality_submission import (
         MunicipalitySubmission)
     try:
         submission = DBSession.query(Submission)\
             .join(MunicipalitySubmission)\
             .filter(Submission.id == item)\
             .one()
     except NoResultFound:
         raise KeyError
     else:
         submission.__parent__ = self
         submission.name = item
         return submission
コード例 #8
0
    def fuel_consumption(self, start_date, end_date):
        submission_subclass = DailyVehicleDataRegister
        query = (DBSession.query(
            sqla_sum(submission_subclass.json_data[
                submission_subclass.FUEL_PURCHASED_LTRS].cast(Float))).join(
                    MunicipalitySubmission,
                    (MunicipalitySubmission.submission_id
                     == submission_subclass.id)).
                 filter(MunicipalitySubmission.municipality == self).filter(
                     submission_subclass.status == Submission.APPROVED).filter(
                         and_(Submission.date >= start_date,
                              Submission.date <= end_date)))

        return query.first()[0]
コード例 #9
0
 def get_compost_density(self, municipality):
     start, end = get_month_start_end(self.date)
     # with our date as ref. find the density for the month
     self._compost_density = self._compost_density\
         or DBSession.query(CompostDensityRegister)\
         .select_from(MunicipalitySubmission)\
         .join(MunicipalitySubmission.submission)\
         .filter(MunicipalitySubmission.municipality == municipality,
                 CompostDensityRegister.date >= start,
                 CompostDensityRegister.date <= end,
                 CompostDensityRegister.status == Submission.APPROVED)\
         .order_by(desc(CompostDensityRegister.date))\
         .limit(1)\
         .first()
     return self._compost_density
コード例 #10
0
    def get_municipality_from_payload(cls, json_payload):
        """
        Try to determine the municipality the submission user belongs to from
        the payload
        :return: Municipality or None
        """
        submitted_by = json_payload.get(constants.SUBMITTED_BY)
        if submitted_by is None:
            return None

        # find the user and join to municipality
        try:
            municipality = DBSession.query(Municipality)\
                                    .select_from(User)\
                                    .filter(User.username == submitted_by)\
                                    .join(Municipality).one()
        except NoResultFound:
            return None
        else:
            return municipality
コード例 #11
0
 def get_by_date(cls, date, municipality, *criterion):
     from composting.models.municipality_submission import\
         MunicipalitySubmission
     """
     Tries to retrieve newest compost density record for whichever month is
     specified by date
     :param date: the target month
     :return:
     """
     start, end = get_month_start_end(date)
     return DBSession.query(cls)\
         .select_from(MunicipalitySubmission)\
         .join(cls)\
         .filter(
             cls.xform_id == cls.XFORM_ID,
             cls.date >= start,
             cls.date <= end,
             MunicipalitySubmission.municipality == municipality,
             *criterion)\
         .order_by(desc(cls.date))\
         .first()
コード例 #12
0
    def get_average_density(cls, date):
        """
        Get the average density for the month that said date falls in
        """
        # get the minimum threshold
        threshold_min = cls.THRESHOLD_MIN
        settings = get_current_registry().settings
        if settings:
            threshold_min = int(settings['monthly_density_threshold_min'])

        # determine the start and end days for the month
        start, end = get_month_start_end(date)

        # get monthly density records that
        monthly_densities = DBSession.query(cls)\
            .filter(
                cls.date >= start, cls.date <= end,
                cls.status == Submission.APPROVED)\
            .all()
        if len(monthly_densities) >= threshold_min:
            return cls.calculate_average_density(monthly_densities)
        else:
            return None
コード例 #13
0
 def get_report_by_date(cls, date, municipality):
     return DBSession.query(SiteReport)\
         .filter(and_(
             extract('month', SiteReport.report_date) == date.month,
             extract('year', SiteReport.report_date) == date.year,
             SiteReport.municipality == municipality)).one()
コード例 #14
0
 def get_users_by_group_query(self, group):
     return DBSession.query(User)\
         .filter(User.group == group,
                 User.municipality == self)
コード例 #15
0
 def get_skips(self, *criterion):
     return DBSession.query(Skip)\
         .filter(Skip.municipality == self, *criterion)\
         .all()
コード例 #16
0
 def get_items_query(cls, *criterion):
     return DBSession.query(cls)\
         .filter(*criterion)
コード例 #17
0
 def test_get_item(self):
     submission = DBSession.query(Submission).first()
     submission_id = submission.id
     item = SubmissionFactory(testing.DummyRequest())[submission.id]
     self.assertIsInstance(item, Submission)
     self.assertEqual(submission_id, item.id)