Exemple #1
0
def sub_show(client, nick, *args):
    l_page = client.mod_conf['listing']['page']
    if args:
        tell_more = False
        key = args[0].title()
        if key.isdigit():
            start = int(key) * l_page
            parts = list(Listing.all())[start:start + l_page]
        elif key == 'mine':
            parts = list(Listing.filter(seller=nick))
        elif key == 'only':
            t = ' '.join(args[1:])
            if not t:
                return 'The "show only" sub-command requires an item type!'
            parts = list(Listing.filter(item=t.title()))
        elif key == 'all':
            parts = list(Listing.all())
        else:
            return ('I don\'t know what you\'re trying to do, but you '
                    'might want to take a look at the "help" sub-command!')
    else:
        parts = list(Listing.all())[:l_page]
        tell_more = True
    
    return format_listing(parts, tell_more)
Exemple #2
0
def format_listing(listing, tell_more=False):
    t_count = Listing.count()
    s_count = len(listing)
    if t_count > s_count and tell_more:
        msg = '(Showing %s results out of %s)\n' % (s_count, len(t_count))
    else:
        msg = ''
    msg += ' | '.join(l_disp % i for i in listing)
    
    close_connection()
    return msg or 'There are no items listed!'
Exemple #3
0
def sub_add(client, nick, *args):
    if len(args) < 4 or not (args[0].isdigit() and args[-1].isdigit()):
        return ('Bad arguments! See the sub-command "help".')
    
    if Listing.count(seller=nick) >= client.mod_conf['listing']['max']:
        close_connection()
        return ('You have too many items listed! Use the "remove" sub-command '
                'alongside "show mine" to remove an item first.')
    
    p = int(args[-1])
    n = int(args[0])
    t = ' '.join(args[1:-2])
    w = args[-2]
    Listing(dict(seller=nick, quantity=n, price=p, item=t.lower(), word=w)
            ).add()
    close_connection()
Exemple #4
0
def listing(client, nick, crawler):
    """
    Classifieds for LoF. See the "help" sub-command for more information.
    """
    args = crawler.chain.split()
    try:
        cmd = args.pop(0).lower()
    except IndexError:
        cmd = 'show'
    
    if cmd == 'show':
        return sub_show(client, nick, *args)
    
    elif cmd == 'add':
        return sub_add(client, nick, *args)
    
    elif cmd == 'remove':
        if len(args) != 1 or not args[0].isdigit():
            return 'The "remove" sub-command requires a numeric id!'
        
        item = int(args[0])
        # one or zero matches
        match = Listing.get(seller=nick, id=item)
        if not match:
            return 'You don\'t have an item with that id!'
        match.remove()
        close_connection()
    
    elif cmd == 'help':
        return ('Use: '
                '`show [<page> | only <item name> | mine | all]` '
                 'to display a page of listings, whether a certain '
                 '(defaults to the first) page of all of results, '
                 'all items of a certain type, all of your own items, '
                 'or every single item;\n'
                
                '`add <number> <item name>` at <price>'
                 'to add an item to the listings;\n'
                
                '`remove` <id> to remove one of your items.')
    
    else:
        return ('`%s` is not a valid sub-command; '
                'see the `help` sub-command for more details.') % cmd
Exemple #5
0
def new_listing(session, num_of_bedroom, num_of_bathroom, address, zipcode,
                listing_price, date_of_listing, agent_id, office_id):
    # create house
    house = House(num_of_bedroom=num_of_bedroom,
                  num_of_bathroom=num_of_bathroom,
                  address=address,
                  zipcode=zipcode)
    session.add(house)

    # create listing
    listing = Listing(listing_price=listing_price,
                      date_of_listing=date_of_listing,
                      status='available',
                      agent=session.query(Agent).get(agent_id),
                      office=session.query(Office).get(office_id),
                      house=house)
    session.add(listing)

    session.commit()
    print ("New listing created with price {:d}".format(listing_price))
Exemple #6
0
def seed_data(session,
              num_of_agent=30,
              num_of_office=10,
              num_of_buyer=300,
              num_of_house=500,
              num_of_listing=300,
              num_of_sale=200):

    fake = Faker('en_US')

    # create random agent
    for i in range(num_of_agent):
        agent = Agent(first_name=fake.first_name(),
                      last_name=fake.last_name(),
                      phone_number=fake.phone_number(),
                      email_address=fake.email())
        session.add(agent)
    session.commit()

    # create random offices
    # add many to many relationship between offices and agents
    for i in range(num_of_office):
        agent_ids = random.sample(range(1, num_of_agent + 1),
                                  random.randint(1, min(30, num_of_agent)))
        agents = [session.query(Agent).get(id) for id in agent_ids]
        office = Office(name=fake.color_name(),
                        address=fake.street_address(),
                        agents=agents)
        session.add(office)
    session.commit()

    # create random buyers
    for i in range(num_of_buyer):
        buyer = Buyer(first_name=fake.first_name(),
                      last_name=fake.last_name(),
                      phone_number=fake.phone_number(),
                      email_address=fake.email())
        session.add(buyer)
    session.commit()

    # create random houses
    for i in range(num_of_house):
        house = House(num_of_bedroom=random.randint(1, 8),
                      num_of_bathroom=random.randint(1, 6),
                      address=fake.street_address(),
                      zipcode=fake.postalcode())
        session.add(house)
    session.commit()

    # create random listings
    # connect agent, office, and house with listing
    house_ids = random.sample(range(1, num_of_house + 1), num_of_listing)
    for house_id in house_ids:
        office = session.query(Office).get(random.randint(1, num_of_office))
        agent = random.choice(office.agents)
        listing = Listing(listing_price=1000 * random.randint(10, 5000),
                          date_of_listing=fake.date_between(start_date='-1y',
                                                            end_date='today'),
                          status='available',
                          agent=agent,
                          office=office,
                          house=session.query(House).get(house_id))
        session.add(listing)
    session.commit()

    # create random sales
    # connect agent, office, and listing with sale
    listing_ids = random.sample(range(1, num_of_listing + 1), num_of_sale)
    for listing_id in listing_ids:
        listing = session.query(Listing).get(listing_id)
        listing.status = 'sold'

        # different situations of how the transition between listing and sale happens
        # the logic leaky as the next random choice may be the same as the last one
        # however this is acceptable as we are only to seed some example data
        if random.random(
        ) < 0.6:  # same agent in the same office made the sale
            agent = listing.agent
            office = listing.office
        elif random.random(
        ) < 0.9:  # different agent in the same office made the sale
            office = listing.office
            agent = random.choice(office.agents)
        else:  # different agent in a different officen made the sale
            office = session.query(Office).get(random.randint(
                1, num_of_office))
            agent = random.choice(office.agents)

        # calculate sale date bese on listing date
        listing_datetime = datetime.strptime(listing.date_of_listing,
                                             '%Y-%m-%d')
        days_variant = min((datetime.now() - listing_datetime).days, 90)
        sale_datetime = listing_datetime + timedelta(
            days=random.randint(0, days_variant))
        date_of_sale = listing_datetime.strftime('%Y-%m-%d')

        # decide sale price base on listing price with probability
        if random.random() < 0.6:  # sale price is the same as listing price
            sale_price = listing.listing_price
        else:  # sale price is different from listing price
            price_variant = int(listing.listing_price / 1000 /
                                4)  # 25% of listing price in thousand dollar
            sale_price = listing.listing_price + 1000 * random.randint(
                -price_variant, price_variant)

        # create sale object to insert
        sale = Sale(sale_price=sale_price,
                    date_of_sale=date_of_sale,
                    buyer=session.query(Buyer).get(
                        random.randint(1, num_of_buyer)),
                    agent=agent,
                    office=office,
                    listing=listing)
        session.add(sale)
    session.commit()

    print("Database seeded with random data")