Example #1
0
def posts():
    form = QuickPost()
    url = form.link.data
    random = form.random.data
    user = current_user.get_id()
    collection = app.config['POSTS_COLLECTION']
    print url
    regex_http = re.compile(r'^(?:http|ftp)s?://')
    if request.method == 'POST' and request.files['file']:
        print 'upload to imgur'
        file = request.files['file']
        if file and allowed_file(file.filename):
            collection = app.config['POSTS_COLLECTION']
            filename = secure_filename(file.filename)
            g = os.path.join(app.config['DIR_PATH'],
                             app.config['UPLOAD_FOLDER'])
            sav = file.save(os.path.join(g, filename))
            user = current_user.get_id()
            _id = app.config['IMGUR_ID']
            _secret = app.config['IMGUR_SECRET']
            tags = ['External', 'Imgur']
            try:
                img = Imgur(_id, _secret)
                f = img.Image_Upload(os.path.join(g, filename))
                entry_make = "<img src=%s><br><a href='%s' target='_blank'>%s</a>" % (
                    f['link'], f['link'], f['link'])
                try:
                    collection.insert({
                        "title": f['id'],
                        "entry": entry_make,
                        "user": user,
                        "created_on": datetime.datetime.now(),
                        "tags": tags
                    })
                    os.remove(os.path.join(g, filename))
                    print 'file deleted'
                    return redirect(url_for('posts'))
                except:
                    flash("Something went bad....", category='error')
            except:
                flash("Something went bad...on imgur", category='error')

    if request.method == 'POST' and regex_http.findall(url):
        print 'checking if user is OK'
        if current_user.is_authenticated() and url != None:
            print 'User is OK'
            handle_posts(url)

    if request.method == 'POST' and random:
        entry_make = random
        tags = ['Random', 'Quick']
        try:
            collection.insert({
                "title": 'Random',
                "entry": entry_make,
                "user": user,
                "created_on": datetime.datetime.now(),
                "tags": tags
            })
            return redirect(url_for('posts'))
        except:
            flash("Something went bad....", category='error')

    page, per_page, offset = get_page_items()
    count = app.config['POSTS_COLLECTION'].find().sort(u'_id', -1).count()
    p = app.config['POSTS_COLLECTION'].find().sort(
        u'_id', -1).limit(per_page).skip(offset)

    pagination = get_pagination(page=page,
                                per_page=per_page,
                                total=count,
                                record_name='posts')
    return render_template('posts.html',
                           posts=p,
                           pagination=pagination,
                           form=form)