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