Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matthew labs #178

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
22 changes: 22 additions & 0 deletions Code/matthew/django/labs/01_django_redo/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unit_converter_proj.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()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UnitConverterAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'unit_converter_app'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unit Converter</title>
</head>
<body>
<h1>Unit Converter</h1>
{% comment %}
try action=" {% url APP_NAME:result %}"
action="ucp/result/"
{% endcomment %}
<form action="result/" method="post">
{% csrf_token %}
<!-- -->

<div class="distance">
<!-- name='' is the variable Name -->
<textarea
name="distance"
id=""
cols="30"
rows="1"
placeholder="Enter the distance"
></textarea>
</div>

<div class="user_units">
<h3>Enter the units you are using</h3>

<label for="km">Kilometers:</label>
<input type="radio" name="user_units" id="" value="km" />

<label for="m">Meters:</label>
<input type="radio" name="user_units" id="" value="m" />

<label for="ft">Feet:</label>
<input type="radio" name="user_units" id="" value="ft" />
</div>

<div class="conversion_units">
<h3>Enter the units you wish to convert to</h3>

<label for="km">Kilometers:</label>
<input type="radio" name="conversion_units" id="" value="km" />

<label for="m">Meters:</label>
<input type="radio" name="conversion_units" id="" value="m" />

<label for="ft">Feet:</label>
<input type="radio" name="conversion_units" id="" value="ft" />
</div>

<button type="submit">submit</button>
</form>

<!-- -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Results</title>
</head>
<body>
<h1>Results</h1>

<p>
You converted {{distance}}{{units}} into
{{converted_output}}{{units_to_convert}}
</p>
<a href="{% url 'unitconverter:index' %}">Reset<a/>

<br>

distance {{ distance }} units {{units}} converted units {{converted_output}}
units to convert {{units_to_convert}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
18 changes: 18 additions & 0 deletions Code/matthew/django/labs/01_django_redo/unit_converter_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.urls import path, include
from . import views
# . :importing the views from the views file in the same directory

#
app_name = 'unitconverter'

urlpatterns = [
# 8000/ucp
path('', views.index, name='index'),


path('result/', views.forms, name='forms')

# 8000/rps/result
# path('result/', views.result, name='result'),

]
101 changes: 101 additions & 0 deletions Code/matthew/django/labs/01_django_redo/unit_converter_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

from django.shortcuts import render

# Create your views here.


def index(request):

return render(request, 'unit_converter_app/index.html')


# request.POST is grabbing the information inside of the from submission
def forms(request):
form_post = request.POST

#EXAMPLE: distance=form_post['distance']

distance = form_post['distance']
units = form_post['user_units']
units_to_convert = form_post['conversion_units']

def feet_to_meters(x):
converted_ft = float(x) * .3048
return converted_ft

def mile_to_meters(x):
converted_mi = float(x) * 1609.34
return converted_mi

def km_to_meters(x):
converted_km = float(x) * 1000
return converted_km

def meters_to_feet(x):
converted_ft = float(x) * 3.28084
return converted_ft

def meters_to_mile(x):
converted_mi = float(x) * .000621371
return converted_mi

def meters_to_km(x):
converted_km = float(x) * .001
return converted_km

if units.lower() == 'mi' and units_to_convert.lower() == 'km':
converted_output = meters_to_km(mile_to_meters(distance))
print(f'\n {distance}mi is {converted_output}km')

elif units.lower() == 'mi' and units_to_convert.lower() == 'm':
converted_output = mile_to_meters(distance)
print(f'\n {distance}mi is {converted_output}m')

elif units.lower() == 'mi' and units_to_convert.lower() == 'ft':
converted_output = meters_to_feet(mile_to_meters(distance))
print(f'\n {distance}mi is {converted_output}ft')

elif units.lower() == 'km' and units_to_convert.lower() == 'mi':
converted_output = meters_to_mile(km_to_meters(distance))
print(f'\n {distance}km is {converted_output}mi')

elif units.lower() == 'km' and units_to_convert.lower() == 'm':
converted_output = km_to_meters(distance)
print(f'\n {distance}km is {converted_output}m')

elif units.lower() == 'km' and units_to_convert.lower() == 'ft':
converted_output = meters_to_feet(km_to_meters(distance))
print(f'\n {distance}km is {converted_output}ft')

elif units.lower() == 'ft' and units_to_convert.lower() == 'mi':
converted_output = mile_to_meters(feet_to_meters(distance))
print(f'\n {distance}ft is {converted_output}mi')

elif units.lower() == 'ft' and units_to_convert.lower() == 'm':
converted_output = feet_to_meters(distance)
print(f'\n {distance}ft is {converted_output}m')

elif units.lower() == 'ft' and units_to_convert.lower() == 'km':
converted_output = meters_to_km(feet_to_meters(distance))
print(f'\n {distance}mi is {converted_output}ft')

elif units.lower() == 'm' and units_to_convert.lower() == 'ft':
converted_output = meters_to_feet(distance)
print(f'\n {distance}km is {converted_output}ft')

elif units.lower() == 'm' and units_to_convert.lower() == 'mi':
converted_output = meters_to_mile(distance)
print(f'\n {distance}ft is {converted_output}mi')

elif units.lower() == 'm' and units_to_convert.lower() == 'km':
converted_output = meters_to_km
print(f'\n {distance}mi is {converted_output}ft')

context = {
'distance': distance,
'converted_output': converted_output,
'units': units,
'units_to_convert': units_to_convert,
}

return render(request, 'unit_converter_app/result.html', context)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for unit_converter_proj 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/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

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

application = get_asgi_application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"""
Django settings for unit_converter_proj project.

Generated by 'django-admin startproject' using Django 4.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-29itg#5d48)-=q32_h*d159m_29+t!)o#ka4px32c6=58((!8s'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'unit_converter_app'
]

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 = 'unit_converter_proj.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'unit_converter_proj.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/4.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/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Loading