示例#1
0
def load_all_apartments_for_map(filter):
    """Loads all apartments for map page

    Args:
        filter (dictionary): filter to apply to search results

    Returns:
        list of apartment dictionary objects
    """

    # Create database session and get apartment objects
    session = connect_to_database('main/apartment_database.db')
    current_page = 1
    apartment_objects, num_pages = get_all_apartments(session,
                                                      current_page,
                                                      filter,
                                                      limit=0)

    # Convert database objects to list of dictionaries
    apartment_list = []
    for u in apartment_objects:
        apartment_dict = convert_database_apartment(u)
        apartment_list.append(apartment_dict)

    # Return list of dictionaries
    return apartment_list
示例#2
0
def load_apartments(filter, current_page, sortation):
    """Loads list of apartments from database, with filter applied to it

    Args:
        filter (dictionary): filter used by user
        current_page (int): current selected page,
        sortation (str): column to sort by

    Returns:
       List of dictionary representation of apartments
    """

    # Connect to database and query for apartments
    session = connect_to_database('main/apartment_database.db')
    apartment_objects, num_pages = get_all_apartments(session, current_page,
                                                      filter, sortation)

    # Convert database objects to dictionaries
    apartment_list = []
    for u in apartment_objects:
        apartment_dict = convert_database_apartment(u)
        apartment_list.append(apartment_dict)

    # Return list of dictionaries
    return apartment_list, num_pages
示例#3
0
def get_agencies():
    session = connect_to_database('main/apartment_database.db')
    agencies_object = get_all_agencies(session)

    agencies_list = []
    for agency in agencies_object:
        agency_dict = {
            'id': agency.__dict__['id'],
            'name': agency.__dict__['name'],
            'baseurl': agency.__dict__['baseurl'],
            'description': agency.__dict__['description']
        }

        rating_list = []
        for rating in agency.__dict__['ratings']:
            rating_dict = {
                'text': rating.__dict__['text'],
                'value': rating.__dict__['value']
            }
            rating_list.append(rating_dict)

        agency_dict['ratings'] = rating_list

        agencies_list.append(agency_dict)

    return agencies_list
示例#4
0
def save_review():
    apartment_id = request.form['id']
    rating = request.form['rating']
    comment = request.form['comment']

    # Insert to database
    session = connect_to_database('main/apartment_database.db')
    save_review_to_database(session, apartment_id, rating, comment)

    # Return success status
    return json.dumps({'status': 'OK'})
示例#5
0
    def test_insertion_single_page(self):
        # Create database
        create_database('test_database.db')
        session = connect_to_database('test_database.db')

        # Create CPM instance
        url = 'http://cpm-apts.com/properties/1102-e-colorado-urbana-il/?tour=1'
        cpm_parser = CpmParser()
        cpm_parser.parse_single_page(url)
        cpm_parser.store_all_to_database(session)

        # Query database to check data was correctly inserted
        all_apartments = session.query(Apartment).all()
        self.assertEqual(url, all_apartments[0].url)
        self.assertEqual(u'2 BR', all_apartments[0].bedrooms)
        self.assertEqual(u'$580', all_apartments[0].price)
    def test_insertion_single_page(self):
        # Create database
        create_database('test_database.db')
        session = connect_to_database('test_database.db')

        # Create JSM instance
        url = 'https://apartments.jsmliving.com/apartments/?unit_type_id=658'
        jsm_parser = JsmParser()
        jsm_parser.parse_single_page(url)
        jsm_parser.store_all_to_database(session)

        # Query database to check data was correctly inserted
        all_apartments = session.query(Apartment).all()
        self.assertEqual(1, len(all_apartments))
        self.assertEqual(
            'https://apartments.jsmliving.com/apartments/?unit_type_id=658',
            all_apartments[0].url)
        self.assertEqual(1, all_apartments[0].bedrooms)
        self.assertEqual(u'$585 - $615', all_apartments[0].price)
示例#7
0
def load_apartment(id):
    """Loads single apartment from database, for given id

    Args:
        id (int): apartment id to load

    Returns:
        apartment as dictionary
    """

    # Connect to database and get apartment object
    session = connect_to_database('main/apartment_database.db')
    apartment_object = get_single_apartment(session, id)

    # Converts database object to dictionary
    if apartment_object:
        apartment_dict = convert_database_apartment(apartment_object)
        return apartment_dict
    else:
        return None
示例#8
0
def main():
    """Handles the parsing and creation of apartment database
    Parses JSM and CPM websites, and stores data to
    sqlite database
    """

    # Setup logger
    setup_logger()

    # Setup the database
    create_database()
    session = connect_to_database()

    # Parse CPM Apartments
    cpm_parser = CpmParser()
    cpm_parser.parse_all()
    cpm_parser.store_all_to_database(session)

    # Parse JSM Apartments
    jsm_parser = JsmParser()
    jsm_parser.parse_all()
    jsm_parser.store_all_to_database(session)