示例#1
0
def unlike_topic_comment(tid, cid):
    like = CommentLike.query.get((cid, current_user.id))
    if not like:
        raise Conflict(description='You already unliked it')

    comment = get_comment_or_404(tid, cid)
    with db.auto_commit():
        db.session.delete(like)

    with db.auto_commit(False):
        comment.reset_like_count()
    return '', 204
示例#2
0
def unlike_topic_comment(tid, cid):
    like = CommentLike.query.get((cid, current_user.id))
    if not like:
        raise Conflict(description='You already unliked it')

    comment = get_comment_or_404(tid, cid)
    with db.auto_commit():
        db.session.delete(like)

    with db.auto_commit(False):
        comment.reset_like_count()
    return '', 204
示例#3
0
def social_authorize(name):
    social = SocialUser.handle_authorized_response(name)
    if social is None:
        return 'error'

    if current_user and not social.user_id:
        social.user_id = current_user.id
        with db.auto_commit():
            db.session.add(social)

    if social.user_id:
        user = User.cache.get(social.user_id)
        UserSession.login(user, True)
        next_url = session.pop('next_url', '/')
        return redirect(next_url)

    session['social.service'] = social.service
    session['social.uuid'] = social.uuid

    if name == 'google' and social.info.get('verified_email'):
        email = social.info.get('email')
        if email:
            token = create_email_signature(email, 'signup')
            url = url_for('.signup', token=token)
            return redirect(url)

    return 'TODO'
示例#4
0
文件: users.py 项目: valley51/zerqu
def update_current_user():
    user = User.query.get(current_user.id)
    form = UserProfileForm.create_api_form(user)
    form.populate_obj(user)
    with db.auto_commit():
        db.session.add(user)
    return jsonify(dict(user))
示例#5
0
def signup(token):
    email, key = get_email_from_signature(token, 'signup')

    social_service = session.get('social.service')
    social_uuid = session.get('social.uuid')
    if social_service and social_uuid:
        social = SocialUser.query.get((social_service, social_uuid))
        if social.user_id:
            social = None
    else:
        social = None

    form = RegisterForm()
    form.email.data = email
    if form.validate_on_submit():
        user = form.create_user()
        redis.delete(key)

        if social:
            session.pop('social.service', None)
            session.pop('social.uuid', None)
            social.user_id = user.id
            with db.auto_commit():
                db.session.add(social)

        UserSession.login(user, True)
        return redirect('/')

    return render_template(
        'account/signup.html',
        form=form,
        email=email,
        social=social,
    )
示例#6
0
文件: account.py 项目: Gwill/zerqu
def signup(token):
    email, key = get_email_from_signature(token, 'signup')

    social_service = session.get('social.service')
    social_uuid = session.get('social.uuid')
    if social_service and social_uuid:
        social = SocialUser.query.get((social_service, social_uuid))
        if social.user_id:
            social = None
    else:
        social = None

    form = RegisterForm()
    form.email.data = email
    if form.validate_on_submit():
        user = form.create_user()
        redis.delete(key)

        if social:
            session.pop('social.service', None)
            session.pop('social.uuid', None)
            social.user_id = user.id
            with db.auto_commit():
                db.session.add(social)

        UserSession.login(user, True)
        return redirect('/')

    return render_template(
        'account/signup.html',
        form=form,
        email=email,
        social=social,
    )
示例#7
0
文件: account.py 项目: Gwill/zerqu
def social_authorize(name):
    social = SocialUser.handle_authorized_response(name)
    if social is None:
        return 'error'

    if current_user and not social.user_id:
        social.user_id = current_user.id
        with db.auto_commit():
            db.session.add(social)

    if social.user_id:
        user = User.cache.get(social.user_id)
        UserSession.login(user, True)
        next_url = session.pop('next_url', '/')
        return redirect(next_url)

    session['social.service'] = social.service
    session['social.uuid'] = social.uuid

    if name == 'google' and social.info.get('verified_email'):
        email = social.info.get('email')
        if email:
            token = create_email_signature(email, 'signup')
            url = url_for('.signup', token=token)
            return redirect(url)

    return 'TODO'
示例#8
0
文件: forms.py 项目: 343829084/zerqu
 def update_topic(self, user_id):
     topic = getattr(self, '_obj')
     topic.title = self.title.data
     topic.content = self.content.data
     topic.update_link(self.link.data, user_id)
     with db.auto_commit():
         db.session.add(topic)
     return topic
示例#9
0
 def update_topic(self, user_id):
     topic = getattr(self, '_obj')
     topic.title = self.title.data
     topic.content = self.content.data
     topic.update_link(self.link.data, user_id)
     with db.auto_commit():
         db.session.add(topic)
     return topic
示例#10
0
def delete_topic_comment(tid, cid):
    comment = get_comment_or_404(tid, cid)
    if comment.user_id != current_user.id:
        raise Denied('deleting this comment')
    with db.auto_commit():
        db.session.delete(comment)

    TopicStat(tid).calculate()
    return '', 204
示例#11
0
def unlike_topic(tid):
    data = TopicLike.query.get((tid, current_user.id))
    if not data:
        raise Conflict(description='You already unliked it')
    with db.auto_commit():
        db.session.delete(data)

    TopicStat(tid).calculate()
    return '', 204
示例#12
0
def delete_topic_comment(tid, cid):
    comment = get_comment_or_404(tid, cid)
    if comment.user_id != current_user.id:
        raise Denied('deleting this comment')
    with db.auto_commit():
        db.session.delete(comment)

    TopicStat(tid).calculate()
    return '', 204
示例#13
0
def unlike_topic(tid):
    data = TopicLike.query.get((tid, current_user.id))
    if not data:
        raise Conflict(description='You already unliked it')
    with db.auto_commit():
        db.session.delete(data)

    TopicStat(tid).calculate()
    return '', 204
示例#14
0
def like_topic(tid):
    data = TopicLike.query.get((tid, current_user.id))
    if data:
        raise Conflict(description='You already liked it')

    topic = Topic.cache.get_or_404(tid)
    like = TopicLike(topic_id=topic.id, user_id=current_user.id)
    with db.auto_commit():
        db.session.add(like)
    return '', 204
示例#15
0
    def create_comment(self, user_id, topic_id):
        c = Comment(content=self.content.data,
                    topic_id=topic_id,
                    user_id=user_id,
                    reply_to=self.reply_to.data)

        with db.auto_commit():
            db.session.add(c)

        return c
示例#16
0
文件: forms.py 项目: 343829084/zerqu
 def create_topic(self, user_id):
     topic = Topic(
         title=self.title.data,
         content=self.content.data,
         user_id=user_id,
         link=self.link.data,
     )
     with db.auto_commit():
         db.session.add(topic)
     return topic
示例#17
0
def like_topic(tid):
    data = TopicLike.query.get((tid, current_user.id))
    if data:
        raise Conflict(description='You already liked it')

    topic = Topic.cache.get_or_404(tid)
    like = TopicLike(topic_id=topic.id, user_id=current_user.id)
    with db.auto_commit():
        db.session.add(like)
    return '', 204
示例#18
0
 def create_topic(self, user_id):
     topic = Topic(
         title=self.title.data,
         content=self.content.data,
         user_id=user_id,
         link=self.link.data,
     )
     with db.auto_commit():
         db.session.add(topic)
     return topic
示例#19
0
文件: forms.py 项目: 343829084/zerqu
 def create_user(self):
     user = User(
         username=self.username.data,
         email=self.email.data,
     )
     user.password = self.password.data
     user.role = User.ROLE_ACTIVE
     with db.auto_commit():
         db.session.add(user)
     return user
示例#20
0
 def create_user(self):
     user = User(
         username=self.username.data,
         email=self.email.data,
     )
     user.password = self.password.data
     user.role = User.ROLE_ACTIVE
     with db.auto_commit():
         db.session.add(user)
     return user
示例#21
0
文件: cafes.py 项目: valley51/zerqu
def leave_cafe(slug):
    cafe = Cafe.cache.first_or_404(slug=slug)
    ident = (cafe.id, current_user.id)
    item = CafeMember.query.get(ident)

    if not item:
        raise NotFound('CafeMember')

    item.role = CafeMember.ROLE_VISITOR
    with db.auto_commit():
        db.session.add(item)
    return '', 204
示例#22
0
def flag_topic_comment(tid, cid):
    key = 'flag:%d:c-%d' % (current_user.id, cid)
    if cache.get(key):
        return '', 204
    comment = get_comment_or_404(tid, cid)
    # here is a concurrency bug, but it doesn't matter
    comment.flag_count += 1
    with db.auto_commit():
        db.session.add(comment)
    # one person, one flag
    cache.inc(key)
    return '', 204
示例#23
0
 def create_cafe(self, user_id):
     cafe = Cafe(
         name=self.name.data,
         slug=self.slug.data,
         description=self.description.data,
         permission=Cafe.PERMISSIONS[self.permission.data],
         user_id=user_id,
         style=self.style,
     )
     with db.auto_commit():
         db.session.add(cafe)
     return cafe
示例#24
0
def flag_topic_comment(tid, cid):
    key = 'flag:%d:c-%d' % (current_user.id, cid)
    if cache.get(key):
        return '', 204
    comment = get_comment_or_404(tid, cid)
    # here is a concurrency bug, but it doesn't matter
    comment.flag_count += 1
    with db.auto_commit():
        db.session.add(comment)
    # one person, one flag
    cache.inc(key)
    return '', 204
示例#25
0
文件: forms.py 项目: 343829084/zerqu
    def create_comment(self, user_id, topic_id):
        c = Comment(
            content=self.content.data,
            topic_id=topic_id,
            user_id=user_id,
            reply_to=self.reply_to.data
        )

        with db.auto_commit():
            db.session.add(c)

        return c
示例#26
0
文件: forms.py 项目: 343829084/zerqu
 def create_cafe(self, user_id):
     cafe = Cafe(
         name=self.name.data,
         slug=self.slug.data,
         description=self.description.data,
         permission=Cafe.PERMISSIONS[self.permission.data],
         user_id=user_id,
         style=self.style,
     )
     with db.auto_commit():
         db.session.add(cafe)
     return cafe
示例#27
0
def write_read_percent(tid):
    topic = Topic.cache.get_or_404(tid)
    percent = request.get_json().get('percent')
    if not isinstance(percent, int):
        raise APIException(description='Invalid payload "percent"')
    read = TopicRead.query.get((topic.id, current_user.id))
    if not read:
        read = TopicRead(topic_id=topic.id, user_id=current_user.id)
    read.percent = percent

    with db.auto_commit():
        db.session.add(read)
    return jsonify(percent=read.percent)
示例#28
0
def write_read_percent(tid):
    topic = Topic.cache.get_or_404(tid)
    percent = request.get_json().get('percent')
    if not isinstance(percent, int):
        raise APIException(description='Invalid payload "percent"')
    read = TopicRead.query.get((topic.id, current_user.id))
    if not read:
        read = TopicRead(topic_id=topic.id, user_id=current_user.id)
    read.percent = percent

    with db.auto_commit():
        db.session.add(read)
    return jsonify(percent=read.percent)
示例#29
0
文件: account.py 项目: Gwill/zerqu
def change_email(token):
    email, key = get_email_from_signature(token, 'email')
    user = User.query.filter_by(email=email).first_or_404()
    form = EmailForm()
    if form.validate_on_submit():
        user.email = form.email.data
        with db.auto_commit():
            db.session.add(user)
        redis.delete(key)
        return redirect('/')
    return render_template(
        'account/email.html',
        form=form,
        user=user,
    )
示例#30
0
def change_email(token):
    email, key = get_email_from_signature(token, 'email')
    user = User.query.filter_by(email=email).first_or_404()
    form = EmailForm()
    if form.validate_on_submit():
        user.email = form.email.data
        with db.auto_commit():
            db.session.add(user)
        redis.delete(key)
        return redirect('/')
    return render_template(
        'account/email.html',
        form=form,
        user=user,
    )
示例#31
0
def like_topic_comment(tid, cid):
    like = CommentLike.query.get((cid, current_user.id))
    if like:
        raise Conflict(description='You already liked it')

    comment = get_comment_or_404(tid, cid)
    # here is a concurrency bug, but it doesn't matter
    if comment.like_count:
        comment.like_count += 1
    else:
        comment.like_count = 1
    like = CommentLike(comment_id=comment.id, user_id=current_user.id)
    with db.auto_commit():
        db.session.add(like)
        db.session.add(comment)
    return '', 204
示例#32
0
def like_topic_comment(tid, cid):
    like = CommentLike.query.get((cid, current_user.id))
    if like:
        raise Conflict(description='You already liked it')

    comment = get_comment_or_404(tid, cid)
    # here is a concurrency bug, but it doesn't matter
    if comment.like_count:
        comment.like_count += 1
    else:
        comment.like_count = 1
    like = CommentLike(comment_id=comment.id, user_id=current_user.id)
    with db.auto_commit():
        db.session.add(like)
        db.session.add(comment)
    return '', 204
示例#33
0
文件: forms.py 项目: 343829084/zerqu
    def update_cafe(self, cafe, user_id):
        keys = ['name', 'description']
        # Only owner can change slug
        if user_id == cafe.user_id:
            keys.append('slug')

        for k in keys:
            value = self.data.get(k)
            if value:
                setattr(cafe, k, value)

        cafe.style = self.style
        if self.permission.data:
            cafe.permission = Cafe.PERMISSIONS[self.permission.data]
        with db.auto_commit():
            db.session.add(cafe)
        return cafe
示例#34
0
    def update_cafe(self, cafe, user_id):
        keys = ['name', 'description']
        # Only owner can change slug
        if user_id == cafe.user_id:
            keys.append('slug')

        for k in keys:
            value = self.data.get(k)
            if value:
                setattr(cafe, k, value)

        cafe.style = self.style
        if self.permission.data:
            cafe.permission = Cafe.PERMISSIONS[self.permission.data]
        with db.auto_commit():
            db.session.add(cafe)
        return cafe
示例#35
0
文件: cafes.py 项目: valley51/zerqu
def create_cafe_topic(slug):
    cafe = Cafe.cache.first_or_404(slug=slug)

    if not current_user.is_active:
        raise InvalidAccount(description='Your account is not active')

    if cafe.permission == Cafe.PERMISSION_PUBLIC:
        CafeMember.get_or_create(cafe.id, current_user.id)

    form = TopicForm.create_api_form()
    topic = form.create_topic(current_user.id)

    with db.auto_commit():
        cafe.create_cafe_topic(topic.id, current_user.id)

    data = dict(topic)
    data['user'] = dict(current_user)
    data['content'] = topic.html
    return jsonify(data), 201
示例#36
0
文件: account.py 项目: Gwill/zerqu
def delete_topic(token):
    key = 'account:delete-topic:%s' % token
    tid = redis.get(key)
    if not tid:
        abort(404)

    topic = Topic.query.get_or_404(int(tid))
    title = topic.title
    form = Form()
    show_message = False
    if form.validate_on_submit():
        with db.auto_commit():
            db.session.delete(topic)
        show_message = True
    return render_template(
        'account/delete-topic.html',
        title=title,
        form=form,
        show_message=show_message,
    )
示例#37
0
def delete_topic(token):
    key = 'account:delete-topic:%s' % token
    tid = redis.get(key)
    if not tid:
        abort(404)

    topic = Topic.query.get_or_404(int(tid))
    title = topic.title
    form = Form()
    show_message = False
    if form.validate_on_submit():
        with db.auto_commit():
            db.session.delete(topic)
        show_message = True
    return render_template(
        'account/delete-topic.html',
        title=title,
        form=form,
        show_message=show_message,
    )
示例#38
0
文件: cafes.py 项目: valley51/zerqu
def join_cafe(slug):
    cafe = Cafe.cache.first_or_404(slug=slug)
    ident = (cafe.id, current_user.id)

    item = CafeMember.query.get(ident)
    if item and item.role != CafeMember.ROLE_VISITOR:
        return '', 204

    if item:
        item.created_at = datetime.datetime.utcnow()
    else:
        item = CafeMember(cafe_id=cafe.id, user_id=current_user.id)

    if cafe.user_id == current_user.id:
        item.role = CafeMember.ROLE_ADMIN
    else:
        item.role = CafeMember.ROLE_SUBSCRIBER

    try:
        with db.auto_commit():
            db.session.add(item)
    except IntegrityError:
        raise Conflict(description='You already joined the cafe')
    return '', 204