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
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/<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()
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
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)

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)
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="")

View File

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