Esempio n. 1
0
def get_subscriber_metadata(request, start=None, end=None, format=None):
    """
Returns metadata about people who have recently subscribed to
curbside waste pickup reminders:

* e.g.,
    * https://apis.detroitmi.gov/website_data/waste_subscribers/
    * https://apis.detroitmi.gov/website_data/waste_subscribers/20190601/
    * https://apis.detroitmi.gov/website_data/waste_subscribers/20190601/20190610/

* optional parameters:
    * start-date - start date of the time window to query (default is 1 week ago) - format YYYYMMDD
    * end-date - end date of the time window to query (default is today) - format YYYYMMDD
    """

    # Parse our date filters
    if start:

        start = parse_date(start)

    else:

        # default start date:  1 week ago
        start = date.today() - timedelta(days=7)

    if end:

        end = parse_date(end)

    else:

        # default end date:  today
        end = date.today()

    subscribers = Subscriber.objects.filter(status='active').filter(
        last_status_update__range=(start, end + timedelta(days=1)))

    content = {
        "filters": {
            "start-date": date_json(start),
            "end-date": date_json(end),
        },
        "subscriber_metadata": []
    }

    for subscriber in subscribers:

        tmp = {
            "address": subscriber.address,
            "lat": subscriber.latitude,
            "lon": subscriber.longitude,
        }
        content["subscriber_metadata"] += [tmp]

    return Response(content)
Esempio n. 2
0
    def json(self):

        # REVIEW: should really pass "-05:00" for add_tz

        return {
            "type": self.detail_type,
            "service": self.service_type,
            "description": self.description,
            "normalDay": util.date_json(self.normal_day, add_tz=False),
            "newDay": util.date_json(self.new_day, add_tz=False),
            "note": self.note,
            "wasteAreaIds": self.waste_area_ids,
        }
Esempio n. 3
0
def get_new_content(request, start=None, end=None, format=None):

    if start:

        start = parse_date(start)

    else:

        # default: most-recent monday
        today = timezone.now().date()
        diff = today.weekday()

        if diff == 0:
            diff = 7

        start = today - timedelta(days=diff)

    if end:

        end = parse_date(end)

    else:

        # default: most-recent sunday
        end = start + timedelta(days=6)

    num_total_pages = get_page_count()
    num_new_pages = get_page_count(start=start, end=end)

    num_total_subscribers = Subscriber.objects.filter(status='active').count()
    num_new_subscribers = Subscriber.objects.filter(status='active').filter(
        created_at__range=(start, end)).count()

    content = {
        "date_start": date_json(start),
        "date_end": date_json(end),
        "num_days": (end - start).days,
        "website_analytics": {
            "num_new_html_pages": num_new_pages,
            "num_total_html_pages": num_total_pages,
        },
        "waste_reminders": {
            "total_subscribers": num_total_subscribers,
            "new_subscribers": num_new_subscribers,
        }
    }

    return Response(content)
Esempio n. 4
0
    def get(self, data_source_name=None, param=None):
        """
        Refresh this dataset (if needed) and returns the datavalue objects.
        """

        data = {}
        updated = None

        data_sources = self.datasource_set.filter(name=data_source_name) if data_source_name else self.datasource_set.all()

        # combine the strings into 1 json object
        for data_source in data_sources:

            data_value = data_source.get(param)
            if data_value and data_value.data:
                json_curr = json.loads(data_value.data)
                self.add_data_json(data, json_curr)

                if not updated or data_value.updated < updated:
                    updated = data_value.updated

        if updated:
            data["updated"] = util.date_json(data_value.updated)

        return data
Esempio n. 5
0
    def to_json(self):

        filename, fileurl = self.move_files()

        return {
            "sketch_id": self.id,
            "date": date_json(self.date),
            "image_url": fileurl,
            "caption": self.caption,
            "is_primary_sketch": self.isPrimarySketch,
        }
Esempio n. 6
0
def clean_parcel_val(val):
    """
    Clean up a parcel value
    """
    if type(val) is str:
        val = val.strip()
    elif type(val) is datetime:
        val = date_json(val)
    elif type(val) is Decimal:
        val = float(val)
    return val
Esempio n. 7
0
    def to_json(self):
        """
        Return json version of this object.
        """

        return {
            "master_account_num":
            self.master_account_num,
            "master_account_name":
            self.master_account_name,
            "sub_account_num":
            self.sub_account_num,
            "sub_account_name":
            self.sub_account_name,
            "short_name":
            self.short_name,
            "account_status":
            self.account_status,
            "group_num":
            self.group_num,
            "item_num":
            self.item_num,
            "original_balance":
            self.convert_decimal(self.original_balance),
            "fed_withholding_tax_this_period":
            self.convert_decimal(self.fed_withholding_tax_this_period),
            "ytd_fed_withholding_tax":
            self.convert_decimal(self.ytd_fed_withholding_tax),
            "int_paid_this_period":
            self.convert_decimal(self.int_paid_this_period),
            "ytd_int_paid":
            self.convert_decimal(self.ytd_int_paid),
            "int_split_this_period":
            self.convert_decimal(self.int_split_this_period),
            "escrow_balance":
            self.convert_decimal(self.escrow_balance),
            "escrow_begin_date":
            date_json(self.escrow_begin_date),
            "escrow_end_date":
            date_json(self.escrow_end_date),
        }
Esempio n. 8
0
def add_license(request):

    if not request.data.get('plate_num'):
        return Response({"message": "'plate_num' is required"}, status=status.HTTP_400_BAD_REQUEST)

    plate_num = request.data['plate_num']

    car_info_data = LicensePlateInfo.objects.filter(plate_num=plate_num)
    if car_info_data.exists():
        return Response({"message": "Error: plate number already added", "date_added": date_json(car_info_data[0].created_at)})

    car_info = LicensePlateInfo(plate_num=plate_num)
    car_info.save()

    return Response({"message": "Plate number added", "date_added": date_json(car_info.created_at)}, status=status.HTTP_201_CREATED)
Esempio n. 9
0
    def test_add_info_twice(self):

        plate_info = LicensePlateInfo(plate_num='dummy12')
        plate_info.save()

        c = Client()

        response = c.post('/car_info/', {"plate_num": "dummy12"})

        expected = {
            "message": "Error: plate number already added",
            "date_added": date_json(plate_info.created_at)
        }

        self.assertEqual(response.status_code, 200)
        self.assertDictEqual(response.data, expected,
                             "A license plate number can be added only once")
Esempio n. 10
0
    def test_add_info(self):

        time_right_now = datetime.now(timezone.utc)

        with patch.object(timezone, 'localtime',
                          return_value=time_right_now) as mock_now:

            c = Client()

            response = c.post('/car_info/', {"plate_num": "dummy12"})

            expected = {
                "message": "Plate number added",
                "date_added": date_json(time_right_now)
            }
            self.assertEqual(response.status_code, 201)
            self.assertDictEqual(response.data, expected,
                                 "License plate number can be added")
Esempio n. 11
0
    def get_url(self):
        """
        Gets url, including auth token (if needed).
        """

        url = self.url

        # Add a socrata syntax where clause?
        #
        # examples of where clause usage:
        #
        # https://data.detroitmi.gov/resource/uzpg-2pfj.json?demolition_date=2018-01-09T00:00:00.000
        # https://data.detroitmi.gov/resource/uzpg-2pfj.json?$where=demolition_date between '2018-01-03T00:00:00.000' and '2018-01-10T00:00:00.000'
        # https://data.detroitmi.gov/resource/uzpg-2pfj.json?$where=demolition_date > '2018-01-03T00:00:00.000'
        #
        if self.socrata_where:
            if url.find("?") < 0:
                url = url + "?"

            where_clause = "$where={}".format(self.socrata_where)
            if where_clause.find("1_week_back") > 0:
                date_val = date.today() - datetime.timedelta(days=7)
                where_clause = where_clause.replace("1_week_back", util.date_json(date_val, add_tz=False))

            url = url + where_clause

        # Do we have credentials token for this data?
        token = None
        if self.credentials:
            success, token = self.credentials.create_auth_token()
            if False == success:
                raise Exception("Authentication failed on credential {}".format(self.credentials))

        # Add auth token to url?
        if token:
            if url.find("?") < 0:
                url = url + "?"
            else:
                url = url + "&"
            url = url + "token={}".format(token)

        return url
Esempio n. 12
0
    def to_json(self):
        """
        Returns json representing this notification.
        """

        data = {
            "id": self.id,
            "day": date_json(self.day),
            "geo_layer_url": self.geo_layer_url,
            "formatter": self.formatter,
            "messages": []
            }

        for message in self.messengermessage_set.all():

            data["messages"].append(message.to_json())

        data["locations"] = get_locations_helper(notification=self)

        return data
Esempio n. 13
0
def get_survey_data(survey):
    answers = [{
        survey_answer.question_id: survey_answer.answer
    } for survey_answer in survey.surveyanswer_set.all()]
    return {
        "id": survey.id,
        "survey_template": survey.survey_template_id,
        "parcel_id": survey.parcel.parcel_id,
        "created_at": date_json(survey.created_at),
        "surveyor": {
            "id": survey.user.id,
            "username": survey.user.username,
            "email": survey.user.email,
        },
        "answers": answers,
        "common_name": survey.common_name,
        "note": survey.note,
        "image_url": survey.image_url,
        "status": survey.status,
    }
Esempio n. 14
0
 def test_date_json_none(self):
     str = util.date_json(None)
     self.assertTrue(str == '',
                     "date_json() converts null objects to empty string")
Esempio n. 15
0
 def test_date_json_datetime(self):
     str = util.date_json(datetime(2017, 5, 1, 00, 00, 00))
     self.assertTrue(str == '2017-05-01T00:00:00.000Z',
                     "date_json() converts datetime object to json")
Esempio n. 16
0
 def __str__(self):  # pragma: no cover (this is mostly just for debugging)
     return "plate: " + self.plate_num + " created_at: " + str(
         date_json(self.created_at))