From faaba48fdf2b30125f7ddc8d53ecb330c3c09380 Mon Sep 17 00:00:00 2001 From: Raffu Khondaker <2022rkhondak@tjhsst.edu> Date: Sun, 14 Jun 2020 23:11:37 -0400 Subject: [PATCH 1/3] cli start --- CLI/student.py | 18 +-- .../api/migrations/0005_auto_20200615_0048.py | 23 +++ Website/api/models.py | 1 + Website/api/permissions.py | 2 +- Website/api/serializers.py | 7 +- Website/users/views.py | 4 +- skoolos.py | 148 +++++++++--------- 7 files changed, 111 insertions(+), 92 deletions(-) create mode 100644 Website/api/migrations/0005_auto_20200615_0048.py diff --git a/CLI/student.py b/CLI/student.py index 711c99e..daec0d5 100644 --- a/CLI/student.py +++ b/CLI/student.py @@ -66,15 +66,12 @@ class Student: def __init__(self, data): # teacher info already stored in API # intitialze fields after GET request - self.first_name=data['first_name'] - self.last_name=data['last_name'] self.git=data['git'] self.username=data['ion_user'] self.url= "http://127.0.0.1:8000/api/students/" + self.username + "/" - self.email = data['email'] self.grade = data['grade'] - self.student_id=data['student_id'] self.completed = data['completed'] + self.user = data['user'] #classes in id form (Example: 4,5) #storing actual classes cid=data['classes'].split(",") @@ -132,15 +129,12 @@ class Student: os.chdir(cdir) self.repo = 'https://github.com/' + self.git + '/' + self.username + '.git' data={ - 'first_name':self.first_name, - 'last_name':self.last_name, + 'user':self.user, 'git':self.git, 'ion_user':self.username, - 'student_id':self.student_id, 'added_to':self.snew, 'url':self.url, 'classes':self.sclass, - 'email':self.email, 'grade':self.grade, 'completed':self.completed, 'repo':self.repo @@ -284,15 +278,13 @@ class Student: #update teacher instance in db, classes field data={ - 'first_name':self.first_name, - 'last_name':self.last_name, + 'user':self.user, 'git':self.git, 'ion_user':self.username, 'student_id':self.student_id, 'added_to':self.snew, 'url':self.url, 'classes':self.sclass, - 'email':self.email, 'grade':self.grade, 'completed':self.completed } @@ -327,15 +319,13 @@ class Student: command("git push -u origin " + self.username + " --tags") self.completed = self.completed + "," + parts[1] + "/" + parts[2] data={ - 'first_name':self.first_name, - 'last_name':self.last_name, + 'user':self.user, 'git':self.git, 'ion_user':self.username, 'student_id':self.student_id, 'added_to':self.snew, 'url':self.url, 'classes':self.sclass, - 'email':self.email, 'grade':self.grade, 'completed':self.completed } diff --git a/Website/api/migrations/0005_auto_20200615_0048.py b/Website/api/migrations/0005_auto_20200615_0048.py new file mode 100644 index 0000000..1a60cfd --- /dev/null +++ b/Website/api/migrations/0005_auto_20200615_0048.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.7 on 2020-06-15 00:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0004_auto_20200614_2107'), + ] + + operations = [ + migrations.RemoveField( + model_name='student', + name='id', + ), + migrations.AddField( + model_name='student', + name='ion_user', + field=models.CharField(default='2022rkhondak', max_length=100, primary_key=True, serialize=False), + preserve_default=False, + ), + ] diff --git a/Website/api/models.py b/Website/api/models.py index e8bb2b3..ea61069 100644 --- a/Website/api/models.py +++ b/Website/api/models.py @@ -50,6 +50,7 @@ class Teacher(models.Model): class Student(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) + ion_user=models.CharField(primary_key=True, max_length=100) grade = models.IntegerField(default=0, blank=True) git=models.CharField(default="", max_length=100, blank=True) repo=models.URLField(default="", blank=True) diff --git a/Website/api/permissions.py b/Website/api/permissions.py index 2d64429..7e0591c 100644 --- a/Website/api/permissions.py +++ b/Website/api/permissions.py @@ -13,7 +13,7 @@ class IsOwnerOrReadOnly(permissions.BasePermission): return True # Write permissions are only allowed to the owner of the snippet. - return obj.owner == request.user or request.user.is_superuser + return obj.user == request.user or request.user.is_superuser class isTeacher(permissions.BasePermission): #only teachers can make classes and assignmenst diff --git a/Website/api/serializers.py b/Website/api/serializers.py index 2904a07..238d6c1 100644 --- a/Website/api/serializers.py +++ b/Website/api/serializers.py @@ -5,8 +5,8 @@ from django.contrib.auth.models import User from .permissions import IsOwnerOrReadOnly,isTeacher class UserSerializer(serializers.HyperlinkedModelSerializer): - students = serializers.PrimaryKeyRelatedField(many=True, queryset=Student.objects.all()) - teachers = serializers.PrimaryKeyRelatedField(many=True, queryset=Teacher.objects.all()) + # students = serializers.PrimaryKeyRelatedField(many=True, queryset=Student.objects.all()) + # teachers = serializers.PrimaryKeyRelatedField(many=True, queryset=Teacher.objects.all()) class Meta: model = User @@ -38,11 +38,10 @@ class ClassesSerializer(serializers.HyperlinkedModelSerializer): class StudentSerializer(serializers.HyperlinkedModelSerializer): # classes = ClassesSerializer(many=True, read_only=True,allow_null=True) - owner = serializers.ReadOnlyField(source='owner.username') class Meta: model = Student # fields = ['url','first_name', 'last_name', 'grade','email','student_id', 'git','ion_user','classes','added_to','completed', 'repo','owner'] - fields = ['grade','email','student_id', 'git','ion_user','classes','added_to','completed', 'repo','owner'] + fields = ['url','grade', 'ion_user','git','user','classes','added_to','completed', 'repo'] class TeacherSerializer(serializers.ModelSerializer): # classes = ClassesSerializer(many=True, read_only=True,allow_null=True) diff --git a/Website/users/views.py b/Website/users/views.py index fa848da..ea3d0d0 100644 --- a/Website/users/views.py +++ b/Website/users/views.py @@ -100,9 +100,9 @@ def create_account (request): token.delete() if isStudent: - profile = Student(user=user, git=git, grade=grade) + profile = Student(owner=user, git=git, grade=grade, ion_user=username) else: - profile = Teacher(user=user, git=git) + profile = Teacher(owner=user, git=git, ion_user=username) profile.save() diff --git a/skoolos.py b/skoolos.py index a802814..bca6186 100644 --- a/skoolos.py +++ b/skoolos.py @@ -35,50 +35,71 @@ def main(): print("") if not os.path.exists(".profile"): - # try: - # URL = "http://127.0.0.1:8000/api/" - # r = requests.get(url = URL) - # print("Stop any processes running on http://127.0.0.1:8000/ before continuing") - # except: - # pass + try: + URL = "http://127.0.0.1:8000/api/" + r = requests.get(url = URL) + except: + print("Stop any processes running on http://127.0.0.1:8000/ before continuing") + sys.exit(0) + input("Welcome to SkoolOS. Press any key to create an account") #webbrowser.open("http://127.0.0.1:8000/login", new=2) authenticate() - # else: - # try: - # URL = "http://127.0.0.1:8000/api/" - # f = open('.profile','r') - # data = json.loads(f.read()) - # f.close() - # PWD = data['password'] - # USER = data['username'] - # r = requests.get(url = URL, auth=(USER,PWD)) - # except: - # print("Incorrect password.") - # sys.exit(0) - # if(data['is_student']): - # studentCLI() - # else: - # teacherCLI() + else: + f = open('.profile','r') + data = json.loads(f.read()) + f.close() + PWD = data['password'] + USER = data['username'] + print(data['username']) + if(data['is_student']): + studentCLI(USER, PWD) + else: + teacherCLI() # while True: # pass -def studentCLI(): +def studentCLI(user, password): from CLI import student - data = getStudent(USER) - print(data) + data = getStudent(user, password) student = student.Student(data) - print(student) + choices = ['1) View Class','2) Exit SkoolOS'] + questions = [ + { + 'type': 'list', + 'name': 'choice', + 'choices':choices, + 'message': 'Select class: ', + }, + ] + choice = prompt(questions) + choice = int(choice['choice'].split(")")[0]) + if(choice == 1): + carray = student.sclass.split(",") + if(len(carray) == 1 and carray[0] == ""): + print("No classes") + return + courses = [ + { + 'type': 'list', + 'name': 'course', + 'choices':carray, + 'message': 'Select class: ', + }, + ] + course = prompt(courses) + if(choice == 2): + student.exitCLI() def teacherCLI(): from CLI.teacher import Teacher print("fail") -def getStudent(ion_user): +def getStudent(ion_user, password): URL = "http://127.0.0.1:8000/api/students/" + ion_user + "/" - r = requests.get(url = URL, auth=('raffukhondaker','hackgroup1')) + r = requests.get(url = URL, auth=(ion_user,password)) if(r.status_code == 200): data = r.json() return data @@ -210,55 +231,40 @@ def authenticate(): pwd = data['pwd'] user = data['username'] print(r.status_code) - r = requests.get(url = "http://localhost:8000/students/" + user + "/", auth=(user,pwd)) + r = requests.get(url = "http://localhost:8000/api/students/" + user + "/", auth=(user,pwd)) is_student = False if(r.status_code == 200): is_student = True print("Welcome, student " + user) + r = requests.get(url = "http://localhost:8000/api/students/" + user + "/", auth=(user,pwd)) + profile = r.json() + username = profile['ion_user'] + grade = profile['grade'] + profile = { + 'username':username, + 'grade':grade, + 'is_student':is_student, + 'password':pwd, + } + profileFile = open(".profile", "w") + profileFile.write(json.dumps(profile)) + profileFile.close() + else: print("Welcome, teacher " + user) - - #print(code) - print(state) - - payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id, - 'client_secret': client_secret, 'csrfmiddlewaretoken': state} - token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() - #print(token) - headers = {'Authorization': f"Bearer {token['access_token']}"} - - # And finally get the user's profile! - profile = requests.get("https://ion.tjhsst.edu/api/profile", headers=headers).json() - - #pprint.pprint(profile) - username = profile['ion_username'] - email = profile['tj_email'] - first_name = profile['first_name'] - last_name = profile['last_name'] - is_student = profile['is_student'] - password = "" - #password creation - - profile = { - 'username':username, - 'email':email, - 'first_name':first_name, - 'last_name':last_name, - 'is_student':is_student, - 'password':password, - } - os.chdir(cdir) - - profileFile = open(".profile", "w") - profileFile.write(json.dumps(profile)) - profileFile.close() - - #try to make password - password = makePass() - profile['password'] = password - profileFile = open(".profile", "w") - profileFile.write(json.dumps(profile)) - profileFile.close() + r = requests.get(url = "http://localhost:8000/api/teachers/" + user + "/", auth=(user,pwd)) + profile = r.json() + username = profile['ion_user'] + grade = profile['grade'] + profile = { + 'username':username, + 'grade':grade, + 'is_student':is_student, + 'password':pwd, + } + profileFile = open(".profile", "w") + profileFile.write(json.dumps(profile)) + profileFile.close() sys.exit From 964bd516ed1a89d1f25408c0c63bfb773381d79d Mon Sep 17 00:00:00 2001 From: Nathaniel Kenschaft Date: Sun, 14 Jun 2020 23:11:53 -0400 Subject: [PATCH 2/3] polished bgservice --- bgservice/__init__.py | 0 {BackgroundService => bgservice}/bgservice.py | 9 ++++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 bgservice/__init__.py rename {BackgroundService => bgservice}/bgservice.py (94%) diff --git a/bgservice/__init__.py b/bgservice/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/BackgroundService/bgservice.py b/bgservice/bgservice.py similarity index 94% rename from BackgroundService/bgservice.py rename to bgservice/bgservice.py index e2c4411..db0f15e 100644 --- a/BackgroundService/bgservice.py +++ b/bgservice/bgservice.py @@ -76,14 +76,15 @@ class EventHandler(pyinotify.ProcessEvent): NOTIFIER = None +STDOUT = sys.stdout def watch_dir(watched_dir="/tmp", logdir="/tmp/skooloslogs"): if not os.path.exists(logdir): os.makedirs(logdir) logfile = open( - logdir + "skoolos_" + time.strftime("%m%d%Y-%H%M%S", time.localtime()), - 'w') + logdir + "/skoolos_" + + time.strftime("%m%d%Y-%H%M%S", time.localtime()), 'w') sys.stdout = logfile print("Start time: " + time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime()) + "\n\n") @@ -99,4 +100,6 @@ def watch_dir(watched_dir="/tmp", logdir="/tmp/skooloslogs"): def stop_watching(): NOTIFIER.stop() print("End time: " + - time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime()) + "\n\n") + time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime())) + sys.stdout = STDOUT + print("Done watching.\n") From b31f6146d29be70601db6d8700d92d45252f2116 Mon Sep 17 00:00:00 2001 From: Raffu Khondaker <2022rkhondak@tjhsst.edu> Date: Sun, 14 Jun 2020 23:43:49 -0400 Subject: [PATCH 3/3] teacher cli --- CLI/teacher.py | 12 ------------ skoolos.py | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/CLI/teacher.py b/CLI/teacher.py index d34ae79..fa82ca8 100644 --- a/CLI/teacher.py +++ b/CLI/teacher.py @@ -66,8 +66,6 @@ class Teacher: def __init__(self, data): # teacher info already stored in API # intitialze fields after GET request - self.first_name=data['first_name'] - self.last_name=data['last_name'] self.git=data['git'] self.username=data['ion_user'] self.url= "http://127.0.0.1:8000/api/teachers/" + self.username + "/" @@ -177,13 +175,10 @@ class Teacher: #update teacher instance in db, classes field teacher={ - 'first_name':self.first_name, - 'last_name':self.last_name, 'git':self.git, 'ion_user':self.username, 'url':self.url, 'classes':self.sclass, - 'email':self.email } putDB(teacher, self.url) @@ -250,13 +245,10 @@ class Teacher: s = s[:-1] print(s) data={ - 'first_name':self.first_name, - 'last_name':self.last_name, 'git':self.git, 'ion_user':self.username, 'url':self.url, 'classes':s, - 'email':self.email } print(putDB(data, self.url)) delDB("http://127.0.0.1:8000/api/classes/" + cname + "/") @@ -300,14 +292,10 @@ class Teacher: return False print(student['added_to']) s={ - 'first_name':student["first_name"], - 'last_name':student["last_name"], 'git':student["git"], 'ion_user':student["ion_user"], - 'student_id':student["student_id"], 'added_to':student['added_to'], 'classes':student["classes"], - 'email':student["email"], 'grade':student["grade"], 'completed':student["completed"], 'repo':student["repo"] diff --git a/skoolos.py b/skoolos.py index bca6186..1cfb9e3 100644 --- a/skoolos.py +++ b/skoolos.py @@ -55,7 +55,7 @@ def main(): if(data['is_student']): studentCLI(USER, PWD) else: - teacherCLI() + teacherCLI(USER, PWD) @@ -63,7 +63,7 @@ def main(): # pass def studentCLI(user, password): from CLI import student - data = getStudent(user, password) + data = getUser(user, password) student = student.Student(data) choices = ['1) View Class','2) Exit SkoolOS'] questions = [ @@ -92,12 +92,41 @@ def studentCLI(user, password): course = prompt(courses) if(choice == 2): student.exitCLI() + -def teacherCLI(): - from CLI.teacher import Teacher - print("fail") +def teacherCLI(user, password): + from CLI import teacher + data = getUser(user, password) + teacher = teacher.Teacher(data) + choices = ['1) View Class','2) Exit SkoolOS'] + questions = [ + { + 'type': 'list', + 'name': 'choice', + 'choices':choices, + 'message': 'Select class: ', + }, + ] + choice = prompt(questions) + choice = int(choice['choice'].split(")")[0]) + if(choice == 1): + carray = student.sclass.split(",") + if(len(carray) == 1 and carray[0] == ""): + print("No classes") + return + courses = [ + { + 'type': 'list', + 'name': 'course', + 'choices':carray, + 'message': 'Select class: ', + }, + ] + course = prompt(courses) + if(choice == 2): + student.exitCLI() -def getStudent(ion_user, password): +def getUser(ion_user, password): URL = "http://127.0.0.1:8000/api/students/" + ion_user + "/" r = requests.get(url = URL, auth=(ion_user,password)) if(r.status_code == 200):