Ejemplo n.º 1
0
    def sync_slides(self, ppt, file):
        """
        make slide indexes to database logs. 
        if an slide with the same title and body already exist
        """
        slides = self.parse_ppt(file)
        oldslides = ppt.slides  # get all the slides in the current ppt.
        cur_slides = []

        # looping over the slides list from parsed list.
        for s in slides:
            # find collection of slides that have the same title and body, and order them by page.
            slide_collection = Slide.query.filter_by(ppt_id=ppt.id,
                                                     title=s['title'],
                                                     body=s['body']).order_by(
                                                         Slide.page).all()
            found = False

            if slide_collection:
                # using collection to deal with the rare case where user create multiple new replicate slides at a random page.
                # if that happens, will update the pages and note/tag in the order of their page.
                # for the last page of the replicates, because it is not in the cur_slides collection, will be created as new slide.
                for slide in slide_collection:
                    # if a slide in the collection is not been seen yet, update it accordingly.
                    # the net effect is basically reindexing all slides with same title and body.
                    if slide not in cur_slides:
                        slide.ppt = ppt
                        slide.page = s['page']
                        slide.date = s['date']
                        # update the tag and notes:
                        for t in POSSIBLE_TAGS.split('|'):
                            setattr(slide, t, s.get(t, None))
                        found = True
                        break
            if not found:
                # create new if not found.
                slide = Slide(ppt_id=ppt.id, **s)
                db.session.add(slide)
            # maintain a log of slides that are already been indexed.
            cur_slides.append(slide)

        # remove deleted slides; if the slide have note or tag, trash it by remove its ppt_id.
        for slide in (set(oldslides) - set(cur_slides)):
            if slide.note or slide.tag:
                slide.ppt_id = None
            else:
                db.session.delete(slide)
        db.session.commit()
        return ppt
Ejemplo n.º 2
0
def ppt_search_handler(query, field, ppt, date_from=None, date_to=None):
    """
    in the order of:
    query string, search field, ppt list
    test, ['all'], ['all','tag'], ['all']
    string, ['9'], ['all', 'title', 'body'], ['15', '16']
    """
    page = request.args.get('page', 1, int)
    pagelimit = current_user.slide_per_page
    thumbnail = current_user.thumbnail
    kwargs = {}
    for k in request.args:
        kwargs[k] = (request.args.getlist(k))

    date_from = date_from or datetime(2010, 1, 1)
    date_to = date_to or datetime.now()
    kwargs.pop('page', None)
    entries, total = Slide.search_in_id(query, field, ppt, date_from, date_to,
                                        page, pagelimit)
    start, end = pagination_gaps(page, total, pagelimit)
    next_url = url_for('main.search', page=page +
                       1, **kwargs) if total > page * pagelimit else None
    prev_url = url_for('main.search', page=page -
                       1, **kwargs) if page > 1 else None
    page_url = [(i, url_for('main.search', page=i, **kwargs))
                for i in range(start, end + 1)]

    tags_list = Slide.tags_list()

    return render_template('ppt/index.html',
                           title='Slide Search result',
                           entries=entries,
                           thumbnail=thumbnail,
                           next_url=next_url,
                           prev_url=prev_url,
                           table='slide',
                           nextcontent=None,
                           tags_list=tags_list,
                           page_url=page_url,
                           active=page,
                           id=id)
Ejemplo n.º 3
0
def new_slide():
    form = NewSlideForm()
    if form.validate_on_submit():
        data = {"image_url": form.image_url.data, "url": form.url.data}

        newslide = Slide(**data)
        db.session.add(newslide)
        db.session.commit()
        time.sleep(0.5)
        return redirect("/slides")

    return utils.render_with_navbar("slide/form.html", form=form)
Ejemplo n.º 4
0
def user_follow_slides():
    ppt_id = request.args.get('id', 0)
    pagelimit = current_user.slide_per_page
    thumbnail = current_user.thumbnail
    page = request.args.get('page', 1, type=int)
    if ppt_id:
        slides_id = current_user.follow_ppt_update().get(str(ppt_id), [])
        if not slides_id:
            return redirect(url_for('ppt.index', table='slide', id=ppt_id))
    else:
        slides_id = [
            i for k in current_user.follow_ppt_update().values() for i in k
        ]
    entries = Slide.query.filter(Slide.id.in_(slides_id)).order_by(
        Slide.date.desc(), Slide.page.desc()).paginate(page, pagelimit, False)
    totalpages = entries.total
    start, end = pagination_gaps(page, totalpages, pagelimit, gap=15)

    next_url = url_for(
        'ppt.user_follow_slides',
        page=entries.next_num,
    ) if entries.has_next else None
    prev_url = url_for(
        'ppt.user_follow_slides',
        page=entries.prev_num,
    ) if entries.has_prev else None
    page_url = [(i, url_for(
        'ppt.user_follow_slides',
        page=i,
    )) for i in range(start, end + 1)]
    return render_template('ppt/slide_comparison.html',
                           title='PPT Updates',
                           entries=entries.items,
                           next_url=next_url,
                           prev_url=prev_url,
                           tags_list=Slide.tags_list(),
                           page_url=page_url,
                           active=page,
                           backurl=request.referrer,
                           mode="follow_ppt",
                           ppt_id=ppt_id,
                           thumbnail=thumbnail)
Ejemplo n.º 5
0
def bookmarked():
    pagelimit = current_user.slide_per_page
    thumbnail = current_user.thumbnail
    page = request.args.get('page', 1, type=int)
    when = [(j, i) for i, j in enumerate(current_user.bookmarked_ppt)]
    if when:
        entries = Slide.query.filter(Slide.id.in_(
            current_user.bookmarked_ppt)).order_by(
                db.case(when,
                        value=Slide.id)).paginate(page, pagelimit, False)
    else:
        entries = Slide.query.filter_by(id=None).paginate(
            page, pagelimit, False)
    totalpages = entries.total

    start, end = pagination_gaps(page, totalpages, pagelimit, gap=15)

    next_url = url_for(
        'ppt.bookmarked',
        page=entries.next_num,
    ) if entries.has_next else None
    prev_url = url_for(
        'ppt.bookmarked',
        page=entries.prev_num,
    ) if entries.has_prev else None
    page_url = [(i, url_for(
        'ppt.bookmarked',
        page=i,
    )) for i in range(start, end + 1)]

    return render_template('ppt/slide_comparison.html',
                           title='PPT-Bookmark',
                           entries=entries.items,
                           next_url=next_url,
                           prev_url=prev_url,
                           tags_list=Slide.tags_list(),
                           page_url=page_url,
                           active=page,
                           backurl=request.referrer,
                           mode="bookmarked_ppt",
                           thumbnail=thumbnail)
Ejemplo n.º 6
0
def index():
    """
    main browsing page
    """
    pagelimit = current_user.slide_per_page
    thumbnail = current_user.thumbnail
    table = request.args.get('table', None)
    if not table:
        return redirect(url_for('ppt.index', table='project'))

    id = request.args.get('id', 0, type=int)
    page = request.args.get('page', 1, type=int)
    target = models_table_name_dictionary.get(table, None)

    tag = request.args.get('tag', None)
    if table == 'tags' and tag == None:
        return render_template(
            'ppt/index.html',
            title=(table.upper() or ' ') + '-View',
            table=table,
            entries=Slide.tags_list(500, True),
        )

    author = request.args.get('author', None)
    if table == 'authors' and author == None:
        return render_template(
            'ppt/index.html',
            title=(table.upper() or ' ') + '-View',
            table=table,
            entries=Slide.authors_list(),
        )

    if table in ['trash', 'tags', 'authors']:
        target = Slide

    tags_list = Slide.tags_list()
    if target:
        if id:
            if table == 'ppt':
                entries = target.query.filter_by(project_id=id).order_by(
                    target.date.desc()).paginate(page, pagelimit, False)
            elif table == 'slide':
                entries = target.query.filter_by(ppt_id=id).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            else:
                entries = target.query.order_by(target.date.desc()).paginate(
                    page, pagelimit, False)
        else:
            if table == 'trash':
                entries = target.query.filter_by(ppt_id=None).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'tags':
                table = 'slide'
                entries = target.query.filter(
                    target.tag.contains(tag)).order_by(
                        target.date.desc(),
                        target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'authors':
                table = 'slide'
                author = None if author == 'Anonymous' else author
                entries = target.query.filter(
                    target.author == author).order_by(
                        target.date.desc(),
                        target.page.desc()).paginate(page, pagelimit, False)
            elif table == 'slide':
                entries = target.query.filter(target.ppt_id != None).order_by(
                    target.date.desc(),
                    target.page.desc()).paginate(page, pagelimit, False)
            else:
                entries = target.query.order_by(target.date.desc()).paginate(
                    page, pagelimit, False)
        nextcontent = {'ppt': 'slide', 'project': 'ppt'}.get(table)
        kwargs = {}
        if id:
            kwargs.update(id=id)
        totalpages = entries.total
        start, end = pagination_gaps(page, totalpages, pagelimit, gap=15)

        next_url = url_for(
            'ppt.index', table=table, page=entries.next_num, **
            kwargs) if entries.has_next else None
        prev_url = url_for(
            'ppt.index', table=table, page=entries.prev_num, **
            kwargs) if entries.has_prev else None
        page_url = [(i, url_for('ppt.index', table=table, page=i, **kwargs))
                    for i in range(start, end + 1)]
        return render_template('ppt/index.html',
                               title=(table.upper() or ' ') + '-View',
                               entries=entries.items,
                               next_url=next_url,
                               prev_url=prev_url,
                               table=table,
                               nextcontent=nextcontent,
                               tags_list=tags_list,
                               page_url=page_url,
                               active=page,
                               id=id,
                               thumbnail=thumbnail)

    return render_template('ppt/index.html',
                           title=(table.upper() or ' ') + '-View',
                           table=table,
                           tags_list=tags_list,
                           thumbnail=thumbnail)
Ejemplo n.º 7
0
def index():
    posts = Post.query.order_by(Post.timestamp.desc()).limit(10)
    slides = Slide.query.order_by(Slide.id_.desc()).all()
    if not slides:
        slides.append(Slide(image_url="//:0", url="/"))
    return render_with_navbar("index.html", posts=posts, slides=slides)
Ejemplo n.º 8
0
 def add(info_dict):
     slide = Slide(**info_dict)
     db.session.add(slide)
     db.session.commit()