예제 #1
0
파일: views.py 프로젝트: 7Pros/circuit
    def form_valid(self, form):
        """
        Check if the user is allowed to edit the post this way.

        The user must be logged in and the post content must be
        at most 256 python characters long.

        @param form: the form containing the new post
        @return `form_valid` if accepted, `form_invalid` if not
        """
        if not self.request.user.is_authenticated:
            return self.form_invalid(form)
        has_img = 'image' in self.request.FILES
        if has_img and not Post.image_is_valid(self.request.FILES['image']):
            messages.error(self.request, 'Invalid image format')

        if not Post.content_is_valid(self.request.POST['content']):
            return self.form_invalid(form)
        old_post = self.object
        parsed_content_old = Post.parse_content(old_post.content)
        post = form.save()
        parsed_content_new = Post.parse_content(post.content)
        mentioned_users = User.objects.filter(username__in=parsed_content_new['mentions'])
        for mentionedUser in mentioned_users:
            if post.circles_id == -1:
                members = mentioned_users
            else:
                members = post.circles.members.all()

            for member in members:
                if mentionedUser.username in parsed_content_old['mentions']:
                    context = {
                        'content': "%s changed his post you were metioned in" % self.request.user.username,
                        'link_to_subject': reverse("posts:post", kwargs={'pk': post.pk})
                    }
                else:
                    context = {
                        'content': "%s mentioned you in his post" % self.request.user.username,
                        'link_to_subject': reverse("posts:post", kwargs={'pk': post.pk})
                    }
                send_notifications(mentionedUser, "You were mentioned!",
                                   'users/notification_for_post_email.html', context, post)

        circle_pk = int(self.request.POST['circle'])
        if circle_pk not in (ME_CIRCLE, PUBLIC_CIRCLE):
            circle_owner_id = Circle.objects.get(pk=circle_pk).owner_id
            # Is the selected circle not one of the user's circles?
            if circle_owner_id != self.request.user.pk:
                return self.form_invalid(form)

        post = form.save()
        if has_img:
            post.image = self.request.FILES['image']

        circle_pk = int(self.request.POST['circle'])
        post.circles = Circle(circle_pk)

        return super(PostEditView, self).form_valid(form)
예제 #2
0
def post_create(request):
    has_image = 'image' in request.FILES
    if not request.user.is_authenticated:
        messages.error(request, 'Not logged in')
    elif 'content' not in request.POST \
            or not Post.content_is_valid(request.POST['content']):
        messages.error(request, 'Invalid post content')
    elif has_image and not Post.image_is_valid(request.FILES['image']):
        messages.error(request, 'Invalid image format')
    elif 'circle' not in request.POST:
        messages.error(request, 'No circle selected')
    else:
        circle_pk = int(request.POST['circle'])
        pseudo_circle = Circle(
            circle_pk
        )  # not saved to DB, only used to store PK, do not use for anything else!
        post = Post(content=request.POST['content'],
                    author=request.user,
                    circles=pseudo_circle)
        if has_image:
            post.image = request.FILES['image']
        post.save()
        parsed_content = Post.parse_content(request.POST['content'])
        post.save_hashtags(parsed_content['hashtags'])
        mentioned_users = User.objects.filter(
            username__in=parsed_content['mentions'])
        for mentionedUser in mentioned_users:
            mentionedUser_is_in_circle = False

            if circle_pk == -1:
                members = mentioned_users
            else:
                members = post.circles.members.all()

            for member in members:
                if mentionedUser == member:
                    mentionedUser_is_in_circle = True
            if mentionedUser_is_in_circle:
                context = {
                    'content':
                    "%s mentioned you in his post" % request.user.username,
                    'link_to_subject':
                    reverse("posts:post", kwargs={'pk': post.pk})
                }
                send_notifications(mentionedUser, "You were mentioned!",
                                   'users/notification_for_post_email.html',
                                   context, post)

    return redirect(request.META['HTTP_REFERER'] or 'landingpage')
예제 #3
0
파일: views.py 프로젝트: 7Pros/circuit
def post_create(request):
    has_image = 'image' in request.FILES
    if not request.user.is_authenticated:
        messages.error(request, 'Not logged in')
    elif 'content' not in request.POST \
            or not Post.content_is_valid(request.POST['content']):
        messages.error(request, 'Invalid post content')
    elif has_image and not Post.image_is_valid(request.FILES['image']):
        messages.error(request, 'Invalid image format')
    elif 'circle' not in request.POST:
        messages.error(request, 'No circle selected')
    else:
        circle_pk = int(request.POST['circle'])
        pseudo_circle = Circle(circle_pk)  # not saved to DB, only used to store PK, do not use for anything else!
        post = Post(content=request.POST['content'], author=request.user, circles=pseudo_circle)
        if has_image:
            post.image = request.FILES['image']
        post.save()
        parsed_content = Post.parse_content(request.POST['content'])
        post.save_hashtags(parsed_content['hashtags'])
        mentioned_users = User.objects.filter(username__in=parsed_content['mentions'])
        for mentionedUser in mentioned_users:
            mentionedUser_is_in_circle = False

            if circle_pk == -1:
                members = mentioned_users
            else:
                members = post.circles.members.all()

            for member in members:
                if mentionedUser == member:
                    mentionedUser_is_in_circle = True
            if mentionedUser_is_in_circle:
                context = {
                    'content': "%s mentioned you in his post" % request.user.username,
                    'link_to_subject': reverse("posts:post", kwargs={'pk': post.pk})
                }
                send_notifications(mentionedUser, "You were mentioned!",
                                   'users/notification_for_post_email.html', context, post)

    return redirect(request.META['HTTP_REFERER'] or 'landingpage')
예제 #4
0
def posts_create(request):
    """
    If the user is authenticated, it will perform the posting of a new entry to the user it was logged in profile.

    The decorators take care of the rendering of the content, the authentication of the incoming requests, the permissions the
    incoming requests have, the throttling and the parsers.

    @param request: incoming request.
    @return: Response rendering a template in case the request was made with a form or a json in case it was a application/json type.
    """
    if request.method == 'POST' and 'multipart/form-data' in request.content_type:
        # authentication
        if request.user.is_authenticated:
            # post validation
            if Post.content_is_valid(request.data['content']):

                parsedString = Post.parse_content(request.data['content'])
                post = Post(content=request.data['content'], author=request.user)

                # non required fields
                if request.data['image'] and Post.image_is_valid(request.data['image']):
                    post.image = request.data['image']

                circle_pk = int(request.data['circle'])
                pseudo_circle = Circle(
                    circle_pk)  # not saved to DB, only used to store PK, do not use for anything else!
                post = Post(content=request.POST['content'], author=request.user, circles=pseudo_circle)

                post.save()
                post.save_hashtags(parsedString['hashtags'])
                setattr(request.user, 'circles', request.user.circle_set.all())

                return Response(data={
                    'message': {
                        'status': 0,
                        'content': 'Post created'
                    },
                    'user': request.user
                }, template_name='api/post_create.html', status=status.HTTP_201_CREATED)
        # Authentification failed response
        return Response(data={
            'message': {
                'status': 1,
                'content': 'Not authenticated or content is not valid!',
            },
        }, template_name='api/post_create.html', status=status.HTTP_401_UNAUTHORIZED)

    elif request.method == 'POST' and 'application/json' in request.content_type:
        if request.user.is_authenticated:
            if Post.content_is_valid(request.data['content']):
                token = request.auth

                parsedString = Post.parse_content(request.data['content'])
                post = Post(content=request.data['content'], author=request.user)

                if 'image' in request.data and request.data['image'] and Post.image_is_valid(request.data['image']):
                    post.image = request.data['image']

                # TODO: fix circles
                if 'circle' in request.data:
                    circle_pk = int(request.data['circle'])
                    pseudo_circle = Circle(
                        circle_pk)  # not saved to DB, only used to store PK, do not use for anything else!
                    post.circles = pseudo_circle
                else:
                    return Response(data={
                        'message': {
                            'status': 1,
                            'action-feedback': 'You didn\'t select any circle',
                            'user-authentication-token': token.key,
                        },
                        'post-required-values': post_required_values
                    }, template_name='api/post_create.html', status=status.HTTP_401_UNAUTHORIZED)

                post.save()
                post.save_hashtags(parsedString['hashtags'])

                user_circles = request.user.circle_set.all()
                user_circles_json = dict()
                for circle in user_circles:
                    user_circles_json[circle.pk] = circle.name
                user_circles_json['-2'] = 'Me'
                user_circles_json['-1'] = 'Public'

                return Response(data={
                    'message': {
                        'status': 0,
                        'action-feedback': 'Post created',
                        'user-authentication-token': token.key,
                    },
                    'post-required-values': post_required_values(user_circles_json),
                }, template_name='api/post_create.html', status=status.HTTP_201_CREATED)
        return Response(data={
            'message': {
                'status': 1,
                'content': 'Not authenticated or content is not valid!',
            },
        }, template_name='api/post_create.html', status=status.HTTP_401_UNAUTHORIZED)
    # Authentification failed response
    return Response(data={
        'message': {
            'status': 1,
            'content': 'Something went wrong!',
        },
    }, template_name='api/post_create.html', status=status.HTTP_401_UNAUTHORIZED)
예제 #5
0
    def form_valid(self, form):
        """
        Check if the user is allowed to edit the post this way.

        The user must be logged in and the post content must be
        at most 256 python characters long.

        @param form: the form containing the new post
        @return `form_valid` if accepted, `form_invalid` if not
        """
        if not self.request.user.is_authenticated:
            return self.form_invalid(form)
        has_img = 'image' in self.request.FILES
        if has_img and not Post.image_is_valid(self.request.FILES['image']):
            messages.error(self.request, 'Invalid image format')

        if not Post.content_is_valid(self.request.POST['content']):
            return self.form_invalid(form)
        old_post = self.object
        parsed_content_old = Post.parse_content(old_post.content)
        post = form.save()
        parsed_content_new = Post.parse_content(post.content)
        mentioned_users = User.objects.filter(
            username__in=parsed_content_new['mentions'])
        for mentionedUser in mentioned_users:
            if post.circles_id == -1:
                members = mentioned_users
            else:
                members = post.circles.members.all()

            for member in members:
                if mentionedUser.username in parsed_content_old['mentions']:
                    context = {
                        'content':
                        "%s changed his post you were metioned in" %
                        self.request.user.username,
                        'link_to_subject':
                        reverse("posts:post", kwargs={'pk': post.pk})
                    }
                else:
                    context = {
                        'content':
                        "%s mentioned you in his post" %
                        self.request.user.username,
                        'link_to_subject':
                        reverse("posts:post", kwargs={'pk': post.pk})
                    }
                send_notifications(mentionedUser, "You were mentioned!",
                                   'users/notification_for_post_email.html',
                                   context, post)

        circle_pk = int(self.request.POST['circle'])
        if circle_pk not in (ME_CIRCLE, PUBLIC_CIRCLE):
            circle_owner_id = Circle.objects.get(pk=circle_pk).owner_id
            # Is the selected circle not one of the user's circles?
            if circle_owner_id != self.request.user.pk:
                return self.form_invalid(form)

        post = form.save()
        if has_img:
            post.image = self.request.FILES['image']

        circle_pk = int(self.request.POST['circle'])
        post.circles = Circle(circle_pk)

        return super(PostEditView, self).form_valid(form)