cli start

This commit is contained in:
Raffu Khondaker 2020-06-14 23:11:37 -04:00
parent 32eb0c32e1
commit faaba48fdf
7 changed files with 111 additions and 92 deletions

View File

@ -66,15 +66,12 @@ class Student:
def __init__(self, data): def __init__(self, data):
# teacher info already stored in API # teacher info already stored in API
# intitialze fields after GET request # intitialze fields after GET request
self.first_name=data['first_name']
self.last_name=data['last_name']
self.git=data['git'] self.git=data['git']
self.username=data['ion_user'] self.username=data['ion_user']
self.url= "http://127.0.0.1:8000/api/students/" + self.username + "/" self.url= "http://127.0.0.1:8000/api/students/" + self.username + "/"
self.email = data['email']
self.grade = data['grade'] self.grade = data['grade']
self.student_id=data['student_id']
self.completed = data['completed'] self.completed = data['completed']
self.user = data['user']
#classes in id form (Example: 4,5) #classes in id form (Example: 4,5)
#storing actual classes #storing actual classes
cid=data['classes'].split(",") cid=data['classes'].split(",")
@ -132,15 +129,12 @@ class Student:
os.chdir(cdir) os.chdir(cdir)
self.repo = 'https://github.com/' + self.git + '/' + self.username + '.git' self.repo = 'https://github.com/' + self.git + '/' + self.username + '.git'
data={ data={
'first_name':self.first_name, 'user':self.user,
'last_name':self.last_name,
'git':self.git, 'git':self.git,
'ion_user':self.username, 'ion_user':self.username,
'student_id':self.student_id,
'added_to':self.snew, 'added_to':self.snew,
'url':self.url, 'url':self.url,
'classes':self.sclass, 'classes':self.sclass,
'email':self.email,
'grade':self.grade, 'grade':self.grade,
'completed':self.completed, 'completed':self.completed,
'repo':self.repo 'repo':self.repo
@ -284,15 +278,13 @@ class Student:
#update teacher instance in db, classes field #update teacher instance in db, classes field
data={ data={
'first_name':self.first_name, 'user':self.user,
'last_name':self.last_name,
'git':self.git, 'git':self.git,
'ion_user':self.username, 'ion_user':self.username,
'student_id':self.student_id, 'student_id':self.student_id,
'added_to':self.snew, 'added_to':self.snew,
'url':self.url, 'url':self.url,
'classes':self.sclass, 'classes':self.sclass,
'email':self.email,
'grade':self.grade, 'grade':self.grade,
'completed':self.completed 'completed':self.completed
} }
@ -327,15 +319,13 @@ class Student:
command("git push -u origin " + self.username + " --tags") command("git push -u origin " + self.username + " --tags")
self.completed = self.completed + "," + parts[1] + "/" + parts[2] self.completed = self.completed + "," + parts[1] + "/" + parts[2]
data={ data={
'first_name':self.first_name, 'user':self.user,
'last_name':self.last_name,
'git':self.git, 'git':self.git,
'ion_user':self.username, 'ion_user':self.username,
'student_id':self.student_id, 'student_id':self.student_id,
'added_to':self.snew, 'added_to':self.snew,
'url':self.url, 'url':self.url,
'classes':self.sclass, 'classes':self.sclass,
'email':self.email,
'grade':self.grade, 'grade':self.grade,
'completed':self.completed 'completed':self.completed
} }

View File

@ -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,
),
]

View File

@ -50,6 +50,7 @@ class Teacher(models.Model):
class Student(models.Model): class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) 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) grade = models.IntegerField(default=0, blank=True)
git=models.CharField(default="", max_length=100, blank=True) git=models.CharField(default="", max_length=100, blank=True)
repo=models.URLField(default="", blank=True) repo=models.URLField(default="", blank=True)

View File

@ -13,7 +13,7 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
return True return True
# Write permissions are only allowed to the owner of the snippet. # 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): class isTeacher(permissions.BasePermission):
#only teachers can make classes and assignmenst #only teachers can make classes and assignmenst

View File

@ -5,8 +5,8 @@ from django.contrib.auth.models import User
from .permissions import IsOwnerOrReadOnly,isTeacher from .permissions import IsOwnerOrReadOnly,isTeacher
class UserSerializer(serializers.HyperlinkedModelSerializer): class UserSerializer(serializers.HyperlinkedModelSerializer):
students = serializers.PrimaryKeyRelatedField(many=True, queryset=Student.objects.all()) # students = serializers.PrimaryKeyRelatedField(many=True, queryset=Student.objects.all())
teachers = serializers.PrimaryKeyRelatedField(many=True, queryset=Teacher.objects.all()) # teachers = serializers.PrimaryKeyRelatedField(many=True, queryset=Teacher.objects.all())
class Meta: class Meta:
model = User model = User
@ -38,11 +38,10 @@ class ClassesSerializer(serializers.HyperlinkedModelSerializer):
class StudentSerializer(serializers.HyperlinkedModelSerializer): class StudentSerializer(serializers.HyperlinkedModelSerializer):
# classes = ClassesSerializer(many=True, read_only=True,allow_null=True) # classes = ClassesSerializer(many=True, read_only=True,allow_null=True)
owner = serializers.ReadOnlyField(source='owner.username')
class Meta: class Meta:
model = Student model = Student
# fields = ['url','first_name', 'last_name', 'grade','email','student_id', 'git','ion_user','classes','added_to','completed', 'repo','owner'] # 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): class TeacherSerializer(serializers.ModelSerializer):
# classes = ClassesSerializer(many=True, read_only=True,allow_null=True) # classes = ClassesSerializer(many=True, read_only=True,allow_null=True)

View File

@ -100,9 +100,9 @@ def create_account (request):
token.delete() token.delete()
if isStudent: if isStudent:
profile = Student(user=user, git=git, grade=grade) profile = Student(owner=user, git=git, grade=grade, ion_user=username)
else: else:
profile = Teacher(user=user, git=git) profile = Teacher(owner=user, git=git, ion_user=username)
profile.save() profile.save()

View File

@ -35,50 +35,71 @@ def main():
print("") print("")
if not os.path.exists(".profile"): if not os.path.exists(".profile"):
# try: try:
# URL = "http://127.0.0.1:8000/api/" URL = "http://127.0.0.1:8000/api/"
# r = requests.get(url = URL) r = requests.get(url = URL)
# print("Stop any processes running on http://127.0.0.1:8000/ before continuing") except:
# except: print("Stop any processes running on http://127.0.0.1:8000/ before continuing")
# pass sys.exit(0)
input("Welcome to SkoolOS. Press any key to create an account") input("Welcome to SkoolOS. Press any key to create an account")
#webbrowser.open("http://127.0.0.1:8000/login", new=2) #webbrowser.open("http://127.0.0.1:8000/login", new=2)
authenticate() authenticate()
# else: else:
# try: f = open('.profile','r')
# URL = "http://127.0.0.1:8000/api/" data = json.loads(f.read())
# f = open('.profile','r') f.close()
# data = json.loads(f.read()) PWD = data['password']
# f.close() USER = data['username']
# PWD = data['password'] print(data['username'])
# USER = data['username'] if(data['is_student']):
# r = requests.get(url = URL, auth=(USER,PWD)) studentCLI(USER, PWD)
# except: else:
# print("Incorrect password.") teacherCLI()
# sys.exit(0)
# if(data['is_student']):
# studentCLI()
# else:
# teacherCLI()
# while True: # while True:
# pass # pass
def studentCLI(): def studentCLI(user, password):
from CLI import student from CLI import student
data = getStudent(USER) data = getStudent(user, password)
print(data)
student = student.Student(data) 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(): def teacherCLI():
from CLI.teacher import Teacher from CLI.teacher import Teacher
print("fail") print("fail")
def getStudent(ion_user): def getStudent(ion_user, password):
URL = "http://127.0.0.1:8000/api/students/" + ion_user + "/" 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): if(r.status_code == 200):
data = r.json() data = r.json()
return data return data
@ -210,55 +231,40 @@ def authenticate():
pwd = data['pwd'] pwd = data['pwd']
user = data['username'] user = data['username']
print(r.status_code) 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 is_student = False
if(r.status_code == 200): if(r.status_code == 200):
is_student = True is_student = True
print("Welcome, student " + user) 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: else:
print("Welcome, teacher " + user) print("Welcome, teacher " + user)
r = requests.get(url = "http://localhost:8000/api/teachers/" + user + "/", auth=(user,pwd))
#print(code) profile = r.json()
print(state) username = profile['ion_user']
grade = profile['grade']
payload = {'grant_type': 'authorization_code', 'code': code, 'redirect_uri': redirect_uri, 'client_id': client_id, profile = {
'client_secret': client_secret, 'csrfmiddlewaretoken': state} 'username':username,
token = requests.post("https://ion.tjhsst.edu/oauth/token/", data=payload).json() 'grade':grade,
#print(token) 'is_student':is_student,
headers = {'Authorization': f"Bearer {token['access_token']}"} 'password':pwd,
}
# And finally get the user's profile! profileFile = open(".profile", "w")
profile = requests.get("https://ion.tjhsst.edu/api/profile", headers=headers).json() profileFile.write(json.dumps(profile))
profileFile.close()
#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()
sys.exit sys.exit