From 41fe4786b59a97b729b3650de03eb2090967bfc3 Mon Sep 17 00:00:00 2001 From: Akash Bhave Date: Sat, 21 Sep 2019 11:30:38 -0400 Subject: [PATCH] Copy files from director --- hoco/__init__.py | 0 hoco/apps/content/__init__.py | 0 hoco/apps/content/admin.py | 3 + hoco/apps/content/apps.py | 5 + hoco/apps/content/migrations/__init__.py | 0 hoco/apps/content/models.py | 1 + hoco/apps/content/tests.py | 3 + hoco/apps/content/views.py | 10 ++ hoco/apps/oauth/__init__.py | 0 hoco/apps/oauth/admin.py | 3 + hoco/apps/oauth/apps.py | 5 + hoco/apps/oauth/migrations/__init__.py | 0 hoco/apps/oauth/models.py | 3 + hoco/apps/oauth/tests.py | 3 + hoco/apps/oauth/views.py | 33 ++++++ hoco/settings.py | 126 +++++++++++++++++++++++ hoco/templates/base.html | 23 +++++ hoco/templates/fail.html | 18 ++++ hoco/templates/index.html | 81 +++++++++++++++ hoco/urls.py | 26 +++++ hoco/wsgi.py | 16 +++ manage.py | 21 ++++ requirements.txt | 12 +++ 23 files changed, 392 insertions(+) create mode 100644 hoco/__init__.py create mode 100644 hoco/apps/content/__init__.py create mode 100644 hoco/apps/content/admin.py create mode 100644 hoco/apps/content/apps.py create mode 100644 hoco/apps/content/migrations/__init__.py create mode 100644 hoco/apps/content/models.py create mode 100644 hoco/apps/content/tests.py create mode 100644 hoco/apps/content/views.py create mode 100644 hoco/apps/oauth/__init__.py create mode 100644 hoco/apps/oauth/admin.py create mode 100644 hoco/apps/oauth/apps.py create mode 100644 hoco/apps/oauth/migrations/__init__.py create mode 100644 hoco/apps/oauth/models.py create mode 100644 hoco/apps/oauth/tests.py create mode 100644 hoco/apps/oauth/views.py create mode 100644 hoco/settings.py create mode 100644 hoco/templates/base.html create mode 100644 hoco/templates/fail.html create mode 100644 hoco/templates/index.html create mode 100644 hoco/urls.py create mode 100644 hoco/wsgi.py create mode 100755 manage.py create mode 100644 requirements.txt diff --git a/hoco/__init__.py b/hoco/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hoco/apps/content/__init__.py b/hoco/apps/content/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hoco/apps/content/admin.py b/hoco/apps/content/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/hoco/apps/content/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hoco/apps/content/apps.py b/hoco/apps/content/apps.py new file mode 100644 index 0000000..3a9bd48 --- /dev/null +++ b/hoco/apps/content/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ContentConfig(AppConfig): + name = 'content' diff --git a/hoco/apps/content/migrations/__init__.py b/hoco/apps/content/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hoco/apps/content/models.py b/hoco/apps/content/models.py new file mode 100644 index 0000000..137941f --- /dev/null +++ b/hoco/apps/content/models.py @@ -0,0 +1 @@ +from django.db import models diff --git a/hoco/apps/content/tests.py b/hoco/apps/content/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/hoco/apps/content/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/hoco/apps/content/views.py b/hoco/apps/content/views.py new file mode 100644 index 0000000..cec87dd --- /dev/null +++ b/hoco/apps/content/views.py @@ -0,0 +1,10 @@ +from django.shortcuts import render, redirect +from django.urls import reverse + +# Create your views here. +def index(request): + if "admin" in request.session and request.session["admin"] == True: + return render(request, "index.html") + else: + request.session.flush() + return render(request, "fail.html") \ No newline at end of file diff --git a/hoco/apps/oauth/__init__.py b/hoco/apps/oauth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hoco/apps/oauth/admin.py b/hoco/apps/oauth/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/hoco/apps/oauth/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hoco/apps/oauth/apps.py b/hoco/apps/oauth/apps.py new file mode 100644 index 0000000..17fcea2 --- /dev/null +++ b/hoco/apps/oauth/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class OauthConfig(AppConfig): + name = 'oauth' diff --git a/hoco/apps/oauth/migrations/__init__.py b/hoco/apps/oauth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hoco/apps/oauth/models.py b/hoco/apps/oauth/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/hoco/apps/oauth/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/hoco/apps/oauth/tests.py b/hoco/apps/oauth/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/hoco/apps/oauth/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/hoco/apps/oauth/views.py b/hoco/apps/oauth/views.py new file mode 100644 index 0000000..ee89056 --- /dev/null +++ b/hoco/apps/oauth/views.py @@ -0,0 +1,33 @@ +import json + +from oauthlib.oauth2.rfc6749.errors import InvalidGrantError +from requests_oauthlib import OAuth2Session + +from django.conf import settings +from django.shortcuts import redirect, render +from django.urls import reverse + +# Create your views here. +def login(request): + oauth = OAuth2Session(settings.CLIENT_ID, redirect_uri=settings.REDIRECT_URI, scope=["read"]) + if "error" in request.GET: + return redirect(reverse("login")) + if "code" not in request.GET: + authorization_url, state = oauth.authorization_url("https://ion.tjhsst.edu/oauth/authorize/") + return redirect(authorization_url) + try: + oauth.fetch_token("https://ion.tjhsst.edu/oauth/token/", code=request.GET["code"], client_secret=settings.CLIENT_SECRET) + profile = oauth.get("https://ion.tjhsst.edu/api/profile") + user_data = json.loads(profile.content.decode()) + + authorized_user = False + authorized_users = ["2021abhave", "2020rranjan"] + for user in authorized_users: + if user == user_data["ion_username"]: + authorized_user = True + + request.session["user"] = user_data["ion_username"] + request.session["admin"] = user_data["is_teacher"] or user_data["is_eighth_admin"] or authorized_user + return redirect(reverse("index")) + except InvalidGrantError: + return redirect(reverse("login")) \ No newline at end of file diff --git a/hoco/settings.py b/hoco/settings.py new file mode 100644 index 0000000..5d2c30f --- /dev/null +++ b/hoco/settings.py @@ -0,0 +1,126 @@ +""" +Django settings for hoco project. + +Generated by 'django-admin startproject' using Django 2.2.5. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.2/ref/settings/ +""" + +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/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '-lq2h6ax-h-r94p213v-f@1pj*!8^yf!!w%l^p@o&gie%#8xr0' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +CLIENT_ID = "0QfO8ZlP7naRYdNLOxpMWayzm2Mlc25FUyeCihGN" +CLIENT_SECRET = "gPfDm2CvNbTnUyj7IG2rCeEBhMaiqicGjLr4xhSO8rDGqTxLVIENX2mWdWNPhg4sSsp48UjILkZCDG83SC3IMnPOSDYj8KnY4bNGJiMjmmPK6d9BZi42ittI4oa62z3j" +REDIRECT_URI = "http://hoco.sites.tjhsst.edu/login" + + +# Application definition + +INSTALLED_APPS = [ + '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 = 'hoco.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join(BASE_DIR, "hoco/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 = 'hoco.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/hoco/templates/base.html b/hoco/templates/base.html new file mode 100644 index 0000000..11a3a93 --- /dev/null +++ b/hoco/templates/base.html @@ -0,0 +1,23 @@ + +{% load static %} + + + + + + TJHSST + + + + + + {% block head %}{% endblock %} + + + {% block root %} +
+ {% block body %}{% endblock %} +
+ {% endblock %} + + \ No newline at end of file diff --git a/hoco/templates/fail.html b/hoco/templates/fail.html new file mode 100644 index 0000000..ee3f67e --- /dev/null +++ b/hoco/templates/fail.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} +{% block body %} +
+

Homecoming 2019

+
+
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/hoco/templates/index.html b/hoco/templates/index.html new file mode 100644 index 0000000..e9c19b7 --- /dev/null +++ b/hoco/templates/index.html @@ -0,0 +1,81 @@ +{% extends 'base.html' %} +{% block body %} + +

Homecoming 2019 Voting

+

Thank you for voting! Without your help homecoming wouldn't be possible. Choose a category:

+

+ Banner + Canned Food Sculpture + Class Song + Float + MEX + T-Shirt Design + Spirit Video +

+ +
+

Canned Food Sculpture (back)

+ +
+
+

Class Song (back)

+ + +
+
+

Float (back)

+ +
+
+

MEX (back)

+ +
+
+

T-Shirt Design (back)

+ +
+
+

Spirit Video (back)

+ + +
+{% endblock %} \ No newline at end of file diff --git a/hoco/urls.py b/hoco/urls.py new file mode 100644 index 0000000..5f368bb --- /dev/null +++ b/hoco/urls.py @@ -0,0 +1,26 @@ +"""hoco URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path +from .apps.oauth import views as oauth_views +from .apps.content import views as content_views + + +urlpatterns = [ + path('admin/', admin.site.urls), + path('login/', oauth_views.login, name="login"), + path('', content_views.index, name="index"), +] diff --git a/hoco/wsgi.py b/hoco/wsgi.py new file mode 100644 index 0000000..7f9dba6 --- /dev/null +++ b/hoco/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for hoco project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hoco.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..22e5652 --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hoco.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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d1b196d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +certifi==2019.9.11 +chardet==3.0.4 +Django==2.2.5 +idna==2.8 +oauthlib==3.1.0 +pkg-resources==0.0.0 +pytz==2019.2 +requests==2.22.0 +requests-oauthlib==1.2.0 +sqlparse==0.3.0 +urllib3==1.25.3 +