예제 #1
0
def newpost(request):
    if request.method == 'POST':
        if request.POST['title']:
            if request.POST['body'] or request.FILES:
                post = Post()
                post.title = request.POST['title']

                if request.POST['url']:
                    if request.POST['url'].startswith(
                            'https://') or request.POST['url'].startswith(
                                'http://'):
                        post.url = request.POST['url']
                    else:
                        posts.url = "http://" + request.POST['url']

                if request.POST['body']:
                    post.body = request.POST['body']

                if request.FILES:
                    if request.FILES['image']:
                        post.image = request.FILES['image']

                post.author = request.user

                post.save()
                return redirect('home')
            else:
                return render(
                    request, 'posts/newpost.html',
                    {'error': "Il faut au moins du texte ou une image"})
    else:
        return render(request, 'posts/newpost.html')
예제 #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
def post_share(request, post_id):
    if not request.user.is_authenticated:
        return redirect('login')

    post = get_object_or_404(Post, pk=post_id)
    myUser = CustomUser.objects.get(user=request.user)

    newPost = Post()
    newPost.user = myUser
    newPost.date_pub = datetime.datetime.now()
    newPost.repost = True
    newPost.original_user = post.user
    newPost.text = post.text
    newPost.image = post.image
    newPost.save()

    return redirect('post_index')
예제 #4
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')
예제 #5
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)