예제 #1
0
파일: api.py 프로젝트: bavardage/Bookmarks
 def put(self):
     g = self.request.get
     b = Bookmark.get(g('key'))
     if b is None:
         self.post()
     else:
         if b.user == users.get_current_user():
             if g('title'): b.title = g('title')
             if g('link'): b.link = self.get_link(g('link'))
             if g('tags'): b.tags = [db.Category(t) for t in g('tags').split()]
             if g('access'): b.access = g('access')
             b.put()
         else:
             self.do_error('forbidden')
예제 #2
0
파일: views.py 프로젝트: wikty/bookmark
def bookmark_remove(id):
    user = auth.get_logged_in_user()
    bookmark = {}
    try:
        bookmark = Bookmark.get(Bookmark.id == id)
    except Bookmark.DoesNotExist:
        flash(u'你要删除的书签不存在', 'error')
        return redirect(url_for('page_404'))
    
    if request.method == 'POST':
        with db.database.transaction():
            bookmark.destory_image()
            bookmark.delete_instance(recursive=True)
            flash(u'你刚刚删除了一个书签', 'success')
            return redirect(url_for('bookmark'))
    
    return render_template('bookmark_remove.html', bookmark=bookmark, user=user)
예제 #3
0
파일: api.py 프로젝트: bavardage/Bookmarks
    def get(self):
        g = self.request.get
        q = Bookmark.all()
        if g('key'):
            b = Bookmark.get(g('key'))
            if b:
                if self.has_permission_for(b):
                    self.json_output([b,])
                    return
                else:
                    self.do_error('forbidden')
            else:
                self.do_error('not-found')
        if g('tag'):
            q = q.filter('tags=', g('tag'))
        if g('tags'): #union comma sep
            q = q.filter('tags IN', g('tags').split(','))
        if g('all_tags'):
            for t in g('all_tags').split(','):
                q = q.filter('tags=', t)
        if g('link'):
            l = Link.all().filter('url=', g('link')).get()
            if l:
                q = q.filter('link=', l)
        if g('title'):
            q = q.filter('title=', g('title'))
        if g('access'):
            q = q.filter('access=', g('access'))
        if g('user'):
            q = q.filter('user='******'-created')

        try:
            limit = int(g('limit'))
        except:
            limit = 10

        results = [r for r in q.fetch(limit) if self.can_view(r)]
        self.json_output(results)
예제 #4
0
파일: views.py 프로젝트: wikty/bookmark
def bookmark_edit(id):
    user = auth.get_logged_in_user()
    bookmark = {}
    try:
        bookmark = Bookmark.get(Bookmark.id == id)
        bookmark.tags = ' '.join([Tag.get(Tag.id == tagID).name for tagID in [tag.tag for tag in bookmark.Tags]])
    except Bookmark.DoesNotExist:
        flash(u'你要编辑的书签不存在', 'error')
        return redirect(url_for('page_404'))
    
    error = {}
    if request.method == 'POST':
        try:
            bookmark = Bookmark.get(Bookmark.id == request.form['id'])
        except Bookmark.DoesNotExist:
            flash(u'你要编辑的书签不存在', 'error')
            return redirect(url_for('page_404'))
        if not request.form['url']:
            error['url'] = u'书签的网址不能为空'
        if not error:
            try:
                db.database.set_autocommit(False)
                
                # before update image, should remove old version
                if bookmark.url != request.form['url']:
                    bookmark.destory_image()
                    bookmark.url = request.form['url']
                    bookmark.fetch_image()
                
                bookmark.title = request.form['title']
                bookmark.save()
                
                tagnames = re.split('\s+', request.form['tags'].strip())
                # marksure request.form['tags'] not a empty string
                if tagnames[0]:
                    for tagname in tagnames:
                        if not Tag.select().where(Tag.user == user,
                                                      Tag.name == tagname
                                                     ).exists():
                                tag = Tag.create(user=user, name=tagname)
                                tag.save()

                                relationship = Relationship.create(
                                    user=user,
                                    tag=tag,
                                    bookmark=bookmark)
                                relationship.save()
            except Exception as e:
                db.database.rollback()
                flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error')
            else:
                try:
                    db.database.commit()
                except Exception as e:
                    db.database.rollback()
                    flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error')
            finally:
                db.database.set_autocommit(True)

            if not get_flashed_messages():
                flash(u'你刚刚完成一个书签的编辑', 'success')
                return redirect(url_for('bookmark'))

    return render_template('bookmark_edit.html', error=error, form=request.form, bookmark=bookmark, user=user)