From 5f3e2401658f848f8764e9bd6326400d829138c2 Mon Sep 17 00:00:00 2001 From: Damian Myrda Date: Mon, 29 Apr 2024 21:19:53 -0500 Subject: [PATCH] Add sqlite database --- README.md | 9 +++- database.db | Bin 0 -> 8192 bytes database.json | 3 +- database.py | 90 +++++++++++++++++++++++++------ main.py | 9 ++-- models/project.py | 4 +- projects.py | 19 ------- schoology.py | 4 +- templates/elements/projects.html | 8 ++- 9 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 database.db delete mode 100644 projects.py diff --git a/README.md b/README.md index d3570ef..59b38d8 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,23 @@ # Computer Science Club Website + A website for Computer Science Club made during a mini-hackathon. ![home page](./assets/home.png) ## setup + ### environment + To setup the server environment, run `python -m venv .` in the root of the project. Then, execute `. ./bin/activate` and you should be in the virtual environment. Next, run `pip install flask schoolopy jsonpickle cachecontrol google-auth google_auth_oauthlib` to install all the dependencies. Now, `exit`. ### schoology -In ```config.py```, change ```DOMAIN``` to the URL of the Schoology instance. In addition, change ```GROUP_ID``` to the ID of the Schoology group. This can be found by visiting the Schoology website and by navigating to the Schoology group. The group's ID should be right after the ```/group/...``` subdirectory of the domain. To get a Schoolopy API key and secret, goto `schoology.(PUT DOMAIN HERE).org/api`. There, you can manage your API credentials. You will need the key and secret to run the server. + +In `config.py`, change `DOMAIN` to the URL of the Schoology instance. In addition, change `GROUP_ID` to the ID of the Schoology group. This can be found by visiting the Schoology website and by navigating to the Schoology group. The group's ID should be right after the `/group/...` subdirectory of the domain. To get a Schoolopy API key and secret, goto `schoology.(PUT DOMAIN HERE).org/api`. There, you can manage your API credentials. You will need the key and secret to run the server. ### google + To setup Google OAuth2, go [here](https://developers.google.com/identity/oauth2/web/guides/get-google-api-clientid) and follow the instructions. Copy the json into a file called `google_auth.json` in the root of the project. In `config.py`, change `REDIRECT_URL` to the URL you used when you set up the Google project. Make sure that the redirect URL is added as one of the "Authorized redirect URIs." ## running -Now, you can execute `SCHOOLOGY_API_KEY="PUT KEY HERE" SCHOOLOGY_API_SECRET="PUT SECRET HERE" ./run.sh`. +Now, you can execute `SCHOOLOGY_API_KEY="PUT KEY HERE" SCHOOLOGY_API_SECRET="PUT SECRET HERE" ./run.sh`. diff --git a/database.db b/database.db new file mode 100644 index 0000000000000000000000000000000000000000..a3b1cf5bd45373f5ebffbc9fa01b822186c101a0 GIT binary patch literal 8192 zcmeI%F-yZh6ae76QV~R{;NVnvV+)cD4uX@lRvltRTXB+dxt`{v$%WjDn$5cESpTor zrWL#BYT-SSyj<>aFZs43y{kZ@f_sz20%4zRGtSu&0As8&KV6yych~CtewwD^zoNpt zr}r&a#cQX`9TX%$0wh2JBtQZrKmsH{0wh2JB(TB);{(3FTd(uC7K(7B(#$+6iFT1I z?dwjf-+_MXIOxFQDKs|VMV-<=(ttwPJ<&>D5Vq1W14cNPq-LfCNZ@1W14cNZ`K;jH`U@ta)CvO`PVa zGU!RI61P8ZkMa<1RcPIR`36SX!gD!jzE(eX=AUyn`Pe)Wu@(tj6xmQrgcl*E_R#kq fG)8&oNfZ0ANo1rH3g3-pT?V%7GLWu|=Ssf-C3tCn literal 0 HcmV?d00001 diff --git a/database.json b/database.json index e5868d1..89638d3 100644 --- a/database.json +++ b/database.json @@ -6,8 +6,7 @@ "name": "Computer Science Club Website", "description": "(this website)", "authors": ["Damian Myrda"], - "source": "https://github.com/moncheeta/computer_science_club.git", - "images": null + "source": "https://github.com/moncheeta/computer_science_club.git" } ] } diff --git a/database.py b/database.py index 08bd2e2..c0c1437 100644 --- a/database.py +++ b/database.py @@ -1,32 +1,92 @@ -import os -from jsonpickle import decode, encode +from models import Project +import jsonpickle as json +import sqlite3 +import pickle class Data: projects = [] - def __init__(self, projects): + def __init__(self, projects=[]): self.projects = projects -class Database: +class JSONDatabase: + def __init__(self): + self.projects = [] + self.read() + def read(self): - if not os.path.isfile("database.json"): - return Data([]) with open("database.json", "r") as file: - return decode(file.read()) + self.projects = json.decode(file.read()).projects + return self.projects - def write(self, data): + def write(self): with open("database.json", "w") as file: - file.write(encode(data)) + file.write(json.encode(Data(self.projects))) + + def add(self, project): + self.projects.append(project) + self.write() + + +class SQLDatabase: + def __init__(self): + self.connection = sqlite3.connect("database.db", check_same_thread=False) + self.cursor = self.connection.cursor() + self.cursor.execute( + """CREATE TABLE IF NOT EXISTS projects ( + name text NOT NULL, + description text, + authors text NOT NULL, + source text + )""" + ) + self.write() + self.read() + + def __del__(self): + self.connection.close() + + def read(self): + self.projects = [] + self.cursor.execute("SELECT * FROM projects") + for data in self.cursor.fetchall(): + name = data[0] + description = data[1] + authors = pickle.loads(data[2]) + source = data[3] + project = Project(name, description, authors, source) + self.projects.append(project) + return self.projects + + def write(self): + self.connection.commit() + + def add(self, project): + self.projects.append(project) + data = ( + project.name, + project.description, + pickle.dumps(project.authors), + project.source, + ) + self.cursor.execute("INSERT INTO projects VALUES (?, ?, ?, ?)", data) + self.write() class ProjectDatabase: + def __init__(self): + self.database = SQLDatabase() + def read(self): - projects = [] - for project in Database().read().projects: - projects.append(project) - return projects + return self.database.read() + + def write(self): + self.database.write() + + def add(self, project): + self.database.add(project) + - def write(self, projects): - Database().write(Data(projects)) +database = ProjectDatabase() diff --git a/main.py b/main.py index 21718fd..c2498e2 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ sys.path.append(os.path.join(PROJECT_DIR, "projects.py")) sys.path.append(os.path.join(PROJECT_DIR, "schoology.py")) from config import REDIRECT_URL from models import Project -from database import ProjectDatabase +from database import database from schoology import group from flask import Flask, request, redirect, abort, render_template, session from cachecontrol import CacheControl @@ -45,15 +45,16 @@ def add_project(): return abort(401) name = request.form["name"].rstrip() description = request.form["description"].rstrip() + if description == "": + description = None authors = request.form["authors"] for author in authors: author = author.rstrip() source = request.form["source"].rstrip() if source == "": source = None - images = request.files.get("images") - group.projects.append(Project(name, description, [authors], source, images)) - ProjectDatabase().write(group.projects) + project = Project(name, description, [authors], source) + database.add(project) return redirect("/projects") return render_template( "projects.html", group=group, create=True, account=session.get("name") diff --git a/models/project.py b/models/project.py index 3484966..06f90ec 100644 --- a/models/project.py +++ b/models/project.py @@ -3,11 +3,9 @@ class Project: description = "" authors = [] source = None - images = None - def __init__(self, name, description, authors, source=None, images=None): + def __init__(self, name, description, authors, source=None): self.name = name self.description = description self.authors = authors self.source = source - self.images = images diff --git a/projects.py b/projects.py deleted file mode 100644 index 6074ee9..0000000 --- a/projects.py +++ /dev/null @@ -1,19 +0,0 @@ -from models import Project -from database import Database, Data - - -class ProjectDatabase: - def read(self): - projects = [] - for project in Database().read()["projects"]: - name = project.get("name") - description = project.get("description") - authors = project.get("authors") - source = project.get("source") - images = project.get("images") - projects.append(Project(name, description, authors, source, images)) - return projects - - def write(self, projects): - with open("database.json", "w") as file: - file.write(encode(Data(projects))) diff --git a/schoology.py b/schoology.py index 7715362..0b409b6 100644 --- a/schoology.py +++ b/schoology.py @@ -2,7 +2,7 @@ import os from datetime import datetime from config import DOMAIN, GROUP_ID from models import Group, Event, Update, Discussion, Member -from database import ProjectDatabase +from database import database from schoolopy import Schoology, Auth @@ -68,7 +68,7 @@ class SchoologyAPI: events = self.events() updates = self.updates() discussions = self.discussions() - projects = ProjectDatabase().read() + projects = database.read() members = self.members() return Group(name, description, events, updates, discussions, projects, members) diff --git a/templates/elements/projects.html b/templates/elements/projects.html index c86dca6..4c6067d 100644 --- a/templates/elements/projects.html +++ b/templates/elements/projects.html @@ -1,7 +1,8 @@ {% import "elements/authors.html" as authors %} {% macro view(project) -%}

{{ project.name }}

+{% if project.description %}

{{ project.description }}

-{% if project.source %} +{% endif %} {% if project.source %} Source Code
{% endif %} {{ authors.list(project.authors) }} {% if project.images %} {% for @@ -21,7 +22,7 @@ project in projects %}
- +
@@ -29,9 +30,6 @@ project in projects %}
- - -
-- 2.43.4