teacher methods

This commit is contained in:
Raffu Khondaker 2020-06-08 00:18:46 -04:00
parent c39a012fed
commit ddea6db3ff
8 changed files with 271 additions and 53 deletions

View File

@ -4,60 +4,188 @@ import requests
#git clone student directory ==> <student-id>/classes/assignments #git clone student directory ==> <student-id>/classes/assignments
def initTeacher(ion_user): '''
#check if git has already been initialized {
if(os.path.exists(str(ion_user) +"/" + ".git")): "url": "http://127.0.0.1:8000/teachers/eharris1/",
print("Already synced to: " + str(ion_user)) "first_name": "Errin",
return "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 class Teacher:
URL = "http://127.0.0.1:8000/students/" + ion_user + "/" def __init__(self, data):
r = requests.get(url = URL, auth=('student','_$YFE#34.9_37jr')) # teacher info already stored in API
if(r.status_code == 200): # intitialze fields after GET request
data = r.json() self.first_name=data['first_name']
repo = data['repo'] self.last_name=data['last_name']
classes = data['classes'] self.classes=data['classes']
print(data) self.git=data['git']
#git clone repo self.username=data['ion_user']
process = subprocess.Popen(['git', 'clone', repo], stdout=subprocess.PIPE,stderr=subprocess.PIPE) self.url= "http://127.0.0.1:8000/teachers/" + self.username + "/"
process.wait() 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 # make classes directory
for c in classes: for c in classes:
cpath = str(ion_user) + "/" + c['name'] cname= c['name'] + "_" + self.username
cpath = self.username + "/" + cname
if(os.path.exists(cpath)): if(os.path.exists(cpath)):
print(cpath + " already exists...") print(cpath + " already exists...")
else: else:
os.mkdir(str(ion_user) + "/" + c['name']) #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 #make assignments directory
for a in c['assignments']: for a in c['assignments']:
path = str(ion_user) + "/" + c['name'] + "/" + a['name'] path = cpath + "/" + a['name']
print(path) print(path)
if(os.path.exists("/" +path)): if(os.path.exists(path)):
print(path + " already exists...") print(path + " already exists...")
else: else:
os.mkdir(str(ion_user) + "/" + c['name'] + "/" + a['name']) os.mkdir(path)
f=open(path + "/README.md", "w")
f.close()
#push to remote repo #push to remote repo
os.chdir(ion_user) os.chdir(cpath)
process = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) process = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
process.wait() process.wait()
process = subprocess.Popen(['git', 'add', '.'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) process = subprocess.Popen(['git', 'add', '.'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
process.wait() process.wait()
process = subprocess.Popen(['git', 'commit', '-m', "First Commit"], stdout=subprocess.PIPE,stderr=subprocess.PIPE) process = subprocess.Popen(['git', 'commit', '-m', "Hello Class!"], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
process.wait() process.wait()
process = subprocess.Popen(['git', 'push', '-u', 'origin','master'], stdout=subprocess.PIPE,stderr=subprocess.PIPE) #git remote add origin git@github.com:alexpchin/<reponame>.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() 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: <course-name>_<ion_user>
#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/<reponame>.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 #make student repo by student id
def addStudent(stid, teacher): 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 = subprocess.Popen(['git', 'commit', '-m', "First Commit"], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
process.communicate() process.communicate()
def addStudents(filename):
print(filename)
def addAsignment(name):
print(name)
def updateAssignment(name): def updateAssignment(name):
print(name) print(name)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,6 +14,7 @@ class Classes(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
assignments = models.ManyToManyField(Assignment, default="") assignments = models.ManyToManyField(Assignment, default="")
repo=models.URLField(default="") repo=models.URLField(default="")
default_file = models.ManyToManyField(DefFiles)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
return super(Classes, self).save(*args, **kwargs) return super(Classes, self).save(*args, **kwargs)
@ -23,7 +24,7 @@ class Teacher(models.Model):
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
classes = models.ManyToManyField(Classes, default="") classes = models.ManyToManyField(Classes, default="")
ion_user=models.CharField(primary_key=True, max_length=100) ion_user=models.CharField(primary_key=True, max_length=100)
git = models.URLField(default="") git=models.CharField(max_length=100)
class Student(models.Model): class Student(models.Model):
created = models.DateTimeField(auto_now_add=True) 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) ion_user=models.CharField(primary_key=True, max_length=100)
webmail = models.EmailField(blank=True) webmail = models.EmailField(blank=True)
grade = models.IntegerField() grade = models.IntegerField()
git = models.URLField() git=models.CharField(max_length=100)
classes = models.ManyToManyField(Classes, default="") classes = models.ManyToManyField(Classes, default="")
repo = models.URLField(default="") repo = models.URLField(default="")

View File

@ -19,7 +19,7 @@ class ClassesSerializer(serializers.HyperlinkedModelSerializer):
assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True) assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True)
class Meta: class Meta:
model = Classes model = Classes
fields = ['url', 'name','assignments', 'repo'] fields = ['url', 'name','assignments', 'repo',"default_file"]
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)