Exemple #1
0
def unblock_user_from_group_request_impl(args):
    try:
        admin = user_from_token(args['token'])
        group = group_from_id(int(args['group_id']))
        is_group_admin(admin, group)
        group.blocked_users.remove(user_from_id(args['user_id']))
        db.session.commit()
        return Response(True, "Unblocked!", GroupSchema().dumps(group).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #2
0
def add_admin_request_impl(args):
    try:
        admin = user_from_token(args['token'])
        group = group_from_id(args['group_id'])
        is_group_admin(admin, group)
        for user_id in args['user_ids']:
            group.admins.append(user_from_id(user_id))
        db.session.commit()
        return Response(True, "Admin Added!", GroupSchema().dumps(group).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #3
0
def create_rule_request_impl(args):
    try:
        user = user_from_token(args['token'])
        group = group_from_id(args['group_id'])
        is_group_admin(user, group)
        rule = Rule(args['property'], args['operand'] ,args['value'], args['value_type'], group)
        rule.name = args['name']
        db.session.add(rule)
        db.session.commit()
        return Response(True, "Rule Created" , RuleSchema().dumps(rule).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #4
0
def invite_request_impl(args):
    try:
        inviter = user_from_token(args['token'])
        group = group_from_id(args['group_id'])
        invitee = user_from_id(args['user_id'])
        invite = Invite(inviter, invitee, group)
        db.session.add(invite)
        db.session.commit()
        invited_for_group.delay(invite, group)
        return Response(True, "Invitation Made", InviteSchema().dumps(invite).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #5
0
def join_group_request_impl(args):
    try:
        joiner = user_from_token(args['token'])
        group = group_from_id(int(args['group_id']))
        is_blocked_from_group(joiner, group)
        user_can_join_group(joiner, group)
        group.subscriptions.append(joiner)
        if group.needs_approval_to_join is False:
                approve_subscription_of_user(joiner, group)
        db.session.commit()
        return Response(True, "Joined Group!", GroupSchema().dumps(group).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #6
0
def group_post_request_impl(args):
    try:
        user = user_from_token(args["token"])
        group = group_from_id(int(args["group_id"]))
        user_is_a_approved_subscriber(user, group)
        return Response(
            True,
            "Posts Searched",
            PostSchema(many=True)
            .dumps(posts_from_group_paginate(user, group, int(args["page"]), int(args["limit"])))
            .data,
        ).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #7
0
def delete_group_request_impl(args):
    try:
        admin = user_from_token(args['token'])
        group = group_from_id(args['group_id'])
        is_group_admin(admin,group)
        group.active = False
        for post in group.posts:
            post.active = False
            for comment in post.comments:
                comment.active = False
        db.session.commit()
        return Response(True, "Group Deleted", GroupSchema().dumps(group).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #8
0
def leave_group_request_impl(args):
    try:
        leaver = user_from_token(args['token'])
        group = group_from_id(int(args['group_id']))
        if user_has_subscribed(leaver, group) is False:
            raise UserCannotDoThisBecuaseIsNotSubscripted
        if is_group_admin_returning_bool(leaver, group):
            if len(group.admins) == 1:
                raise UserCannotLeaveBecauseIsTheLastAdmin
            else:
                group.admins.remove(leaver)
        group.subscriptions.remove(leaver)
        db.session.commit()
        return Response(True, "Leaved Group", None).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #9
0
def approve_join_request_impl(args):
    try:
        admin = user_from_token(args["token"])
        user_pending = user_from_id(args["user_id"])
        group = group_from_id(int(args["group_id"]))
        is_group_admin(admin, group)
        approved = bool(args["approved"])
        if approved:
            approve_subscription_of_user(user_pending, group)
        else:
            remove_subscription_of_user(user_pending, group)
        db.session.commit()
        return Response(
            True, "Joined Group!" if approved is True else "Cannot Join Group!", GroupSchema().dumps(group).data
        ).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #10
0
def anon_action_request_impl(args):
    try:
        user = user_from_token(args['token'])
        post = post_from_id(user, args['post_id'])
        group = group_from_id(args['group_id'])
        status = int(args['status'])
        post.approved = status
        if status == UNDEFINED:
            raise CannotAssignValueException
        elif status == APPROVED:
            post_auth_approved_request_notification.delay(post)
        elif status == REMOVED:
            post_auth_removed_request_notification.delay(post)


        db.session.commit()
        return Response(True, "Action taken", PostSchema().dumps(post).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #11
0
def post_request_impl(args):
    try:
        poster = user_from_token(args['token'])
        group = group_from_id(args['group_id'])

        user_is_a_approved_subscriber(poster, group)

        new_post = Post(args['content'], args['anon'])
        if args.has_key("picture") and args['picture'] is not None:
            output = s3_upload(args['picture'])
            url = 'https://{}.s3.amazonaws.com/{}'.format(os.environ['S3_BUCKET_NAME'], output)
            new_post.pic = url
        group.posts.append(new_post)
        poster.posts.append(new_post)
        db.session.add(new_post)
        db.session.commit()

        #notifications
        if new_post.anon == 1:
            post_auth_request_notification.delay(new_post)

        return Response(True, "Posted Successfully", PostSchema().dumps(new_post).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()
Exemple #12
0
def get_group_request_impl(args):
    try:
        user_from_token(args['token'])
        return Response(True, "Group Created", GroupSchema().dumps(group_from_id(args['group_id'])).data).output()
    except Exception as exception:
        return Response(False, str(exception), None).output()