Ejemplo n.º 1
0
    def test_district_add(self):
        District.add_district('ny', '01', 'lower')

        db.session.commit()

        testdistrict = District.query.get(1)
        self.assertIsNotNone(testdistrict)
        self.assertEqual(testdistrict.state, "ny")
        self.assertEqual(testdistrict.district_num, "01")
        self.assertEqual(testdistrict.house, "lower")
Ejemplo n.º 2
0
    def test_check_district(self):
        District.add_district('ny', '01', 'lower')

        db.session.commit()

        testdistrict = District.query.get(1)
        checkeddistrict = District.check_district('ny', '01', 'lower')
        self.assertEqual(testdistrict.state, checkeddistrict.state)
        self.assertEqual(testdistrict.district_num,
                         checkeddistrict.district_num)
        self.assertEqual(testdistrict.house, checkeddistrict.house)
Ejemplo n.º 3
0
def update_districts():
    from models import District
    doc = doc_from_url(BASE_URL)
    desc = doc.xpath('/html/body/div[4]/dl[1]//a')
    hlid = doc.xpath('/html/body/div[4]/dl[1]//a/@href')
    desc = [d.text for d in desc[1:]]
    hlid = [l.split('/')[-2] for l in hlid[1:]]
    for arg in zip(hlid, desc):
        dist = District(hlid=arg[0], desc=arg[1])
        try:
            dist.save()
            print 'Added new district', dist.desc
        except:
            pass
Ejemplo n.º 4
0
def update_districts():
    from models import District
    doc = doc_from_url(BASE_URL)
    desc = doc.xpath('/html/body/div[4]/dl[1]//a')
    hlid = doc.xpath('/html/body/div[4]/dl[1]//a/@href')
    desc = [d.text for d in desc[1:]]
    hlid = [l.split('/')[-2] for l in hlid[1:]]
    for arg in zip(hlid, desc):
        dist = District(hlid=arg[0], desc=arg[1])
        try:
            dist.save()
            print 'Added new district', dist.desc
        except:
            pass
    def __init__(
            self,  # pylint: disable=too-many-arguments
            user,
            district_factory=None,
            subdistrict_factory=None,
            sporg_factory=None,
            unit_factory=None,
            organization_persister=None):
        self.user = user

        if district_factory:
            self.district_factory = district_factory
        else:
            self.district_factory = District.Factory()
        if subdistrict_factory:
            self.subdistrict_factory = subdistrict_factory
        else:
            self.subdistrict_factory = Subdistrict.Factory()
        if sporg_factory:
            self.sporg_factory = sporg_factory
        else:
            self.sporg_factory = SponsoringOrganization.Factory()
        if unit_factory:
            self.unit_factory = unit_factory
        else:
            self.unit_factory = Unit.Factory()
        if organization_persister:
            self.organization_persister = organization_persister
        else:
            self.organization_persister = Organization.Persister()
Ejemplo n.º 6
0
def push_district(data):
    entry = dict()
    entry["ocd_id"] = data["ocd_id"]
    entry["type_name"] = data["type"]
    entry["party"] = data["party"]
    entry["number"] = data["number"]
    entry["total_population"] = data["demographics"]["total_population"]
    entry["age_out_of"] = data["demographics"]["age"]["out_of"]
    entry["age_stats"] = data["demographics"]["age"]["items"]
    entry["race_out_of"] = data["demographics"]["race"]["out_of"]
    entry["race_stats"] = data["demographics"]["race"]["items"]
    entry["enrollment_out_of"] = data["demographics"]["education"]["enrollment"][
        "out_of"
    ]
    entry["enrollment_stats"] = data["demographics"]["education"]["enrollment"]["items"]
    entry["attainment_out_of"] = data["demographics"]["education"]["attainment"][
        "out_of"
    ]
    entry["attainment_stats"] = data["demographics"]["education"]["attainment"]["items"]
    entry["income_out_of"] = data["demographics"]["income"]["out_of"]
    entry["income_stats"] = data["demographics"]["income"]["items"]
    if "ethnicity" in data["demographics"]:
        entry["ethnicity_out_of"] = data["demographics"]["ethnicity"]["out_of"]
        entry["ethnicity_stats"] = data["demographics"]["ethnicity"]["items"]

    district_db_instance = District(**entry)
    for county in data["counties"]:
        push_county(county, district_db_instance)
    db.session.add(district_db_instance)
def insert_test_data():
    all_objects = []
    with open('test_data/volunteers.json', encoding='utf8') as volunteers_file:
        volunteers_data = json.load(volunteers_file)
        volunteers = [
            Volunteer(id=int(id_), name=vol['name'], userpic=vol['userpic'], phone=vol['phone'])
            for id_, vol in volunteers_data.items()
        ]
    all_objects.extend(volunteers)

    with open('test_data/streets.json', encoding='utf8') as streets_file:
        streets_data = json.load(streets_file)
        streets = [
            Street(id=int(id_), title=st['title'], volunteers=list(filter(lambda x: x.id in st['volunteer'], volunteers)))
            for id_, st in streets_data.items()
        ]
    all_objects.extend(streets)

    with open('test_data/districts.json', encoding='utf8') as districts_file:
        districts_data = json.load(districts_file)
        districts = [
            District(id=int(id_), title=ds['title'], streets=list(filter(lambda x: x.id in ds['streets'], streets)))
            for id_, ds in districts_data.items()
        ]
    all_objects.extend(districts)

    db.session.add_all(all_objects)
    db.session.commit()
Ejemplo n.º 8
0
    def setUp(self):
        """Make sure we start with a clean slate"""

        db.drop_all()
        db.create_all()

        Office.query.delete()
        District.query.delete() 
        Representative.query.delete()
        User.query.delete()
        Interaction.query.delete()
        UserRepresentative.query.delete()

        test_office = Office(phone='123-555-1234', address='123 Test St.', location='district')
        test_district = District(state='ny', district_num='123', house='lower')

        db.session.add(test_office)
        db.session.add(test_district)
        db.session.commit()

        office = Office.query.get(1)
        district = District.query.get(1)

        test_rep = Representative(first_name='Testy', last_name='McTestface', full_name='Testy McTestface', photo_url='https://mn315.net/wp-content/uploads/2018/06/cropped-Ugandan-Knuckles.jpg', email='*****@*****.**', serving=True, district=district, websites=[
                    {
                        "url": "http://www.google.com"
                    },
                    {
                        "url": "http://tesla.com"
                    }
                ])
        test_user = User.register(username='******', password='******', first_name='Some', last_name='User', email='*****@*****.**', address='123 Any St., Anytown NY 12345')
        # login_test = User.register(username='******', password='******', first_name='test', last_name='test', email='*****@*****.**', address='82 Kent Blvd., Salamanca NY 14779')
        db.session.add(test_rep)
        db.session.add(test_user)
        # db.session.add(login_test)
        db.session.commit()

        user = User.query.get(1)
        self.user = user
        # user2 = User.query.get(2)
        rep = Representative.query.get(1)


        user.representatives.append(rep)
        rep.offices.append(office)
        # user2.representatives.append(rep)
        db.session.commit()

        # import pdb
        # pdb.set_trace()

        test_interaction = Interaction(user=user, representative=rep, district=district, interaction_date='2020-07-15 10:00:00', medium='email', topic='stuff and junk', content='all the things')
        db.session.add(test_interaction)

        db.session.commit()
Ejemplo n.º 9
0
def add_district():
    form = AddDistrict()
    user = current_user
    if request.method == "POST":
        #Add district to db.
        db.session.add(
            District(form.name.data, form.shortname.data, form.base_path.data))
        db.session.commit()

    return render_template('add_district.html', form=form, user=user)
Ejemplo n.º 10
0
 def get(self, request):
     response_data = {
         'retCode': error_constants.ERR_STATUS_SUCCESS[0],
         'retMsg': error_constants.ERR_STATUS_SUCCESS[1]
     }
     for item in district:
         code = item.get('code')
         name = item.get('name')
         if not District.objects.filter(name=name, code=code).exists():
             cur_district = District(name=name, code=code)
             try:
                 with transaction.atomic():
                     cur_district.save()
             except Exception as ex:
                 print 'function name: ', __name__
                 print Exception, ":", ex
                 return generate_error_response(
                     error_constants.ERR_SAVE_INFO_FAIL,
                     status.HTTP_500_INTERNAL_SERVER_ERROR)
     return Response(response_data, status.HTTP_200_OK)
Ejemplo n.º 11
0
def process_record(event, context):
    """Lambda facade for Organization.Controller.process_record method"""
    controller = _get_import_controller(event, context)
    message = event['Records'][0]['Sns']['Message']
    data = json.loads(message)

    (district, subdistrict, sporg) = controller.process_record(data)

    District.Persister().save(district)
    Subdistrict.Persister().save(subdistrict)
    SponsoringOrganization.Persister().save(sporg)
    return ''
Ejemplo n.º 12
0
def load_districts():
    data = read_excel('data/tambon.xlsx')
    data['amphoe'] = \
        data['AMPHOE_T'].str.split('.').str.get(1).str.lstrip()
    added_items = set()
    for _, row in data[['amphoe', 'AM_ID', 'CH_ID']].iterrows():
        if str(row['AM_ID']) not in added_items:
            p = Province.query.filter(
                Province.code == str(row['CH_ID'])).first()
            d = District(code=str(row['AM_ID']), name=row['amphoe'])
            db.session.add(d)
            added_items.add(str(row['AM_ID']))
            p.districts.append(d)
            db.session.commit()
    def __init__(self,
                 district_factory=None,
                 subdistrict_factory=None,
                 sporg_factory=None):
        if district_factory:
            self.district_factory = district_factory
        else:
            self.district_factory = District.Factory()

        if subdistrict_factory:
            self.subdistrict_factory = subdistrict_factory
        else:
            self.subdistrict_factory = Subdistrict.Factory()

        if sporg_factory:
            self.sporg_factory = sporg_factory
        else:
            self.sporg_factory = SponsoringOrganization.Factory()
Ejemplo n.º 14
0
def parse_search_result():
    with open('amap_result.json', 'r') as result:
        r_json = json.load(result)
    provinces = []
    cities = []
    districts = []
    # 第一层是国家
    if 'status' in r_json and r_json[
            'status'] == '1' and 'districts' in r_json and 'districts' in r_json[
                'districts'][0]:
        print('=====convert result to model=====')
        for province_json in r_json['districts'][0]['districts']:
            province = District(province_json, None)
            provinces.append(province)
            # print('=====district=====')
            # province.print()
            for city_json in province_json['districts']:
                city = District(city_json, province)
                cities.append(city)

                for district_json in city_json['districts']:
                    district = District(district_json, city)
                    districts.append(district)

        sql = ''
        for province in provinces:
            # province.print()
            sql += province.generate_insert_sql() + '\n'

        for city in cities:
            sql += city.generate_insert_sql() + '\n'

        for district in districts:
            sql += district.generate_insert_sql() + '\n'

        with open('result.sql', 'w') as result:
            result.write(sql)
Ejemplo n.º 15
0
def setup(bot):
    with open("metro-config.json", encoding="utf8") as o:
        configs = json.loads(o.read())

    districts = [
        District(bot, d["cat_id"], d["role_id"], d["station_id"])
        for d in configs["districts"]
    ]

    metro = configs["metro"]
    trains = [
        Train(bot, t["channel_id"], t["role_id"], t["direction"], t["start"])
        for t in metro["trains"]
    ]

    transit_id = metro["cat_id"]
    looped = metro["looped"]
    speed = metro["speed"]
    wait = metro["wait"]

    new_metro = Metro(districts, trains, transit_id, looped, speed, wait)

    return new_metro
Ejemplo n.º 16
0
 def test_district_add_dist_num_error(self):
     # This shouldn't work
     # with self.assertRaises(TypeError):
     #     District.add_district('ny', 1, 'lower')
     with self.assertRaises(exc.IntegrityError):
         District.add_district('ny', None, 'lower')
Ejemplo n.º 17
0
def build_district(name, abbrev, rep_id, wikipedia_link, district):
    """
    Builds a District from the information scrapped

    name            -- the name of the district
    abbrev          -- the state usps abbreviation
    rep_id          -- the ID of the representative
    wikipedia_link  -- the link to the Wikipedia page for this district
    district        -- the dict of scraped information

    Returns the built District
    """

    return District(
        alpha_num=abbrev + '-' + str(name),
        state=abbrev,
        id=name,
        representative_id=rep_id,
        wikipedia_link=wikipedia_link,
        population=district['population'],
        population_male=district['population_male'],
        median_age=float(district['median_age']),
        median_age_male=float(district['median_age_male']),
        median_age_female=float(district['median_age_female']),
        population_white=district['population_white'],
        population_black_or_african_american=district[
            'population_black_or_african_american'],
        population_american_indian_and_alaska_native=district[
            'population_american_indian_and_alaska_native'],
        population_asian=district['population_asian'],
        population_native_hawaiian_and_other_pacific_islander=district[
            'population_native_hawaiian_and_other_pacific_islander'],
        population_some_other_race=district['population_some_other_race'],
        population_two_or_more_races=district['population_two_or_more_races'],
        ethnicity_not_hispanic_or_latino=district[
            'ethnicity_not_hispanic_or_latino'],
        ethnicity_hispanic_or_latino=district['ethnicity_hispanic_or_latino'],
        citizenship_us_citizen_born_in_us=district[
            'citizenship_us_citizen_born_in_us'],
        citizenship_us_citizen_born_in_pr_or_us_island_areas=district[
            'citizenship_us_citizen_born_in_pr_or_us_island_areas'],
        citizenship_us_citizen_born_abroad_of_american_parents=district[
            'citizenship_us_citizen_born_abroad_of_american_parents'],
        citizenship_us_citizen_by_naturalization=district[
            'citizenship_us_citizen_by_naturalization'],
        citizenship_not_a_us_citizen=district['citizenship_not_a_us_citizen'],
        language_speak_only_english=district['language_speak_only_english'],
        language_speak_spanish=district['language_speak_spanish'],
        language_speak_other_languages=district[
            'language_speak_other_languages'],
        marriage_never_married=district['marriage_never_married'],
        marriage_now_married=district['marriage_now_married'],
        marriage_divorced=district['marriage_divorced'],
        marriage_separated=district['marriage_separated'],
        marriage_widowed=district['marriage_widowed'],
        education_less_than_hs=district['education_less_than_hs'],
        education_hs_grad=district['education_hs_grad'],
        education_some_college=district['education_some_college'],
        education_bachelors=district['education_bachelors'],
        education_grad_prof=district['education_grad_prof'],
        income_none=district['income_none'],
        income_9999_less=district['income_9999_less'],
        income_10000_14999=district['income_10000_14999'],
        income_15000_19999=district['income_15000_19999'],
        income_20000_24999=district['income_20000_24999'],
        income_25000_29999=district['income_25000_29999'],
        income_30000_34999=district['income_30000_34999'],
        income_35000_39999=district['income_35000_39999'],
        income_40000_44999=district['income_40000_44999'],
        income_45000_49999=district['income_45000_49999'],
        income_50000_59999=district['income_50000_59999'],
        income_60000_74999=district['income_60000_74999'],
        income_75000_99999=district['income_75000_99999'],
        income_100000_124999=district['income_100000_124999'],
        income_125000_149999=district['income_125000_149999'],
        income_150000_199999=district['income_150000_199999'],
        income_200000_more=district['income_200000_more'],
        veteran=district['veteran'],
        computers_has_one_or_more=district['computers_has_one_or_more'],
        computers_has_desktop_laptop=district['computers_has_desktop_laptop'],
        computers_has_smartphone=district['computers_has_smartphone'],
        computers_has_tablet=district['computers_has_tablet'],
        computers_has_other=district['computers_has_other'],
        computers_none=district['computers_none'],
        internet_has=district['internet_has'],
        internet_has_dialup=district['internet_has_dialup'],
        internet_has_broadband=district['internet_has_broadband'],
        internet_has_cellular_data=district['internet_has_cellular_data'],
        internet_has_satellite=district['internet_has_satellite'],
        internet_none=district['internet_none'])
Ejemplo n.º 18
0
 def test_district_add_state_error(self):
     with self.assertRaises(exc.IntegrityError):
         District.add_district(None, '01', 'lower')
Ejemplo n.º 19
0
 def test_district_add_house_error(self):
     with self.assertRaises(exc.IntegrityError):
         District.add_district('ny', '01', None)
Ejemplo n.º 20
0
from app import app

db.drop_all()
db.create_all()

Office.query.delete()
District.query.delete()
Representative.query.delete()
User.query.delete()
Interaction.query.delete()
UserRepresentative.query.delete()

test_office = Office(phone="123-555-1234",
                     address="123 Test St.",
                     location="district")
test_district = District(state="ny", district_num="123", house="lower")

db.session.add(test_office)
db.session.add(test_district)
db.session.commit()

office = Office.query.get(1)
district = District.query.get(1)

test_rep = Representative(
    given_name="Testy",
    family_name="McTestface",
    name="Testy McTestface",
    image=
    "https://mn315.net/wp-content/uploads/2018/06/cropped-Ugandan-Knuckles.jpg",
    email="*****@*****.**",