Skip to content

Commit

Permalink
Add listing model
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Jan 31, 2023
0 parents commit ad936f9
Show file tree
Hide file tree
Showing 38 changed files with 499 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/commerce.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added auctions/__init__.py
Empty file.
Binary file added auctions/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added auctions/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file added auctions/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file added auctions/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added auctions/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added auctions/__pycache__/views.cpython-311.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions auctions/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

from .models import User, Listing

admin.site.register(User)
admin.site.register(Listing)
5 changes: 5 additions & 0 deletions auctions/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AuctionsConfig(AppConfig):
name = 'auctions'
55 changes: 55 additions & 0 deletions auctions/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 4.1.5 on 2023-01-31 19:09

from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Listing',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64)),
('description', models.CharField(max_length=256)),
('seller', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='listings', to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file added auctions/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions auctions/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
pass


class Listing(models.Model):
name = models.CharField(max_length=64)
description = models.CharField(max_length=256)
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="listings")

def __str__(self):
return f"{self.name}, {self.description}"
3 changes: 3 additions & 0 deletions auctions/static/auctions/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
padding: 10px;
}
11 changes: 11 additions & 0 deletions auctions/templates/auctions/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "auctions/layout.html" %}

{% block body %}
<h2>Active Listings</h2>

<ul>
{% for listing in listings %}
<li>Listing {{ listing.id }}: {{ listing.name }} - {{ listing.description }}, made by {{ listing.seller }}</li>
{% endfor %}
</ul>
{% endblock %}
40 changes: 40 additions & 0 deletions auctions/templates/auctions/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Auctions{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
</head>
<body>
<h1>Auctions</h1>
<div>
{% if user.is_authenticated %}
Signed in as <strong>{{ user.username }}</strong>.
{% else %}
Not signed in.
{% endif %}
</div>
<ul class="nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'index' %}">Active Listings</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Log Out</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Log In</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'register' %}">Register</a>
</li>
{% endif %}
</ul>
<hr>
{% block body %}
{% endblock %}
</body>
</html>
24 changes: 24 additions & 0 deletions auctions/templates/auctions/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "auctions/layout.html" %}

{% block body %}

<h2>Login</h2>

{% if message %}
<div>{{ message }}</div>
{% endif %}

<form action="{% url 'login' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input autofocus class="form-control" type="text" name="username" placeholder="Username">
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<input class="btn btn-primary" type="submit" value="Login">
</form>

Don't have an account? <a href="{% url 'register' %}">Register here.</a>

{% endblock %}
30 changes: 30 additions & 0 deletions auctions/templates/auctions/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "auctions/layout.html" %}

{% block body %}

<h2>Register</h2>

{% if message %}
<div>{{ message }}</div>
{% endif %}

<form action="{% url 'register' %}" method="post">
{% csrf_token %}
<div class="form-group">
<input class="form-control" autofocus type="text" name="username" placeholder="Username">
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Email Address">
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<input class="form-control" type="password" name="confirmation" placeholder="Confirm Password">
</div>
<input class="btn btn-primary" type="submit" value="Register">
</form>

Already have an account? <a href="{% url 'login' %}">Log In here.</a>

{% endblock %}
3 changes: 3 additions & 0 deletions auctions/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions auctions/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from . import views

urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register")
]
65 changes: 65 additions & 0 deletions auctions/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .models import User, Listing


def index(request):
return render(request, "auctions/index.html", {
"listings": Listing.objects.all()
})


def login_view(request):
if request.method == "POST":

# Attempt to sign user in
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, username=username, password=password)

# Check if authentication successful
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/login.html", {
"message": "Invalid username and/or password."
})
else:
return render(request, "auctions/login.html")


def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse("index"))


def register(request):
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]

# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request, "auctions/register.html", {
"message": "Passwords must match."
})

# Attempt to create new user
try:
user = User.objects.create_user(username, email, password)
user.save()
except IntegrityError:
return render(request, "auctions/register.html", {
"message": "Username already taken."
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/register.html")
Empty file added commerce/__init__.py
Empty file.
Binary file added commerce/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added commerce/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file added commerce/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added commerce/__pycache__/wsgi.cpython-311.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions commerce/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for commerce project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'commerce.settings')

application = get_asgi_application()
Loading

0 comments on commit ad936f9

Please sign in to comment.