commit af3ab5adea7a6b93f6a1644430210c3f7d7f9a20 Author: Guy King Date: Wed Jul 29 08:15:11 2020 +0100 commit message diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4405eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/__pycache__ +*.pyc +venv diff --git a/README.md b/README.md new file mode 100644 index 0000000..e22561e --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +## Summary + +Holiday lettings website + +## Local development + +### Prerequisites + +- GitHub account with read access to this repository +- Git CLI +- SQLite3 CLI +- Python interpreter, version 3.6 or higher. + +In the rest of the local development documentation, it is assumed the command `python` in +your OS shell runs the above Python interpreter (unless a virtual environment is activated) + +### macOS / Linux + +#### Clone the repository + +- `cd /path/to/put/project/in` +- `git clone https://github.com/grking8/oc-lettings-site.git` + +#### Create the virtual environment + +- `cd /path/to/oc-lettings-site` +- `python -m venv venv` +- `apt-get install python3-venv` (If previous step errors with package not found on Ubuntu) +- Activate the environment `source venv/bin/activate` +- Confirm the command `python` now runs the Python interpreter in the virtual environment, +`which python` +- Confirm the version of the Python interpreter is 3.6 or higher `python --version` +- Confirm the command `pip` runs the pip executable in the virtual environment, `which pip` +- To deactivate the environment, `deactivate` + +#### Run the site + +- `cd /path/to/oc-lettings-site` +- `source venv/bin/activate` +- `pip install --requirement requirements.txt` +- `python manage.py runserver` +- Go to `http://localhost:8000` in a browser +- Confirm the site is running and can be navigated (you should see several profiles and lettings) + +#### Linting + +- `cd /path/to/oc-lettings-site` +- `source venv/bin/activate` +- `flake8` + +#### Unit tests + +- `cd /path/to/oc-lettings-site` +- `source venv/bin/activate` +- `pytest` + +#### Database + +- `cd /path/to/oc-lettings-site` +- Open a shell session `sqlite3` +- Connect to the database `.open oc-lettings-site.sqlite3` +- Display tables in the database `.tables` +- Display columns in the profiles table, `pragma table_info(oc_lettings_site_profile);` +- Run a query on the profiles table, `select user_id, favorite_city from + oc_lettings_site_profile where favorite_city like 'B%';` +- `.quit` to exit + +#### Admin panel + +- Go to `http://localhost:8000/admin` +- Login with user `admin`, password `Abc1234!` + +### Windows + +Using PowerShell, as above except + +- To activate the virtual environment, `.\venv\Scripts\Activate.ps1` +- Replace `which ` with `(Get-Command ).Path` diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..c0e27e0 --- /dev/null +++ b/manage.py @@ -0,0 +1,19 @@ +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oc_lettings_site.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/oc-lettings-site.sqlite3 b/oc-lettings-site.sqlite3 new file mode 100644 index 0000000..3d88541 Binary files /dev/null and b/oc-lettings-site.sqlite3 differ diff --git a/oc_lettings_site/__init__.py b/oc_lettings_site/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oc_lettings_site/admin.py b/oc_lettings_site/admin.py new file mode 100644 index 0000000..63328c6 --- /dev/null +++ b/oc_lettings_site/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +from .models import Letting +from .models import Address +from .models import Profile + + +admin.site.register(Letting) +admin.site.register(Address) +admin.site.register(Profile) diff --git a/oc_lettings_site/apps.py b/oc_lettings_site/apps.py new file mode 100644 index 0000000..6489692 --- /dev/null +++ b/oc_lettings_site/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class OCLettingsSiteConfig(AppConfig): + name = 'oc_lettings_site' diff --git a/oc_lettings_site/asgi.py b/oc_lettings_site/asgi.py new file mode 100644 index 0000000..61f2d23 --- /dev/null +++ b/oc_lettings_site/asgi.py @@ -0,0 +1,7 @@ +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oc_lettings_site.settings') + +application = get_asgi_application() diff --git a/oc_lettings_site/migrations/0001_initial.py b/oc_lettings_site/migrations/0001_initial.py new file mode 100644 index 0000000..774cf23 --- /dev/null +++ b/oc_lettings_site/migrations/0001_initial.py @@ -0,0 +1,46 @@ +# Generated by Django 3.0 on 2020-06-14 09:35 + +from django.conf import settings +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Address', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('number', models.PositiveIntegerField(validators=[django.core.validators.MaxValueValidator(9999)])), + ('street', models.CharField(max_length=64)), + ('city', models.CharField(max_length=64)), + ('state', models.CharField(max_length=2, validators=[django.core.validators.MinLengthValidator(2)])), + ('zip_code', models.PositiveIntegerField(validators=[django.core.validators.MaxValueValidator(99999)])), + ('country_iso_code', models.CharField(max_length=3, validators=[django.core.validators.MinLengthValidator(3)])), + ], + ), + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('favorite_city', models.CharField(blank=True, max_length=64)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Letting', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=256)), + ('address', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='oc_lettings_site.Address')), + ], + ), + ] diff --git a/oc_lettings_site/migrations/__init__.py b/oc_lettings_site/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oc_lettings_site/models.py b/oc_lettings_site/models.py new file mode 100644 index 0000000..ed255e8 --- /dev/null +++ b/oc_lettings_site/models.py @@ -0,0 +1,31 @@ +from django.db import models +from django.core.validators import MaxValueValidator, MinLengthValidator +from django.contrib.auth.models import User + + +class Address(models.Model): + number = models.PositiveIntegerField(validators=[MaxValueValidator(9999)]) + street = models.CharField(max_length=64) + city = models.CharField(max_length=64) + state = models.CharField(max_length=2, validators=[MinLengthValidator(2)]) + zip_code = models.PositiveIntegerField(validators=[MaxValueValidator(99999)]) + country_iso_code = models.CharField(max_length=3, validators=[MinLengthValidator(3)]) + + def __str__(self): + return f'{self.number} {self.street}' + + +class Letting(models.Model): + title = models.CharField(max_length=256) + address = models.OneToOneField(Address, on_delete=models.CASCADE) + + def __str__(self): + return self.title + + +class Profile(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE) + favorite_city = models.CharField(max_length=64, blank=True) + + def __str__(self): + return self.user.username diff --git a/oc_lettings_site/settings.py b/oc_lettings_site/settings.py new file mode 100644 index 0000000..ffce49d --- /dev/null +++ b/oc_lettings_site/settings.py @@ -0,0 +1,109 @@ +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'fp$9^593hsriajg$_%=5trot9g!1qa@ew(o-1#@=&4%=hp46(s' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'oc_lettings_site.apps.OCLettingsSiteConfig', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'oc_lettings_site.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, 'templates')], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'oc_lettings_site.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'oc-lettings-site.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/oc_lettings_site/tests.py b/oc_lettings_site/tests.py new file mode 100644 index 0000000..3658525 --- /dev/null +++ b/oc_lettings_site/tests.py @@ -0,0 +1,2 @@ +def test_letting_view(): + assert 1 diff --git a/oc_lettings_site/urls.py b/oc_lettings_site/urls.py new file mode 100644 index 0000000..f0ff589 --- /dev/null +++ b/oc_lettings_site/urls.py @@ -0,0 +1,13 @@ +from django.contrib import admin +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.index, name='index'), + path('lettings/', views.lettings_index, name='lettings_index'), + path('lettings//', views.letting, name='letting'), + path('profiles/', views.profiles_index, name='profiles_index'), + path('profiles//', views.profile, name='profile'), + path('admin/', admin.site.urls), +] diff --git a/oc_lettings_site/views.py b/oc_lettings_site/views.py new file mode 100644 index 0000000..a72db27 --- /dev/null +++ b/oc_lettings_site/views.py @@ -0,0 +1,45 @@ +from django.shortcuts import render +from .models import Letting, Profile + + + + +# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie quam lobortis leo consectetur ullamcorper non id est. Praesent dictum, nulla eget feugiat sagittis, sem mi convallis eros, +# vitae dapibus nisi lorem dapibus sem. Maecenas pharetra purus ipsum, eget consequat ipsum lobortis quis. Phasellus eleifend ex auctor venenatis tempus. +# Aliquam vitae erat ac orci placerat luctus. Nullam elementum urna nisi, pellentesque iaculis enim cursus in. Praesent volutpat porttitor magna, non finibus neque cursus id. +def index(request): + return render(request, 'index.html') + +# Aenean leo magna, vestibulum et tincidunt fermentum, consectetur quis velit. Sed non placerat massa. Integer est nunc, pulvinar a +# tempor et, bibendum id arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Cras eget scelerisque +def lettings_index(request): + lettings_list = Letting.objects.all() + context = {'lettings_list': lettings_list} + return render(request, 'lettings_index.html', context) + + +#Cras ultricies dignissim purus, vitae hendrerit ex varius non. In accumsan porta nisl id eleifend. Praesent dignissim, odio eu consequat pretium, purus urna vulputate arcu, vitae efficitur +# lacus justo nec purus. Aenean finibus faucibus lectus at porta. Maecenas auctor, est ut luctus congue, dui enim mattis enim, ac condimentum velit libero in magna. Suspendisse potenti. In tempus a nisi sed laoreet. +# Suspendisse porta dui eget sem accumsan interdum. Ut quis urna pellentesque justo mattis ullamcorper ac non tellus. In tristique mauris eu velit fermentum, tempus pharetra est luctus. Vivamus consequat aliquam libero, eget bibendum lorem. Sed non dolor risus. Mauris condimentum auctor elementum. Donec quis nisi ligula. Integer vehicula tincidunt enim, ac lacinia augue pulvinar sit amet. +def letting(request, letting_id): + letting = Letting.objects.get(id=letting_id) + context = { + 'title': letting.title, + 'address': letting.address, + } + return render(request, 'letting.html', context) + +# Sed placerat quam in pulvinar commodo. Nullam laoreet consectetur ex, sed consequat libero pulvinar eget. Fusc +# faucibus, urna quis auctor pharetra, massa dolor cursus neque, quis dictum lacus d +def profiles_index(request): + profiles_list = Profile.objects.all() + context = {'profiles_list': profiles_list} + return render(request, 'profiles_index.html', context) + +# Aliquam sed metus eget nisi tincidunt ornare accumsan eget lac +# laoreet neque quis, pellentesque dui. Nullam facilisis pharetra vulputate. Sed tincidunt, dolor id facilisis fringilla, eros leo tristique lacus, +# it. Nam aliquam dignissim congue. Pellentesque habitant morbi tristique senectus et netus et males +def profile(request, username): + profile = Profile.objects.get(user__username=username) + context = {'profile': profile} + return render(request, 'profile.html', context) diff --git a/oc_lettings_site/wsgi.py b/oc_lettings_site/wsgi.py new file mode 100644 index 0000000..d78ca6d --- /dev/null +++ b/oc_lettings_site/wsgi.py @@ -0,0 +1,7 @@ +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oc_lettings_site.settings') + +application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c48c84e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +django==3.0 +flake8==3.7.0 +pytest-django==3.9.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b93a5f5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,8 @@ +[flake8] +max-line-length = 99 +exclude = **/migrations/*,oc-lettings-site,oc_lettings_site/views.py + +[tool:pytest] +DJANGO_SETTINGS_MODULE = oc_lettings_site.settings +python_files = tests.py +addopts = -v diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0bd6445 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,4 @@ +Holiday Homes +

Welcome to Holiday Homes

+ + \ No newline at end of file diff --git a/templates/letting.html b/templates/letting.html new file mode 100644 index 0000000..579bb6a --- /dev/null +++ b/templates/letting.html @@ -0,0 +1,8 @@ +{{ title }} +

{{ title }}

+

{{ address.number }} {{ address.street }}

+

{{ address.city }}, {{ address.state }} {{ address.zip_code }}

+

{{ address.country_iso_code }}

+ + + \ No newline at end of file diff --git a/templates/lettings_index.html b/templates/lettings_index.html new file mode 100644 index 0000000..a14bd50 --- /dev/null +++ b/templates/lettings_index.html @@ -0,0 +1,17 @@ +Lettings +

Lettings

+{% if lettings_list %} + +{% else %} +

No lettings are available.

+{% endif %} + + \ No newline at end of file diff --git a/templates/profile.html b/templates/profile.html new file mode 100644 index 0000000..1e62a54 --- /dev/null +++ b/templates/profile.html @@ -0,0 +1,9 @@ +{{ profile.user.username }} +

{{ profile.user.username }}

+

First name: {{ profile.user.first_name }}

+

Last name: {{ profile.user.last_name }}

+

Email: {{ profile.user.email }}

+

Favorite city: {{ profile.favorite_city }}

+ + + \ No newline at end of file diff --git a/templates/profiles_index.html b/templates/profiles_index.html new file mode 100644 index 0000000..7757515 --- /dev/null +++ b/templates/profiles_index.html @@ -0,0 +1,17 @@ +Profiles +

Profiles

+{% if profiles_list %} + +{% else %} +

No profiles are available.

+{% endif %} + + \ No newline at end of file