Exemplo n.º 1
0
def index():
    form = SearchForm()
    if request.method == "POST" and form.validate_on_submit():
        return redirect(url_for(".search", query=form.search.data))
    page = request.args.get("page", 1, type=int)
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get("show_followed", ""))
    if show_followed:
        query = current_user.followed_images
    else:
        query = Image.query
    pagination = query.order_by(Image.timestamp.desc()).paginate(page, per_page=current_app.config["IMAGES_PER_PAGE"], error_out=False)
    images = pagination.items
    return render_template("index.html", images=images, pagination=pagination, form=form, show_followed=show_followed)
Exemplo n.º 2
0
def before_request():
    if current_user.is_authenticated:
        current_user.last_seen = datetime.utcnow()
        db.session.commit()
        g.search_form = SearchForm()
    g.locale = str(get_locale())
Exemplo n.º 3
0
def index():
    form = SearchForm()
    if form.validate_on_submit():

        # build data from search filters to send to domain
        data = dict()
        min_bedrooms = int(form.min_bedrooms.data)
        min_bathrooms = int(form.min_bathrooms.data)
        max_price = int(form.max_price.data)
        #surround_suburbs = form.surround_suburbs.data
        postcode = form.postcode.data
        if (min_bedrooms != -1):
            data['minBedrooms'] = min_bedrooms
        if (min_bathrooms != -1):
            data['minBathrooms'] = min_bathrooms
        if (max_price):
            data['maxPrice'] = max_price
        if (postcode):
            loc_data = []
            loc_data.append({'postcode': postcode})
            data['locations'] = loc_data
        #if (postcode or surround_suburbs):
        #    loc_data = []
        #    if (postcode):
        #        loc_data.append({'postcode':postcode})
        #    if (surround_suburbs):
        #        loc_data.append({'includeSurroundingSuburbs':surround_suburbs})
        #    data['locations'] = loc_data
        data['page'] = 1
        data['pageSize'] = 200
        data['listingType'] = 'Rent'
        print(data)

        # send a post request to domain api with json data
        resp = domain.post(
            'https://api.domain.com.au/v1/listings/residential/_search',
            json=data)

        # construct pandas dataframe and save to csv
        df = pd.DataFrame(columns=[\
        'Property type', 'Price', 'Suburb','Postcode','Display address','Bedrooms',\
        'Bathrooms','Headline',\
        'url','Advert type','Advert name','Advert contact'\
        ])
        json_resp = resp.json()
        print(json_resp)
        for j in json_resp:
            df = df.append({\
                'Property type': j['listing']['propertyDetails']['propertyType'],
                'Price': j['listing']['priceDetails']['displayPrice'],
                'Suburb': j['listing']['propertyDetails']['suburb'],\
                'Postcode': j['listing']['propertyDetails']['postcode'],\
                'Display address' : j['listing']['propertyDetails']['displayableAddress'],\
                'Bedrooms': j['listing']['propertyDetails']['bedrooms'],\
                'Bathrooms': j['listing']['propertyDetails']['bathrooms'],\
                'Headline': j['listing']['headline'],\
                'url': 'http://www.domain.com.au/'+j['listing']['listingSlug'],\
                'Advert type': j['listing']['advertiser']['type'],\
                'Advert name': j['listing']['advertiser']['name'],\
                'Advert contact': j['listing']['advertiser']['contacts']\
                }, ignore_index=True)

        df.to_csv(current_app.config['RENTAL_FOLDER'] / 'test.csv',
                  index=False)
        return render_template('main/results.html', data=resp.json())
    return render_template('main/index.html', form=form)
Exemplo n.º 4
0
def before_request():
    if current_user.is_authenticated:
        g.search_form = SearchForm()
Exemplo n.º 5
0
def before_request():
    g.search_form = SearchForm()
Exemplo n.º 6
0
def index():
    form = SearchForm()

    amount_of_available_countries = (
        Country.count_countries() -
        Post.count_different_countries_with_posts())
    amount_of_available_posts = Post.count_available_posts()

    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('main.index', page=posts.prev_num) \
        if posts.has_prev else None

    if request.method == 'POST':
        button_clicked = request.form.get('submit button')

        if button_clicked == 'go to available posts':
            return redirect(
                url_for('recipes.available_posts', country_name='all'))

        if button_clicked == 'pick country':
            if amount_of_available_countries:
                picked_countries = Post.get_my_countries_with_posts()
                country = Country.get_random_country(picked_countries)
                Post.create_empty_post(country.id)
                return redirect(
                    url_for('recipes.country', country_name=country.name))
            flash('You don\'t have any left country to try.')
            flash('Please search for that country you really want' +
                  'to give another try.')
            flash('Or, if you are brave enough, reset the whole list!')
            return redirect(
                url_for('main.user', username=current_user.username))

        if button_clicked == 'search user':
            searched_user = User.get_user_by_username(
                form.search_user_text.data).first()
            if not searched_user is None:
                return redirect(
                    url_for('main.user', username=searched_user.username))
            flash('User {} not found'.format(form.search_user_text.data))

        if button_clicked == 'search country':
            searched_country = Country.get_country_by_name(
                form.search_country_text.data).first()
            if not searched_country is None:
                return redirect(
                    url_for('recipes.country',
                            country_name=searched_country.name))
            flash('Country {} not found'.format(form.search_country_text.data))

    return render_template(
        'index.html',
        title='Home',
        index=True,
        posts=posts.items,
        next_url=next_url,
        prev_url=prev_url,
        form=form,
        amount_of_available_countries=amount_of_available_countries,
        amount_of_available_posts=amount_of_available_posts)
Exemplo n.º 7
0
def before_request():  # tracks when user was last seen, writes to db
    if current_user.is_authenticated:
        current_user.last_seen = datetime.utcnow()
        db.session.commit()
        g.search_form = SearchForm()  # explain
Exemplo n.º 8
0
def before_request():
    if current_user.is_authenticated:
        db.session.commit()
        g.search_form = SearchForm()
Exemplo n.º 9
0
def search():
    form = SearchForm()

    if form.validate_on_submit():
        query = form.query.data
        tags = form.tags.data
        tag_all = False if form.tags_inclusive.data == 'Any' else True
    else:
        query = parse.unquote_plus(request.args.get('q', ''))
        tags = parse.unquote_plus(request.args.get('t', ''))
        form.query.data = query
        form.tags.data = tags
        tag_all = False

    if not query and not tags:
        return render_template('new_search.html', title='Search', form=form)
    else:
        items = None
        if query:
            items = Post.query.search(query, sort=True)
        else:
            items = Post.query

        if tags:
            tags = list(filter(None, tags.split(' ')))
            old_len = len(tags)
            tags = Tag.get_valid(tags)
            new_len = len(tags)

            if (new_len == 0 and query == '') or (tag_all
                                                  and old_len != new_len):
                items = None
            else:
                items = items.join(Post.tags) \
                             .filter(Tag.id.in_(map(lambda t: t.id, tags)))

                if tag_all:
                    items = items.group_by(Post.id) \
                                 .having(func.count(Tag.id) == len(tags))

    if items == None:
        return render_template('no_search_results.html',
                               title='Search',
                               form=form)

    page = request.args.get('p', 1, type=int)
    posts = items.paginate(page, current_app.config['POSTS_PER_PAGE'], True)

    if len(posts.items) == 0:
        return render_template('no_search_results.html',
                               title='Search',
                               form=form)

    next_url = url_for('main.search',
                       p=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.search',
                       p=posts.prev_num) if posts.has_prev else None

    return render_template('search.html',
                           title='Search',
                           form=form,
                           page=page,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url,
                           pagination=posts)
Exemplo n.º 10
0
def before_request():
    g.search_form = SearchForm()
    if request.endpoint == 'main.search':
        g.search_form = SearchForm(term=request.args.get('term'),
                                   filter=request.args.get('filter'))
Exemplo n.º 11
0
def before_request() -> None:
    if current_user.is_authenticated:
        current_user.last_seen = datetime.utcnow()
        db.session.commit()
        g.search_form = SearchForm()
    g.locale = str(get_locale())  # g 'acts a proxy for a werkzeug local'
Exemplo n.º 12
0
def index():
    form = SearchForm()
    if form.validate_on_submit():

        # if first time
        if 'domain' not in session:
            session['domain'] = get_token()

        # get token again if expired
        expiry = datetime.now() + timedelta(
            seconds=session['domain']['expires_in'] - 900)
        if datetime.now() >= expiry:
            session['domain'] = get_token()

        # build data from search filters to send to domain
        data = dict()
        min_bedrooms = int(form.min_bedrooms.data)
        min_bathrooms = int(form.min_bathrooms.data)
        max_price = int(form.max_price.data)

        #surround_suburbs = form.surround_suburbs.data
        postcode = form.postcode.data
        if (min_bedrooms != -1):
            data['minBedrooms'] = min_bedrooms
        if (min_bathrooms != -1):
            data['minBathrooms'] = min_bathrooms
        if (max_price):
            data['maxPrice'] = max_price
        if (postcode):
            loc_data = []
            loc_data.append({'postcode': postcode})
            data['locations'] = loc_data
        #if (postcode or surround_suburbs):
        #    loc_data = []
        #    if (postcode):
        #        loc_data.append({'postcode':postcode})
        #    if (surround_suburbs):
        #        loc_data.append({'includeSurroundingSuburbs':surround_suburbs})
        #    data['locations'] = loc_data
        data['page'] = 1
        data['pageSize'] = 200
        data['listingType'] = 'Rent'

        # send a post request to domain api with json data
        domain_session = get_session(session['domain'])
        resp = domain_session.post(
            'https://api.domain.com.au/v1/listings/residential/_search',
            json=data)

        # construct pandas dataframe and save to csv
        df = pd.DataFrame(columns=[\
        'Property type', 'Price', 'Suburb','Postcode','Display address','Bedrooms',\
        'Bathrooms','Carspaces','Headline','Description',\
        'url','Advert type','Advert name','Advert contact'\
        ])
        json_resp = resp.json()
        for j in json_resp:
            df = df.append({\
                'Property type': get_data(data=j['listing']['propertyDetails'], key='propertyType'),\
                'Price': get_data(data=j['listing']['priceDetails'], key='displayPrice'),\
                'Suburb': get_data(data=j['listing']['propertyDetails'], key='suburb'),\
                'Postcode': get_data(data=j['listing']['propertyDetails'], key='postcode'),\
                'Display address' : get_data(data=j['listing']['propertyDetails'], key='displayableAddress'),\
                'Bedrooms': get_data(data=j['listing']['propertyDetails'], key='bedrooms'),\
                'Bathrooms': get_data(data=j['listing']['propertyDetails'], key='bathrooms'),\
                'Carspaces': get_data(data=j['listing']['propertyDetails'], key='carspaces'),\
                'Headline': get_data(data=j['listing'], key='headline'),\
                'Description': BeautifulSoup(get_data(data=j['listing'], key='summaryDescription'),'html.parser').\
                    get_text().replace('\r','').replace('\n',' '),\
                'url': 'http://www.domain.com.au/'+get_data(data=j['listing'], key='listingSlug'),\
                'Advert type': get_data(data=j['listing']['advertiser'], key='type'),\
                'Advert name': get_data(data=j['listing']['advertiser'], key='name'),\
                'Advert contact': get_contact(get_data(data=j['listing']['advertiser'], key='contacts'))\
                }, ignore_index=True)

        # get current date time for filename
        curr_dt = datetime.now().strftime('%Y%m%d_%H%M%S')
        filename = curr_dt + '_domain.csv'
        df.to_csv(current_app.config['RENTAL_FOLDER'] / filename, index=False)
        return render_template('main/results.html', data=df, filename=filename)

    return render_template('main/index.html', form=form)
Exemplo n.º 13
0
def print_scores(host, q):
    if request.method == 'GET':
        if ('handled_q' in session.keys()) and (session['handled_q'] == q):
            return render_template(
                'main/visualization.html',
                auth=authenticate(
                    host=host,
                    on_callback=True if host == 'client' else False),
                host=host,
                q=q,
                scores=session['results'])
        if re.search("\w", q) is None:
            return redirect(url_for('main.search', host=host))
        auth = authenticate(host=host,
                            on_callback=True if host == 'client' else False)
        if True:  # always run the demo
            date_time_obj = [
                dt.date.today() - dt.timedelta(days=1),
                dt.date.today() - dt.timedelta(days=2),
                dt.date.today() - dt.timedelta(days=3),
                dt.date.today() - dt.timedelta(days=4),
                dt.date.today() - dt.timedelta(days=5),
                dt.date.today() - dt.timedelta(days=6),
                dt.date.today() - dt.timedelta(days=7)
            ]
            dummy_data = [[rand(),
                           rand(),
                           rand(),
                           rand(), date_time_obj[i]]
                          for i in range(len(date_time_obj))]
            dummy_data = pd.DataFrame(dummy_data,
                                      columns=[
                                          "neg_sent", "pos_sent", "comp_sent",
                                          "polarity", "date"
                                      ])
            dummy_data["date"] = pd.to_datetime(dummy_data["date"])
            preprocesser = Preprocesser(dummy_data)
            session['results'] = {
                'd_7_mean_scores':
                preprocesser.simple_mean(to_json=True),
                'd_7_means_by_date':
                preprocesser.by_date_mean(unix=True, to_json=True)
            }
            session['handled_q'] = q
            return render_template('main/visualization.html',
                                   auth=auth,
                                   host=host,
                                   q=q,
                                   scores=session['results'])
        else:
            from app.main.methods.sentiment_engine import analyze_sentiments
            try:
                preprocesser = Preprocesser(
                    analyze_sentiments(
                        extract_tweets_from_q(query=q,
                                              api=twp.API(
                                                  auth,
                                                  wait_on_rate_limit=True),
                                              use_pages=False,
                                              num=200)))
            except TypeError:
                return render_template(
                    'main/visualization.html',
                    auth=auth,
                    host=host,
                    q=q,
                    form=SearchForm(submit_label='Try again'))
            else:
                session['results'] = {
                    'd_7_mean_scores':
                    preprocesser.simple_mean(to_json=True),
                    'd_7_means_by_date':
                    preprocesser.by_date_mean(unix=True, to_json=True)
                }
                print(session['results'])
                session['handled_q'] = q
                return render_template('main/visualization.html',
                                       auth=auth,
                                       host=host,
                                       q=q,
                                       scores=session['results'])
    return redirect(
        url_for('main.print_scores',
                host=host,
                q=SearchForm(submit_label='Try again').build_query()))
Exemplo n.º 14
0
def before_request():
    if current_user.is_authenticated:
        current_user.update_last_seen()
        g.search_form = SearchForm()
    g.locale = str(get_locale())
Exemplo n.º 15
0
def assets():
    form = SearchForm()
    if form.submit3.data and form.validate():
        md5 = form.md5.data
        return redirect(url_for('main.results', md5=md5))
    return render_template('main/admin.html', title='Check Asset', form=form)