From ddea6db3fff4e0ba0e3aeccd3cf1e2c7e92e8a56 Mon Sep 17 00:00:00 2001 From: Raffu Khondaker <2022rkhondak@tjhsst.edu> Date: Mon, 8 Jun 2020 00:18:46 -0400 Subject: [PATCH] teacher methods --- CLI/t-git.py | 222 ++++++++++++++---- .../api/migrations/0004_auto_20200608_0220.py | 18 ++ .../api/migrations/0005_auto_20200608_0222.py | 18 ++ .../api/migrations/0006_auto_20200608_0223.py | 18 ++ .../migrations/0007_classes_default_file.py | 19 ++ .../api/migrations/0008_auto_20200608_0341.py | 22 ++ Website/api/models.py | 5 +- Website/api/serializers.py | 2 +- 8 files changed, 271 insertions(+), 53 deletions(-) create mode 100644 Website/api/migrations/0004_auto_20200608_0220.py create mode 100644 Website/api/migrations/0005_auto_20200608_0222.py create mode 100644 Website/api/migrations/0006_auto_20200608_0223.py create mode 100644 Website/api/migrations/0007_classes_default_file.py create mode 100644 Website/api/migrations/0008_auto_20200608_0341.py diff --git a/CLI/t-git.py b/CLI/t-git.py index 1241390..26b211a 100644 --- a/CLI/t-git.py +++ b/CLI/t-git.py @@ -4,60 +4,188 @@ import requests #git clone student directory ==> /classes/assignments -def initTeacher(ion_user): - #check if git has already been initialized - if(os.path.exists(str(ion_user) +"/" + ".git")): - print("Already synced to: " + str(ion_user)) - return +''' +{ + "url": "http://127.0.0.1:8000/teachers/eharris1/", + "first_name": "Errin", + "last_name": "Harris", + "classes": [ + { + "url": "http://127.0.0.1:8000/classes/1/", + "name": "Math5", + "assignments": [ + { + "name": "Week1_HW", + "due_date": "2020-06-07T07:46:30.537197Z", + "url": "http://127.0.0.1:8000/assignments/1/", + "files": [ + { + "name": "instructions.txt" + } + ] + }, + { + "name": "Week2_HW", + "due_date": "2020-06-07T07:46:30.548596Z", + "url": "http://127.0.0.1:8000/assignments/2/", + "files": [ + { + "name": "instructions.txt" + } + ] + } + ], + "repo": "" + } + ], + "git": "therealraffi", + "ion_user": "eharris1" + }, +''' +#get teacher info from api +def getData(ion_user): + URL = "http://127.0.0.1:8000/students/" + ion_user + "/" + r = requests.get(url = URL, auth=('student','_$YFE#34.9_37jr')) + if(r.status_code == 200): + data = r.json() + return data + elif(r.status_code == 404): + return None + print("Make new account!") + elif(r.status_code == 403): + return None + print("Invalid username/password") + else: + return None + print(r.status_code) - #get student repo from API - URL = "http://127.0.0.1:8000/students/" + ion_user + "/" - r = requests.get(url = URL, auth=('student','_$YFE#34.9_37jr')) - if(r.status_code == 200): - data = r.json() - repo = data['repo'] - classes = data['classes'] - print(data) - #git clone repo - process = subprocess.Popen(['git', 'clone', repo], stdout=subprocess.PIPE,stderr=subprocess.PIPE) - process.wait() +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.classes=data['classes'] + self.git=data['git'] + self.username=data['ion_user'] + self.url= "http://127.0.0.1:8000/teachers/" + self.username + "/" + self.data=data + if(os.path.exists(self.username )): + print("Already synced to: " + str(self.username)) + return + os.mkdir(self.username) + classes = self.classes # make classes directory for c in classes: - cpath = str(ion_user) + "/" + c['name'] + cname= c['name'] + "_" + self.username + cpath = self.username + "/" + cname if(os.path.exists(cpath)): print(cpath + " already exists...") else: - os.mkdir(str(ion_user) + "/" + c['name']) - - #make assignments directory - for a in c['assignments']: - path = str(ion_user) + "/" + c['name'] + "/" + a['name'] - print(path) - if(os.path.exists("/" +path)): - print(path + " already exists...") - else: - os.mkdir(str(ion_user) + "/" + c['name'] + "/" + a['name']) - - #push to remote repo - os.chdir(ion_user) - process = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) - process.wait() - process = subprocess.Popen(['git', 'add', '.'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) - process.wait() - process = subprocess.Popen(['git', 'commit', '-m', "First Commit"], stdout=subprocess.PIPE,stderr=subprocess.PIPE) - process.wait() - process = subprocess.Popen(['git', 'push', '-u', 'origin','master'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + #make class directory + os.mkdir(cpath) + #make default files for each class + for filename in c['default_file']: + f=open(cpath+"/"+filename, "w") + f.close() + + #make assignments directory + for a in c['assignments']: + path = cpath + "/" + a['name'] + print(path) + if(os.path.exists(path)): + print(path + " already exists...") + else: + os.mkdir(path) + f=open(path + "/README.md", "w") + f.close() + + #push to remote repo + os.chdir(cpath) + process = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + process.wait() + process = subprocess.Popen(['git', 'add', '.'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + process.wait() + process = subprocess.Popen(['git', 'commit', '-m', "Hello Class!"], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + process.wait() + #git remote add origin git@github.com:alexpchin/.git + process = subprocess.Popen(['git', 'remote', 'add', "origin", "git@github.com:" + self.git + "/" + cname + ".git"], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + process.wait() + process = subprocess.Popen(['git', 'push', '-u', 'origin','master'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) + process.wait() + + #update API and Github + def update(self): + data = { + "url": self.url, + "first_name": self.first_name, + "last_name": self.first_name, + "classes": self.classes, + "git": self.git, + "ion_user": self.username + }, + r = requests.put(url = self.url, data= data, headers={'Content-type': 'application/json'} ,auth=('raffukhondaker','hackgroup1')) + + def command(self, command): + ar = [] + command = command.split(" ") + for c in command: + ar.append(c) + process = subprocess.Popen(ar, stdout=subprocess.PIPE,stderr=subprocess.PIPE) process.wait() - elif(r.status_code == 404): - print("Make new account!") - elif(r.status_code == 403): - print("Invalid username/password") - else: - print(r.status_code) -def addClass(Name) + #class name format: _ + + #turn existing directory into class + def addClas(self, path): + cname = path.split("/") + if(os.path.exists(cname)): + print("Already synced to: " + str(self.username)) + return + + + #make a new class from scratch + def makeClass(self, subject): + cname = subject + "_" + self.username + os.chdir(self.username) + #check if class exists + if(os.path.exists(cname)): + print("Already synced to: " + str(self.username)) + return + else: + os.mkdir(cname) + f=open(cname + "/README.md", "w") + f.close() + #push to remote repo + os.chdir(cname) + command(self, 'git init') + command(self, 'git add .') + command(self, 'git commit -m "Hello Class!"') + #git remote add origin git@github.com:alexpchin/.git + command(self, 'git remote add origin git@github.com:'+ self.git + "/" + cname + ".git") + command(self, 'git push -u origin master') + + cinfo=[ + { + "name":subject, + "assignments":[], + "repo": "https://github.com:" + self.git + "/" + cname + ".git", + "default_file": [ + { + "name":"README.md" + } + ] + } + ] + + #update rest API + self.classes.append(cinfo) + update(self) + + def addAsignment(self, class_name, name): + os.chdir(self.username+"/") #make student repo by student id def addStudent(stid, teacher): @@ -70,12 +198,6 @@ def addStudent(stid, teacher): process = subprocess.Popen(['git', 'commit', '-m', "First Commit"], stdout=subprocess.PIPE,stderr=subprocess.PIPE) process.communicate() -def addStudents(filename): - print(filename) - -def addAsignment(name): - print(name) - def updateAssignment(name): print(name) diff --git a/Website/api/migrations/0004_auto_20200608_0220.py b/Website/api/migrations/0004_auto_20200608_0220.py new file mode 100644 index 0000000..948874d --- /dev/null +++ b/Website/api/migrations/0004_auto_20200608_0220.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-06-08 02:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0003_auto_20200607_1519'), + ] + + operations = [ + migrations.AlterField( + model_name='teacher', + name='git', + field=models.CharField(default='', max_length=100), + ), + ] diff --git a/Website/api/migrations/0005_auto_20200608_0222.py b/Website/api/migrations/0005_auto_20200608_0222.py new file mode 100644 index 0000000..7d4ac8f --- /dev/null +++ b/Website/api/migrations/0005_auto_20200608_0222.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-06-08 02:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0004_auto_20200608_0220'), + ] + + operations = [ + migrations.AlterField( + model_name='teacher', + name='git', + field=models.CharField(max_length=100), + ), + ] diff --git a/Website/api/migrations/0006_auto_20200608_0223.py b/Website/api/migrations/0006_auto_20200608_0223.py new file mode 100644 index 0000000..e0f85d3 --- /dev/null +++ b/Website/api/migrations/0006_auto_20200608_0223.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-06-08 02:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0005_auto_20200608_0222'), + ] + + operations = [ + migrations.AlterField( + model_name='student', + name='git', + field=models.CharField(max_length=100), + ), + ] diff --git a/Website/api/migrations/0007_classes_default_file.py b/Website/api/migrations/0007_classes_default_file.py new file mode 100644 index 0000000..ef9e018 --- /dev/null +++ b/Website/api/migrations/0007_classes_default_file.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.7 on 2020-06-08 03:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0006_auto_20200608_0223'), + ] + + operations = [ + migrations.AddField( + model_name='classes', + name='default_file', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='api.DefFiles'), + ), + ] diff --git a/Website/api/migrations/0008_auto_20200608_0341.py b/Website/api/migrations/0008_auto_20200608_0341.py new file mode 100644 index 0000000..f57236c --- /dev/null +++ b/Website/api/migrations/0008_auto_20200608_0341.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.7 on 2020-06-08 03:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0007_classes_default_file'), + ] + + operations = [ + migrations.RemoveField( + model_name='classes', + name='default_file', + ), + migrations.AddField( + model_name='classes', + name='default_file', + field=models.ManyToManyField(to='api.DefFiles'), + ), + ] diff --git a/Website/api/models.py b/Website/api/models.py index 0b139e6..0f3bde3 100644 --- a/Website/api/models.py +++ b/Website/api/models.py @@ -14,6 +14,7 @@ class Classes(models.Model): name = models.CharField(max_length=100) assignments = models.ManyToManyField(Assignment, default="") repo=models.URLField(default="") + default_file = models.ManyToManyField(DefFiles) def save(self, *args, **kwargs): return super(Classes, self).save(*args, **kwargs) @@ -23,7 +24,7 @@ class Teacher(models.Model): last_name = models.CharField(max_length=100) classes = models.ManyToManyField(Classes, default="") ion_user=models.CharField(primary_key=True, max_length=100) - git = models.URLField(default="") + git=models.CharField(max_length=100) class Student(models.Model): created = models.DateTimeField(auto_now_add=True) @@ -33,7 +34,7 @@ class Student(models.Model): ion_user=models.CharField(primary_key=True, max_length=100) webmail = models.EmailField(blank=True) grade = models.IntegerField() - git = models.URLField() + git=models.CharField(max_length=100) classes = models.ManyToManyField(Classes, default="") repo = models.URLField(default="") diff --git a/Website/api/serializers.py b/Website/api/serializers.py index 5eb1c63..0a48ca6 100644 --- a/Website/api/serializers.py +++ b/Website/api/serializers.py @@ -19,7 +19,7 @@ class ClassesSerializer(serializers.HyperlinkedModelSerializer): assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True) class Meta: model = Classes - fields = ['url', 'name','assignments', 'repo'] + fields = ['url', 'name','assignments', 'repo',"default_file"] class StudentSerializer(serializers.HyperlinkedModelSerializer): classes = ClassesSerializer(many=True, read_only=True,allow_null=True)