Skip to content

Commit 3ca7cd1

Browse files
0.4.4
1 parent 7c973db commit 3ca7cd1

10 files changed

Lines changed: 485 additions & 24 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

.env

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DATABASE_URL=postgresql://postgres:unimi@localhost:5432/Authentication
2-
OPENAI_API_KEY = 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
3-
OPENAI_THREAD_ID = thread_GHd7arrTE7imixNtBSdn1WKL
4-
OPENAI_ASSISTANT_ID = asst_JD2zzWLdb2wKWsIsHonqli2A
2+
OPENAI_API_KEY=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
3+
OPENAI_THREAD_ID=thread_GHd7arrTE7imixNtBSdn1WKL
4+
OPENAI_ASSISTANT_ID=asst_JD2zzWLdb2wKWsIsHonqli2A
55
SECRET_KEY=bc8f3e7ceed4feb4eb898d851b9e46b5977ec2f4eeb776daca8fe187eb6331d7
6-
EMAIL_PASSWORD =vnco cbrz emlq tbee
6+
EMAIL_PASSWORD=vnco cbrz emlq tbee

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
__pycache__
22
venv
3+
.env

database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dotenv import load_dotenv
55
import os
66

7-
load_dotenv()
7+
load_dotenv(override=True)
88

99
DATABASE_URL = os.getenv("DATABASE_URL")
1010

database.sql

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
CREATE TABLE Users (
2+
username VARCHAR(50) PRIMARY KEY,
3+
email VARCHAR(255) NOT NULL,
4+
password VARCHAR(255) NOT NULL,
5+
firstName VARCHAR(20) NOT NULL,
6+
lastName VARCHAR(20) NOT NULL,
7+
birthDate DATE NOT NULL,
8+
status VARCHAR(8) NOT NULL CHECK (status IN ('pending', 'active', 'disabled', 'blocked'))
9+
);
10+
11+
CREATE TABLE Policy (
12+
name VARCHAR(8) PRIMARY KEY CHECK (name IN ('trial', 'silver', 'gold', 'platinum')),
13+
maxAccess INT,
14+
threshold INT
15+
);
16+
17+
CREATE TABLE Operation (
18+
name VARCHAR(50) PRIMARY KEY,
19+
target VARCHAR(8) CHECK (target IN ('class', 'relation', 'subgraph')),
20+
description VARCHAR(100) NOT NULL
21+
);
22+
23+
CREATE TABLE Category (
24+
name VARCHAR(13) PRIMARY KEY CHECK (name IN ('add', 'fix', 'reification', 'explain',
25+
'openGPTDialog', 'generate'))
26+
);
27+
28+
CREATE TABLE UserSubscribesPolicy (
29+
username VARCHAR(50) REFERENCES Users(username) ON DELETE CASCADE,
30+
startDate TIMESTAMP,
31+
endDate TIMESTAMP,
32+
requestDate TIMESTAMP,
33+
status VARCHAR(8) NOT NULL CHECK (status IN ('pending', 'active', 'rejected', 'expired')),
34+
policyName VARCHAR(8) NOT NULL CHECK (policyName IN ('trial', 'silver', 'gold', 'platinum'))
35+
REFERENCES Policy(name) ON DELETE RESTRICT,
36+
PRIMARY KEY (username, requestDate)
37+
);
38+
39+
CREATE TABLE UserMadeOperation (
40+
username VARCHAR(50) REFERENCES Users(username) ON DELETE CASCADE,
41+
date TIMESTAMP,
42+
operationName VARCHAR(50) NOT NULL REFERENCES Operation(name) ON DELETE RESTRICT,
43+
PRIMARY KEY (username, date)
44+
);
45+
46+
CREATE TABLE OperationIsCategory (
47+
operationName VARCHAR(50) REFERENCES Operation(name) ON DELETE CASCADE,
48+
categoryName VARCHAR(13) REFERENCES Category(name) ON DELETE RESTRICT CHECK
49+
(categoryName IN ('add', 'fix', 'reification', 'explain', 'openGPTDialog', 'generate')),
50+
PRIMARY KEY (operationName, categoryName)
51+
);
52+
53+
CREATE TABLE PolicyAllowsCategory (
54+
policyName VARCHAR(8) CHECK (policyName IN ('trial', 'silver', 'gold', 'platinum')) REFERENCES
55+
Policy(name) ON DELETE CASCADE,
56+
categoryName VARCHAR(13) REFERENCES Category(name) ON DELETE RESTRICT CHECK
57+
(categoryName IN ('add', 'fix', 'reification', 'explain', 'openGPTDialog', 'generate')),
58+
PRIMARY KEY (policyName, categoryName)
59+
);
60+
61+
62+
63+
64+
INSERT INTO Policy (name, maxAccess, threshold)
65+
VALUES
66+
('trial', 10, 3),
67+
('silver', 50, 10),
68+
('gold', 100, 15),
69+
('platinum', null, null);
70+
71+
INSERT INTO Operation (name, target, description)
72+
VALUES
73+
('AddClassSimilarToClass', 'class', 'add a new class semantically similar to another class'),
74+
('AddClassAssociatedToClass', 'class', 'add a new class in relation with another class'),
75+
('AddAttributeToRelationship', 'relation', 'add relevant attributes to a relationship'),
76+
('AddClassesSimilarToEntities', 'subgraph', 'add one or more new classes that semantically fit the context
77+
defined by subgraph'),
78+
('ReifyClass', 'class', 'create a class for representing the instance of a class'),
79+
('ExplainClass', 'class', 'explain in human-friendly terms the role of class in the schema'),
80+
('ExplainEntities', 'subgraph', 'explain in human-friendly terms the role of a portion of the schema in the
81+
schema'),
82+
('FixClassName', 'class', 'rename a class'),
83+
('FixClassOntology', 'class', 'enhance the relevant ontologies for a class'),
84+
('FixRelationshipCardinality', 'relation', 'fix cardinality for a relationship'),
85+
('OpenGPTDialog', NULL, 'openGPTDialog'),
86+
('Generate', NULL, 'generate');
87+
88+
INSERT INTO Category (name)
89+
VALUES
90+
('add'),
91+
('fix'),
92+
('reification'),
93+
('explain'),
94+
('openGPTDialog'),
95+
('generate');
96+
97+
INSERT INTO OperationIsCategory (operationName, categoryName)
98+
VALUES
99+
('AddClassSimilarToClass', 'add'),
100+
('AddClassAssociatedToClass', 'add'),
101+
('AddAttributeToRelationship', 'add'),
102+
('AddClassesSimilarToEntities', 'add'),
103+
('ReifyClass', 'reification'),
104+
('ExplainClass', 'explain'),
105+
('ExplainEntities', 'explain'),
106+
('FixClassName', 'fix'),
107+
('FixClassOntology', 'fix'),
108+
('FixRelationshipCardinality', 'fix'),
109+
('OpenGPTDialog', 'openGPTDialog'),
110+
('Generate', 'generate');
111+
112+
INSERT INTO PolicyAllowsCategory (policyName, categoryName)
113+
VALUES
114+
('trial', 'add'),
115+
('trial', 'fix'),
116+
('trial', 'reification'),
117+
('trial', 'explain'),
118+
('trial', 'openGPTDialog'),
119+
('trial', 'generate'),
120+
('silver', 'add'),
121+
('silver', 'fix'),
122+
('silver', 'reification'),
123+
('silver', 'explain'),
124+
('silver', 'openGPTDialog'),
125+
('silver', 'generate'),
126+
('gold', 'add'),
127+
('gold', 'fix'),
128+
('gold', 'reification'),
129+
('gold', 'explain'),
130+
('gold', 'openGPTDialog'),
131+
('gold', 'generate'),
132+
('platinum', 'add'),
133+
('platinum', 'fix'),
134+
('platinum', 'reification'),
135+
('platinum', 'explain'),
136+
('platinum', 'openGPTDialog'),
137+
('platinum', 'generate');
138+
139+
140+
141+
142+
143+
CREATE OR REPLACE FUNCTION notify_user_status() RETURNS trigger AS $notify_user_status$
144+
DECLARE
145+
to_email TEXT;
146+
user_name TEXT;
147+
BEGIN
148+
IF OLD.status IS DISTINCT FROM NEW.status THEN
149+
to_email := NEW.email;
150+
user_name := NEW.username;
151+
PERFORM pg_notify('user_status', to_email || ',' || user_name || ',' || NEW.status::TEXT);
152+
END IF;
153+
RETURN NEW;
154+
END;
155+
$notify_user_status$ LANGUAGE plpgsql;
156+
157+
CREATE TRIGGER send_status_email
158+
AFTER UPDATE OF status ON Users
159+
FOR EACH ROW
160+
WHEN (OLD.status IS DISTINCT FROM NEW.status)
161+
EXECUTE FUNCTION notify_user_status() ;
162+
163+
164+
165+
166+
167+
CREATE OR REPLACE FUNCTION notify_policy_status() RETURNS trigger AS $notify_policy_status$
168+
DECLARE
169+
to_email TEXT;
170+
user_name TEXT;
171+
BEGIN
172+
IF OLD.status IS DISTINCT FROM NEW.status THEN
173+
SELECT email INTO to_email FROM Users WHERE username = NEW.username;
174+
user_name := NEW.username;
175+
PERFORM pg_notify('policy_status', to_email || ',' || user_name || ',' || NEW.status::TEXT);
176+
END IF;
177+
RETURN NEW;
178+
END;
179+
$notify_policy_status$ LANGUAGE plpgsql;
180+
181+
CREATE TRIGGER send_policy_status_email
182+
AFTER UPDATE OF status ON UserSubscribesPolicy
183+
FOR EACH ROW
184+
WHEN (OLD.status IS DISTINCT FROM NEW.status)
185+
EXECUTE FUNCTION notify_policy_status() ;
186+

email_listener.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
local_tz = pytz.timezone("Europe/Rome")
1111

12-
load_dotenv()
12+
load_dotenv(override=True)
1313

1414
DATABASE_URL = os.getenv("DATABASE_URL")
1515

@@ -25,8 +25,8 @@ async def handle_notify(connection, pid, channel_name, payload):
2525
"Great news! Your SchemaLink account has been approved.\n\n"
2626
"You have been granted a Trial policy that allows up to 10 intelligent requests "
2727
"within the next 24 hours. After this period, your access may be limited unless you upgrade "
28-
"to a higher tier.\n\n"
29-
"Thank you for joining SchemaLink, and we hope you enjoy using the platform!\n\n"
28+
"your policy to an upper tier.\n\n"
29+
"Thank you for joining SchemaLink!\n\n"
3030
f"Best regards,\n"
3131
f"The SchemaLink Team"
3232
)

gmail_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
current_window = None
99

10-
load_dotenv()
10+
load_dotenv(override=True)
1111

1212
EMAIL_ADDRESS = 'schemalinkanacleto@gmail.com'
1313
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")

main.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
local_tz = pytz.timezone("Europe/Rome")
3535

36-
load_dotenv()
36+
load_dotenv(override=True)
3737

3838
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
3939
OPENAI_THREAD_ID = os.getenv("OPENAI_THREAD_ID")
@@ -127,7 +127,7 @@ def get_db ():
127127

128128
app.add_middleware(
129129
CORSMiddleware,
130-
allow_origins=["schemalink.anacleto.di.unimi.it"],
130+
allow_origins=["schemalink.anacleto.di.unimi.it", "http://localhost:8000","http://localhost:4200",],
131131
allow_credentials=True,
132132
allow_methods=["*"], # Allows all methods
133133
allow_headers=["*"], # Allows all headers
@@ -343,7 +343,7 @@ async def get_user_subscriptions(db: db_dependency):
343343
async def update_subscription_status( status_subscription_update: UpdateUserStatusRequest, db: db_dependency):
344344
policySubscription = db.query(models.UserSubscribesPolicy).filter(
345345
models.UserSubscribesPolicy.username == status_subscription_update.username,
346-
models.UserSubscribesPolicy.status == "pending" # Filtro aggiunto
346+
models.UserSubscribesPolicy.status == "pending"
347347
).first()
348348

349349

@@ -423,12 +423,12 @@ async def check_user_operation(request_data: OperationRequest, db: db_dependency
423423
username = request_data.username
424424
operation = request_data.operation
425425

426-
print("Username ricevuto:", username)
427-
print("Operation ricevuta:", operation)
426+
print("Username received:", username)
427+
print("Operation received:", operation)
428428

429429
# Username null
430430
if not username:
431-
return JSONResponse(content={"allowed": False, "reason": "You must be logged in to performs operation."})
431+
return JSONResponse(content={"allowed": False, "reason": "You must be logged in to perform intelligent requests."})
432432

433433
# Admin user
434434
if username == "schemalink":
@@ -474,7 +474,7 @@ async def check_user_operation(request_data: OperationRequest, db: db_dependency
474474
).scalar()
475475

476476
if user_ops_count >= max_access:
477-
return JSONResponse(content={"allowed": False, "reason": "You reached the maximum number of allowed operations for your policy."})
477+
return JSONResponse(content={"allowed": False, "reason": "You reached the maximum number of intelligent requests for your policy."})
478478

479479
return JSONResponse(content={"allowed": True, "policy": policy_name})
480480

@@ -519,9 +519,9 @@ async def log_user_operation(operation: UserMadeOperationInput, db: db_dependenc
519519
subject = f"You have {policy.threshold} operations remaining on your '{policy.name}' plan"
520520
body = (
521521
f"Hi {user.username},\n\n"
522-
f"You have {policy.threshold} operations remaining"
522+
f"You have {policy.threshold} intelligent requests remaining"
523523
f"under your current '{policy.name}' subscription plan.\n\n"
524-
f"Once you reach the limit of {policy.maxAccess} operations, your subscription will expire "
524+
f"Once you reach the limit of {policy.maxAccess} intelligent requests, your subscription will expire "
525525
f"and you will no longer be able to use intelligent requests.\n\n"
526526
f"To continue uninterrupted, consider upgrading or renewing your plan.\n\n"
527527
f"Thank you for using SchemaLink!\n"
@@ -786,18 +786,18 @@ async def contribute_on_ai_store(request: ContributeRequest, db: db_dependency):
786786
subject = "New contribution received"
787787
body = (
788788
f"The gold/platinum user {user.username} ({user.email}) "
789-
f"proposes the schema '{request.diagramName}' to the AI store. "
790-
f"See attached file for the graph."
789+
f"proposes the schema '{request.diagramName}' to be included into the AI store. "
790+
f"See attached file for the SchemaLink JSON internal representation."
791791
f"\n\nSchemaLink Notification System"
792792
)
793793
to_email = "schemalinkanacleto@gmail.com"
794794

795795
send_email(to_email=to_email, subject=subject, message=body, attachment=temp_file_path)
796796

797-
subject = "Thanks for your contribution to AI store"
797+
subject = "Thanks for contributing to the AI store"
798798
body = (
799799
f"Hi {user.username},\n\n"
800-
f"Thank you for your contribution to the AI store! "
800+
f"Thank you for contributing to the AI store! "
801801
f"Your schema '{request.diagramName}' has been received and is under review.\n"
802802
f"\nBest regards,\n"
803803
f"The SchemaLink Team"

0 commit comments

Comments
 (0)