diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fafb871 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\ProgramData\\Anaconda3\\python.exe" +} \ No newline at end of file diff --git a/auctions/__pycache__/forms.cpython-39.pyc b/auctions/__pycache__/forms.cpython-39.pyc new file mode 100644 index 0000000..ff6697c Binary files /dev/null and b/auctions/__pycache__/forms.cpython-39.pyc differ diff --git a/auctions/__pycache__/models.cpython-39.pyc b/auctions/__pycache__/models.cpython-39.pyc index 030c3c2..209a5ed 100644 Binary files a/auctions/__pycache__/models.cpython-39.pyc and b/auctions/__pycache__/models.cpython-39.pyc differ diff --git a/auctions/__pycache__/views.cpython-39.pyc b/auctions/__pycache__/views.cpython-39.pyc index edfc591..04032c7 100644 Binary files a/auctions/__pycache__/views.cpython-39.pyc and b/auctions/__pycache__/views.cpython-39.pyc differ diff --git a/auctions/forms.py b/auctions/forms.py new file mode 100644 index 0000000..5c754c4 --- /dev/null +++ b/auctions/forms.py @@ -0,0 +1,39 @@ + +from django import forms + +categories = [ + (None,'Select one...'), + ('Fashion','Fashion'), + ('Electronics','Electronics'), + ('Home','Home'), + ('Sports','Sports'), + ('Toys','Toys'), + ('Automobile','Automobile'), + ('Books','Books'), + ('Videogames','Videogames'), + ('Others','Others') +] + +class BidForm(forms.Form): + bid_value = 0.00 + + def __init__(self, bid_value,*args,**kwargs ): + super().__init__(*args,**kwargs) + self.bid_value = bid_value + 1 + self.fields['bid'].widget.attrs['min'] = self.bid_value + + bid = forms.DecimalField(decimal_places=2) + +class CreateForm(forms.Form): + title = forms.CharField( max_length=100, widget=forms.TextInput(attrs={'placeholder': 'title'})) + desc= forms.CharField(max_length=100,widget=forms.TextInput(attrs={'placeholder': 'description'})) + starting_bid= forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Starting bid'})) + category = forms.ChoiceField(choices=categories) + photo = forms.ImageField() + + def __init__(self, *args, **kwargs): + super(CreateForm, self).__init__(*args, **kwargs) + for visible in self.visible_fields(): + visible.field.widget.attrs['class'] = 'form-control' + + diff --git a/auctions/models.py b/auctions/models.py index 9c4019d..3c1d710 100644 --- a/auctions/models.py +++ b/auctions/models.py @@ -20,7 +20,7 @@ class AuctionListing(models.Model): user = models.ForeignKey(User,on_delete = models.CASCADE) price = models.DecimalField(max_digits = 10,decimal_places = 2) desc = models.CharField(max_length = 1000) - picture = models.URLField(default="https://www.riobeauty.co.uk/images/product_image_not_found.gif") + picture = models.ImageField(); category = models.CharField(max_length = 64,choices=category_choices) date_added = models.DateTimeField(auto_now_add=True) closed = models.BooleanField(default=False) diff --git a/auctions/templates/auctions/create_listing.html b/auctions/templates/auctions/create_listing.html index 9001e5a..7730b14 100644 --- a/auctions/templates/auctions/create_listing.html +++ b/auctions/templates/auctions/create_listing.html @@ -2,33 +2,15 @@ {% block body %} +

Create Listing

-
+ {% csrf_token %} -
- -
-
- -
-
- -
-
- -
-
- -
- + + {{ Form.as_p }} +
- {% endblock %} \ No newline at end of file diff --git a/auctions/templates/auctions/listing.html b/auctions/templates/auctions/listing.html index b9953ea..77b02b0 100644 --- a/auctions/templates/auctions/listing.html +++ b/auctions/templates/auctions/listing.html @@ -20,7 +20,7 @@ - +

{{Listing.desc}}

diff --git a/auctions/views.py b/auctions/views.py index 817f0ac..8ddbe45 100644 --- a/auctions/views.py +++ b/auctions/views.py @@ -5,19 +5,11 @@ from django.shortcuts import render from django.urls import reverse from django import forms from .models import User,AuctionListing,Comment,Bids +from .forms import BidForm,CreateForm -categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Videogames', 'Other'] -class BidForm(forms.Form): - bid_value = 0.00 - def __init__(self, bid_value,*args,**kwargs ): - super().__init__(*args,**kwargs) - self.bid_value = bid_value + 1 - self.fields['bid'].widget.attrs['min'] = self.bid_value - - bid = forms.DecimalField(decimal_places=2) def watch(request,title): """ @@ -170,27 +162,21 @@ def create(request): """ Used to create a Listing """ - if request.method == "POST": - title = request.POST["title"] - desc = request.POST["desc"] - starting_bid = request.POST["starting_bid"] - photo_url = request.POST["photo"] - - # alternate photo to display - if photo_url == "": - photo_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/No_image_available_600_x_450.svg/1280px-No_image_available_600_x_450.svg.png" - category_value = request.POST["category"] - listing_obj = AuctionListing(title = title, desc = desc, user = request.user,price = starting_bid, picture = photo_url,category=category_value) - listing_obj.save() - bid_obj = Bids(bid_value = starting_bid, listing = listing_obj, user = request.user) - bid_obj.save() + if request.method == 'POST': + form = CreateForm(request.POST, request.FILES) + if form.is_valid(): + listing_obj = AuctionListing(title = form.cleaned_data["title"], desc =form.cleaned_data["desc"], user = request.user,price = form.cleaned_data["starting_bid"], picture = form.cleaned_data["photo"],category= form.cleaned_data["category"]) + listing_obj.save() + bid_obj = Bids(bid_value = form.cleaned_data["starting_bid"], listing = listing_obj, user = request.user) + bid_obj.save() return render(request,"auctions/index.html",{ "Listings" : AuctionListing.objects.all() }) - else: + + else: return render(request,"auctions/create_listing.html",{ - "categories":categories - }) + "Form": CreateForm() + }) def bid(request, title): """ Used to bid on a listing diff --git a/commerce/__pycache__/settings.cpython-39.pyc b/commerce/__pycache__/settings.cpython-39.pyc index 95a27fa..4948a24 100644 Binary files a/commerce/__pycache__/settings.cpython-39.pyc and b/commerce/__pycache__/settings.cpython-39.pyc differ diff --git a/commerce/__pycache__/urls.cpython-39.pyc b/commerce/__pycache__/urls.cpython-39.pyc index 19651cf..18ba202 100644 Binary files a/commerce/__pycache__/urls.cpython-39.pyc and b/commerce/__pycache__/urls.cpython-39.pyc differ diff --git a/commerce/s3_storage.py b/commerce/s3_storage.py new file mode 100644 index 0000000..4325cbf --- /dev/null +++ b/commerce/s3_storage.py @@ -0,0 +1,5 @@ +from storages.backends.s3boto3 import S3Boto3Storage + +class MediaStorage(S3Boto3Storage): + location = 'media' + file_overwrite = False \ No newline at end of file diff --git a/commerce/settings.py b/commerce/settings.py index 016409a..7dcbf90 100644 --- a/commerce/settings.py +++ b/commerce/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'storages', ] MIDDLEWARE = [ @@ -120,3 +121,6 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' + +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') diff --git a/commerce/urls.py b/commerce/urls.py index 2f7a9e0..0bdde9d 100644 --- a/commerce/urls.py +++ b/commerce/urls.py @@ -15,8 +15,13 @@ Including another URLconf """ from django.contrib import admin from django.urls import include, path +from django.conf import settings +from django.conf.urls.static import static urlpatterns = [ path("admin/", admin.site.urls), path("", include("auctions.urls")) -] \ No newline at end of file +] + +if settings.DEBUG: # new + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index a2180ed..c5c6ef9 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/media/Screenshot_2021-01-12_121010.png b/media/Screenshot_2021-01-12_121010.png new file mode 100644 index 0000000..ffe0c71 Binary files /dev/null and b/media/Screenshot_2021-01-12_121010.png differ diff --git a/media/Screenshot_2021-01-12_121010_fohlCLJ.png b/media/Screenshot_2021-01-12_121010_fohlCLJ.png new file mode 100644 index 0000000..ffe0c71 Binary files /dev/null and b/media/Screenshot_2021-01-12_121010_fohlCLJ.png differ