Skip to content

Commit

Permalink
Finalized app. Missing create-user script
Browse files Browse the repository at this point in the history
  • Loading branch information
ramcav committed Dec 2, 2023
1 parent 9fad2b6 commit 53dad8f
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__
*.sqlite3
django4
venv
.env

106 changes: 81 additions & 25 deletions Testing_Group_Project_Database.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,30 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting mysql-connector\n",
" Using cached mysql_connector-2.2.9-cp312-cp312-macosx_10_9_universal2.whl\n",
"Installing collected packages: mysql-connector\n",
"Successfully installed mysql-connector-2.2.9\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.12 -m pip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"!pip3 install mysql-connector"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "85460dff-ff83-4bf5-8133-e788753fd16a",
"metadata": {},
"outputs": [
Expand All @@ -19,28 +42,14 @@
"output_type": "stream",
"text": [
"=== Testing connection to the Database\n",
"SELECT now();\n",
"[(datetime.datetime(2023, 11, 10, 23, 13, 27),)]\n",
"=== Showing the Databases you have access to. This should include DUDB_3\n",
"show databases;\n",
"SELECT NOW();\n",
"[(datetime.datetime(2023, 12, 1, 12, 11, 8),)]\n",
"=== Showing the Databases you have access to.\n",
"SHOW DATABASES;\n",
"[(bytearray(b'DUDB_3'),), (bytearray(b'information_schema'),), (bytearray(b'performance_schema'),)]\n",
"=== Create a sample table: event\n",
"CREATE TABLE IF NOT EXISTS event (\n",
" id INT NOT NULL AUTO_INCREMENT, \n",
" name VARCHAR(20) , \n",
" state VARCHAR(20),\n",
" datetime DATETIME NOT NULL,\n",
" PRIMARY KEY(id)\n",
");\n",
"show tables;\n",
"[(bytearray(b'event'),)]\n",
"=== Inserting a record into event\n",
"INSERT INTO event(name, state, datetime) VALUES ('test','live',NOW())\n",
"SELECT * FROM event;\n",
"[(1, 'test', 'live', datetime.datetime(2023, 11, 10, 23, 13, 27))]\n",
"=== Dropping table event to leave a clean schema\n",
"DROP TABLE event;\n",
"[]\n"
"\n",
"\n",
"[(bytearray(b'auth_group'),), (bytearray(b'auth_group_permissions'),), (bytearray(b'auth_permission'),), (bytearray(b'auth_user'),), (bytearray(b'auth_user_groups'),), (bytearray(b'auth_user_user_permissions'),), (bytearray(b'django_admin_log'),), (bytearray(b'django_content_type'),), (bytearray(b'django_migrations'),), (bytearray(b'django_session'),), (bytearray(b'home_attendance'),), (bytearray(b'home_course'),), (bytearray(b'home_professor'),), (bytearray(b'home_professor_courses'),), (bytearray(b'home_session'),), (bytearray(b'home_student'),), (bytearray(b'home_student_courses'),), (bytearray(b'social_auth_association'),), (bytearray(b'social_auth_code'),), (bytearray(b'social_auth_nonce'),), (bytearray(b'social_auth_partial'),), (bytearray(b'social_auth_usersocialauth'),), (bytearray(b'taggit_tag'),), (bytearray(b'taggit_taggeditem'),)]\n"
]
}
],
Expand Down Expand Up @@ -72,15 +81,62 @@
"rs = cursor.fetchall()\n",
"print(rs)\n",
"\n",
"\n",
"print(\"\\n\")\n",
"sql_str = 'SHOW TABLES;'\n",
"cursor.execute(sql_str)\n",
"rs = cursor.fetchall()\n",
"print(rs)\n",
"\n",
"cursor.close() # Always close a cursor when no longer required\n",
"cnx.close() # always close your database connections before the program exits"
"# cursor = cnx.cursor()\n",
"\n",
"# # List of tables to be deleted\n",
"# tables_to_delete = [\n",
"# 'snappy_attendance',\n",
"# 'snappy_course',\n",
"# 'snappy_professor',\n",
"# 'snappy_professor_courses',\n",
"# 'snappy_session',\n",
"# 'snappy_student',\n",
"# 'snappy_student_courses'\n",
"# ]\n",
"\n",
"# # Form the SQL statement\n",
"# drop_statement = \"DROP TABLE \" + \", \".join(tables_to_delete)\n",
"\n",
"# # Execute the statement\n",
"# try:\n",
"# cursor.execute(drop_statement)\n",
"# cnx.commit()\n",
"# print(\"Tables deleted successfully.\")\n",
"# except mysql.connector.Error as err:\n",
"# print(f\"Error: {err}\")\n",
"\n",
"# Close the connection\n",
"cursor.close()\n",
"cnx.close()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"ename": "OperationalError",
"evalue": "MySQL Connection not available.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD COMPUTING/Classlink/Testing_Group_Project_Database.ipynb Cell 4\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m cursor \u001b[39m=\u001b[39m cnx\u001b[39m.\u001b[39;49mcursor()\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m# List of tables to be deleted\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m tables_to_delete \u001b[39m=\u001b[39m [\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m'\u001b[39m\u001b[39msnappy_attendance\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39m'\u001b[39m\u001b[39msnappy_course\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=10'>11</a>\u001b[0m \u001b[39m'\u001b[39m\u001b[39msnappy_student_courses\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/ricardomendezcavalieri/Desktop/IE/Y2/S1/CLOUD%20COMPUTING/Classlink/Testing_Group_Project_Database.ipynb#W4sZmlsZQ%3D%3D?line=11'>12</a>\u001b[0m ]\n",
"File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/mysql/connector/connection.py:809\u001b[0m, in \u001b[0;36mMySQLConnection.cursor\u001b[0;34m(self, buffered, raw, prepared, cursor_class, dictionary, named_tuple)\u001b[0m\n\u001b[1;32m 806\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_unread_result()\n\u001b[1;32m 808\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mis_connected():\n\u001b[0;32m--> 809\u001b[0m \u001b[39mraise\u001b[39;00m errors\u001b[39m.\u001b[39mOperationalError(\u001b[39m\"\u001b[39m\u001b[39mMySQL Connection not available.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 810\u001b[0m \u001b[39mif\u001b[39;00m cursor_class \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 811\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39missubclass\u001b[39m(cursor_class, CursorBase):\n",
"\u001b[0;31mOperationalError\u001b[0m: MySQL Connection not available."
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -106,7 +162,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down
39 changes: 39 additions & 0 deletions mysite/home/scripts/user_creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings")
django.setup()

from django.contrib.auth.models import User
from your_app.models import Professor, Student, Course

# Function to create user and assign role-specific profile
def create_user(username, email, password, role, name, courses):
# Create User
user = User.objects.create_user(username=username, email=email, password=password)

# Create role-specific profile and assign courses
if role.lower() == 'professor':
professor = Professor.objects.create(user=user, name=name)
for course_name in courses:
course, created = Course.objects.get_or_create(course_name=course_name)
professor.courses.add(course)
elif role.lower() == 'student':
student = Student.objects.create(user=user, name=name)
for course_name in courses:
course, _ = Course.objects.get_or_create(course_name=course_name)
student.courses.add(course)

courses = ['Designing and Using Databases', 'Programming 1', 'Algorithms & Data Structures', 'Cloud Computing', 'Technology', 'Calculus for Computer Science']

students = [('ricardo', '[email protected]', 'password123', 'student', 'Ricardo Mendez', courses),
]
professors = [('antonio', '[email protected]', 'password123', 'professor', 'Antonio Momblan', [courses[2]]),
]

# Create users
for student in students:
create_user(*student)

for professor in professors:
create_user(*professor)
44 changes: 0 additions & 44 deletions mysite/home/templates/base_bootstrap.html

This file was deleted.

10 changes: 0 additions & 10 deletions mysite/home/templates/base_menu.html

This file was deleted.

14 changes: 6 additions & 8 deletions mysite/home/templates/home/attendance.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<div class="container mt-5">
<div class="jumbotron bg-light text-center">
<h1 class="display-4 text-primary">{{ selected_class }}</h1>
<p class="lead">Manage attendance for your class. Attendance Code: <span id="attendanceCode">ABCD1234</span></p>
<button class="btn btn-info" id="generateCode">Generate New Code</button>
<a href="{% url 'attendance_management' %}" class="btn btn-secondary">Check Attendance</a>
<p class="lead">Manage attendance for your class. Attendance Code:</p>
<h2 class="display-3 text-secondary"><span id="attendanceCode"></span></h2>
<a href="{% url 'attendance_management' %}" class="btn btn-info">Check Attendance</a>
<hr class="my-4">
</div>
</div>
Expand All @@ -28,7 +28,7 @@ <h1 class="display-4 text-primary">{{ selected_class }}</h1>
// Function to delete old codes from Azure Table Storage
function deleteOldCodes() {
$.ajax({
url: 'https://tempcodes.azurewebsites.net/api/DeleteCodesFunction?code=bjDNB3x9NRcJtChu5YvEuSOT__c3jdmRnKhDmoIDwjX2AzFuXYyQbw==',
url: '/api/delete-code/,
type: 'GET',
success: function(response, statusText, xhr) {
console.log('Old codes deleted, status:', xhr.status);
Expand All @@ -41,17 +41,15 @@ <h1 class="display-4 text-primary">{{ selected_class }}</h1>

// Function to update the attendance code from Azure Function
function updateAttendanceCode() {
deleteOldCodes(); // Delete old codes before updating the new code

$.ajax({
url: 'https://tempcodes.azurewebsites.net/api/GenerateCodeFunction?code=74pbScwXMtLf6JDxQy6s8MpIEHTHXke6q-7mP5BX-JkIAzFumSyvkQ==',
url: '/api/azure-function/', // URL of the Django endpoint
type: 'GET',
success: function(response) {
console.log('New code fetched:', response.code);
$('#attendanceCode').text(response.code);
},
error: function(error) {
console.log('Error fetching new code:', error);
console.log('Error:', error);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion mysite/home/templates/home/attendance_management.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1 class="display-4 text-primary">{{ course.course_name }}</h1>

<div class="row justify-content-center">
<div class="col-md-8 text-center">
<button type="submit" class="btn btn-primary">Update Attendance</button>
<button type="submit" class="btn btn-primary">Submit Attendance</button>
</div>
</div>
</form>
Expand Down
41 changes: 32 additions & 9 deletions mysite/home/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
{% load static %}

{% block content %}
<html>
<head>
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
<head>
<body>
<div class="container mt-5">
<div class="jumbotron bg-light text-center">
<h1 class="display-4 text-primary">Login</h1>
<p class="lead">Please enter your credentials to login.</p>

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if form.errors %}
<div class="alert alert-danger" role="alert">
Your username and password didn't match. Please try again.
</div>
{% endif %}

<form method="post" action="{% url 'login' %}" class="mt-4">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
</body>

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</html>
4 changes: 3 additions & 1 deletion mysite/home/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.urls import path, include
from . import views
from .views import ConfirmationView, AttendanceManagementeView, DisplayCodesView, ProfessorDashboardView, StudentAttendanceView, ProfessorAttendanceView
from .views import ConfirmationView, AttendanceManagementeView, DisplayCodesView, ProfessorDashboardView, StudentAttendanceView, ProfessorAttendanceView, call_azure_function, call_delete_function

# URL patterns for the app
urlpatterns = [
Expand All @@ -15,4 +15,6 @@
path('professor/display_codes/validate/', AttendanceManagementeView.as_view(), name="attendance_management"),
path('student/attendance/', StudentAttendanceView.as_view(), name='student_attendance'),
path('professor/attendance/', ProfessorAttendanceView.as_view(), name='professor_attendance'),
path('api/azure-function/', call_azure_function, name='azure-function'),
path('api/delete-code/', call_delete_function, name='delete-code'),
]
Loading

0 comments on commit 53dad8f

Please sign in to comment.