Beispiel #1
0
def tag():
    """Tag Photo"""
    form = MultiTagForm()
    if form.validate_on_submit():
        tags = form.data['tags'].split(',')

        photo = Query(Photo).get(form.data['photoid'])
        relation = photo.relation('tags')

        for tag in tags:
            # Obtain existed tag by name
            results = Query(Tag).equal_to('name', tag).find()
            if len(results) != 0:
                avostag = results[0]
                avostag.increment('count', 1)
            else:
                avostag = Tag()
                avostag.set('name', tag)
                avostag.save()
            contributors = avostag.relation('contributors')
            contributors.add(g.user)
            avostag.save()
            # Add relation to photo
            relation.add(avostag)
        photo.save()

        query = Relation.reverse_query('Tag', 'contributors', g.user)
        count = query.count()
        return render_template('site/tag/done.html', user_tag_count=count)
    else:
        total = Query.do_cloud_query('select count(*) from Photo')
        try:
            # query = Query(Photo).descending('createdAt').skip(randint(0, total.count))
            # item = query.first()
            query = Query(Photo).descending('createdAt').skip(randint(0, total.count)).limit(5)
            results = query.find()

            jsonresult = json.dumps([o.dump() for o in results])

            query = Relation.reverse_query('PhotoTag', 'contributors', g.user)
            count = query.count()

            categories = Query(Category).find()
            return render_template('site/tag/tag.html', current_photo=results[0],
                coming_photos=jsonresult, utagcount=count, categories=categories, form=form)
        except LeanCloudError, e:
            return redirect(url_for('site.about'))
 def _get_notes(self):
     notes = []
     all_notes = Relation.reverse_query('Note', 'tags', self).find()
     for note in all_notes:
         author = note.get('author')
         author.fetch()
         if author == current_user:
             notes.append(note)
     return notes
Beispiel #3
0
def load():
    page = int(request.form['page'])
    catid = request.form['category']
    if catid:
        category = Query(Category).get(catid)
        query_by_cat = Relation.reverse_query('Photo', 'category', category)
        photos = query_by_cat.skip((page-1) * 24).limit(24).find()
    else:
        photos = Query(Photo).equal_to('featured', True).skip((page-1) * 24).limit(24).find()
    
    jsonresult = json.dumps([o.dump() for o in photos])
    return jsonify(result=jsonresult)
Beispiel #4
0
def tagit():
    tags = request.form['tags'].split(',')
    photo = Query(Photo).get(request.form['photoid'])

    ptags_relation = photo.relation('ptags')

    for tag in tags:
        # Check if tag name existed
        results = Query(Tag).equal_to('name', tag).find()
        if len(results) != 0:
            # existed
            avostag = results[0]
        else:
            # not existed
            # save general tag
            avostag = Tag()
            avostag.set('name', tag)
            avostag.save()
        
        contributors = avostag.relation('contributors')
        contributors.add(g.user)
        avostag.save()

        ptaglist = ptags_relation.query().equal_to('tag', avostag).find()
        if len(ptaglist) == 0:
            # 标签未在该照片标记
            ptag = PhotoTag()
            ptag.set('tag', avostag)
            # 该标签被打在新的照片上
            avostag.increment('count', 1)
            avostag.save()
        else:
            # 标签已在该照片标记
            ptag = ptaglist[0]
            ptag.increment('count', 1)

        ptag.relation('contributors').add(g.user)
        ptag.save()

        # 给照片加标签
        ptags_relation.add(ptag)

    photo.save()

    query = Relation.reverse_query('PhotoTag', 'contributors', g.user)
    count = query.count()
    # count = photo.relation('ptags').query().count()

    return json.dumps({'status':'OK', 'count':count, 'photoid': request.form['photoid']});
Beispiel #5
0
def hot(cat=None):
    categories = Query(Category).ascending('order').find()
    
    cat_name = request.args.get('category')
    cats = Query(Category).equal_to('name', cat_name).find()
    cat = None
    if len(cats):
        cat = cats[0]
        query_by_cat = Relation.reverse_query('Photo', 'category', cat)
        # query_by_feature = Query(Photo).equal_to('featured', True)
        # main_query = Query.and_(query_by_cat, query_by_feature)
        photos = query_by_cat.limit(24).find()
    else:
        photos = Query(Photo).equal_to('featured', True).descending('createdAt').limit(24).find()
    return render_template('site/hot/hot.html', 
        categories=categories, 
        current_cat=cat,
        photos=photos)
Beispiel #6
0
def search(keyword=None):
    keyword = request.args.get('q')
    results = Query(Tag).equal_to('name', keyword).find()

    if len(results) != 0:
        tag = results[0]
        # tag per photo
        print tag.get('name')
        ptags = Query(PhotoTag).equal_to("tag", tag).find()

        photos = []
        for ptag in ptags:
            # print ptag.get('tag').get('name')
            plist = Relation.reverse_query('Photo', 'ptags', ptag).find()
            print len(plist)
            photos.extend(plist)
    else:
        photos = []
    return render_template('site/search/search.html', keyword=keyword, photos=photos)
Beispiel #7
0
def test_create_relation():
    album = Album()
    r = Relation(album, 'band')
    assert r
def test_create_relation():  # type: () -> None
    album = Album()
    r = Relation(album, "band")
    assert r