Esempio n. 1
0
def image_upload(request):

    errors = []

    if request.method == "POST":
        image_form = ImageUploadForm(request.POST, request.FILES)
        if image_form.is_valid():
            file = image_form.cleaned_data["image"]

            image = Image()
            image.original = file
            image.created_by = request.user
            image.save()
            return HttpResponse(
                '{"uploaded_image":{"url":"%s","id":%d,"width":%d,"height":%d}}'
                % (image.original.url, image.id, image.width, image.height)
            )
Esempio n. 2
0
def post_to_stream(request, stream_id):

    stream = Stream.objects.get(pk=stream_id)

    if request.method == "POST":

        # May upload an image directly from the phone
        submitted_image = None
        image_form = ImageUploadForm(request.POST, request.FILES)
        if image_form.is_valid():
            print "YAY"
            new_image = Image()
            new_image.created_by = request.user
            new_image.original = image_form.cleaned_data["image"]
            new_image.cropped = image_form.cleaned_data["image"]
            new_image.save()
            submitted_image = new_image

        form = PostForm(request.POST, request.FILES)
        if form.is_valid():

            images = []
            image_ids = [int(id) for id in form.cleaned_data["image_ids"].split(",") if id]
            if image_ids:
                images = Image.objects.filter(pk__in=image_ids)
            if submitted_image:
                images.append(submitted_image)

            # Make sure there is text OR an image.  Doesn't need to be both
            if form.cleaned_data["text"] or len(images):
                post = Post()
                post.description = form.cleaned_data["text"] or "posted an image."
                post.created_by = request.user
                post.stream = stream
                post.save()

                post.images = images

                # Send an email alerting of a post
                target_url = request.build_absolute_uri(
                    reverse("thecavins.views.stream", args=(stream.id,)) + "#post-" + str(post.id)
                )

                image_section = ""
                for image in post.images.all():
                    image_section += '<br><img src="' + image.cropped.url + '">'

                text_content = "Please tell me if you see this."
                html_content = (
                    "---- Reply above this line ----<br><br>"
                    + post.description
                    + "<br><br>"
                    + image_section
                    + "<br>"
                    + "Link to this post : "
                    + target_url
                )

                subject = request.user.get_profile().nickname + " posted to TheCavins.com"
                from_email = "*****@*****.**"
                recipient_list = [
                    user.email
                    for user in User.objects.filter(groups=stream.group).exclude(pk=request.user.pk)
                    if user.email
                ]

                msg = EmailMultiAlternatives(subject, text_content, from_email, recipient_list)
                msg.attach_alternative(html_content, "text/html")
                msg.send(fail_silently=True)

    return redirect("thecavins.views.stream", stream.id)