Пример #1
0
def manage():
    """Manage the list of RSS
    """

    # Show page
    form = DeleteForm(request.form)
    user = users.get_current_user()
    if request.method != 'POST':
        action = url_for('.manage')
        rss_data = Rss_list.query().filter(Rss_list.user == user)
        return render_template('manage.html',
                               action=action,
                               form=form,
                               rss_data=rss_data)
    # Validate post data
    if form.validate():
        # Check authorization
        rss_key = ndb.Key(urlsafe=form.id.data)
        rss_record = rss_key.get()
        if rss_record.user != user:
            flash('Unauthorized operation!')
            return redirect(url_for('.manage'))
        # Edit RSS list
        if form.submit.data == 'edit':
            session['url'] = rss_record.url
            session['area_tag'] = rss_record.area_tag
            session['item_tag'] = rss_record.item_tag
            session['title_tag'] = rss_record.title_tag
            session['desc_tag'] = rss_record.desc_tag
            session['link_tag'] = rss_record.link_tag
            session['next_link'] = rss_record.next_link
            flash('Please modify your record!')
            return redirect('/')
        # Delete RSS list
        if form.submit.data == 'delete':
            rss_key.delete()
            flash('Record deleted!')
            return redirect(url_for('.manage'))
    else:
        flash('Unauthorized operation!')
        return redirect(url_for('.manage'))
Пример #2
0
def main():
    """Handle data input and test operation
    """
    rss_data = None
    error = False
    form = RSSForm(request.form)
    # Make list for save to
    user = users.get_current_user()
    save_list = None
    if user is not None:
        save_list = Rss_list.query().filter(Rss_list.user == user)

    # Show page
    if request.method == 'GET':
        # Get previous data from session
        form.url.data = session.get('url')
        form.area_tag.data = session.get('area_tag')
        form.item_tag.data = session.get('item_tag')
        form.title_tag.data = session.get('title_tag')
        form.desc_tag.data = session.get('desc_tag')
        form.link_tag.data = session.get('link_tag')
        form.next_link.data = session.get('next_link')
        return render_template('main.html',
                               form=form,
                               rss_data=rss_data,
                               save_list=save_list)

    # Handle post
    if request.method == 'POST' and form.validate():
        # Reset session data
        if form.submit.data == 'reset':
            clean_session()
        else:
            # Get the data from post and clean the data
            url = bleach.clean((form.url.data))
            area_tag = bleach.clean(form.area_tag.data)
            item_tag = bleach.clean(form.item_tag.data)
            title_tag = bleach.clean(form.title_tag.data)
            desc_tag = bleach.clean(form.desc_tag.data)
            link_tag = bleach.clean(form.link_tag.data)
            next_link = bleach.clean(form.next_link.data)
            # Save data to session
            session['url'] = url
            session['area_tag'] = area_tag
            session['item_tag'] = item_tag
            session['title_tag'] = title_tag
            session['desc_tag'] = desc_tag
            session['link_tag'] = link_tag
            session['next_link'] = next_link
            # Save data to ndb
            if form.submit.data == 'save':
                # Goto login if user has not login
                user = users.get_current_user()
                if user is None:
                    return redirect(users.create_login_url('/'))
                rss_data, error = check_data(url=url,
                                             area_tag=area_tag,
                                             item_tag=item_tag,
                                             title_tag=title_tag,
                                             desc_tag=desc_tag,
                                             link_tag=link_tag,
                                             next_link=next_link)

                url_key = bleach.clean((form.id.data))
                if url_key == '':
                    # Save to new record
                    try:
                        rss_list = Rss_list(user=user,
                                            url=url,
                                            area_tag=area_tag,
                                            item_tag=item_tag,
                                            title_tag=title_tag,
                                            desc_tag=desc_tag,
                                            link_tag=link_tag,
                                            next_link=next_link)
                        rss_list.put()
                        flash('RSS record created successfully!')
                    except Exception, e:
                        print e
                        error = False
                        flash('Failed to create RSS record!')
                else:
                    # Save to existed record
                    try:
                        rss_key = ndb.Key(urlsafe=url_key)
                        rss_record = rss_key.get()
                        if rss_record.user != user:
                            error = False
                            flash('Unauthorized operation!')
                            return render_template('main.html',
                                                   form=form,
                                                   rss_data=rss_data,
                                                   save_list=save_list)
                        rss_record.url = url
                        rss_record.area_tag = area_tag
                        rss_record.item_tag = item_tag
                        rss_record.title_tag = title_tag
                        rss_record.desc_tag = desc_tag
                        rss_record.link_tag = link_tag
                        rss_record.next_link = next_link
                        rss_record.put()
                        flash('RSS record saved successfully!')
                    except Exception, e:
                        print e
                        flash('Failed to save RSS record!')

            else:
                rss_data, error = check_data(url=url,
                                             area_tag=area_tag,
                                             item_tag=item_tag,
                                             title_tag=title_tag,
                                             desc_tag=desc_tag,
                                             link_tag=link_tag,
                                             next_link=next_link)
                if error == False:
                    flash('Congretulation! There is no problem!'
                          + ' Please check your result! ')