Ejemplo n.º 1
0
def message(request):
    if request.method == 'POST':
        content = request.POST.get('message')
        sender = request.user
        sender = User.objects.get(username = sender)
        recipient = request.POST.get('recipient')
        recipient = User.objects.get(username = recipient)
        item_code = request.POST.get('item')
        item = Item.objects.get(url_code = item_code)
        url_code = request.POST.get('url_code')

        m= Message.objects

        if m.filter(item=item, sender=recipient):
            msg_code = url_code
        elif m.filter(item=item, recipient=recipient):
            msg_code = url_code
        else:
            msg_code = url_coder(size=9)

        msg = Message(sender = sender, recipient = recipient, content = content, item = item, url_code=msg_code)
        msg.save()

        if request.POST.get('url_code'):
            return redirect('/inbox/' + msg_code)
        else:
            messages.success(request, 'Message sent!')
            return redirect('/post/' + item_code)
    else:
        return redirect('/')
Ejemplo n.º 2
0
def post_process(request):
    if request.method == 'POST':

        if request.POST.get('edit'):
            title = request.POST.get('title')
            details = request.POST.get('details')
            url_code = request.POST.get('item_code')
            category = request.POST.get('category')
            category = Category.objects.get(eng_name=category)
            city = request.POST.get('city')
            city = City.objects.get(slug=city)

            item = Item.objects.get(url_code=url_code)
            item.title = title
            item.details = details
            item.category = category
            item.city = city
            if request.POST.get('repost'):
                exp_date = item.pub_date + timedelta(days=item.category.days_til_expire)
                if timezone.now() >= exp_date:
                    #price = request.POST.get('price')
                    #item.price = price
                    item.pub_date = timezone.now()
                item.is_removed = False

            item.save()

            messages.success(request, 'You have successfully edited the item')
            return redirect('/')

        elif request.POST.get('create'):
            title = request.POST.get('title')
            details = request.POST.get('details')
            price = request.POST.get('price')
            url_code = url_coder()
            user = request.user
            category = request.POST.get('category')
            category = Category.objects.get(eng_name=category)
            city = request.POST.get('city')
            city = City.objects.get(slug=city)
            pub_date = timezone.now()

            item = Item(title=title, details=details, price=price, url_code=url_code, user=user, category=category, city=city, pub_date=pub_date)
            item.save()

            if 'image' in request.FILES:
                images = request.FILES.getlist('image')
                number  = len(images)

                # image count limit is 8
                if number <= 8:
                    for image in images:
                        img_filename = image.name
                        img_type = image.content_type
                        img_size = len(image)

                        # image types are jpg, png, gif
                        if img_type == 'image/jpeg' or img_type == 'image/png' or img_type == 'image/gif':
                            # image size limited to 5MB
                            if img_size <= 50000000:
                                img_name = url_coder(size=11)
                                img_ext = os.path.splitext(img_filename)[1].lower()
                                img_filename = img_name + img_ext
                                img_path = os.path.join(img_filename)
                                default_storage.save(img_path, ContentFile(image.read()))

                                img_db = ItemImage(name = img_filename, location = img_filename, item=item)
                                img_db.save()

                                thumb = Image.open(image)
                                W = thumb.size[0] / 2
                                H = thumb.size[1] / 2

                                if W > H:
                                    thumb = thumb.crop((W-H, H-H, W+H, H+H))
                                    thumb = thumb.resize((350,350))
                                elif W < H:
                                    thumb = thumb.crop((W-W, H-W, W+W, H+W))
                                    thumb = thumb.resize((350,350))

                                thumb_path = 'thumb_' + img_filename
                                f_thumb = default_storage.open(thumb_path, 'w')
                                thumb.save(f_thumb, 'png')
                                f_thumb.close()

                            else:
                                messages.error(request, 'The image is too big')
                                return render(request, 'post_create.html')
                        else:
                            messages.error(request, 'Appropriate filetypes are jpg, png, and gif')
                            return render(request, 'post_create.html')

                    return redirect('/post/' + url_code)
                else:
                    messages.error(request, 'You may only upload up to 8 images')
                    return render(request, 'post_create.html')
            else:
                    messages.error(request, 'You need to add at least one image')
                    return render(request, 'post_create.html')
        else:
            # not a POST method
            return redirect('/')