This commit is contained in:
Raffu Khondaker 2020-06-09 15:39:27 -04:00
parent 6b031bbd3f
commit 266ed360f1
11 changed files with 230 additions and 67 deletions

View File

@ -4,6 +4,8 @@ import requests
import webbrowser import webbrowser
import pprint import pprint
import json import json
import shutil
import time
#git clone student directory ==> <student-id>/classes/assignments #git clone student directory ==> <student-id>/classes/assignments
@ -26,14 +28,23 @@ def getTeacher(ion_user):
def getDB(url): def getDB(url):
r = requests.get(url = url, auth=('raffukhondaker','hackgroup1')) r = requests.get(url = url, auth=('raffukhondaker','hackgroup1'))
return r.json() print("GET:" + str(r.status_code))
return(r.json())
def postDB(data, url): def postDB(data, url):
r = requests.post(url = url, data=data, auth=('raffukhondaker','hackgroup1')) r = requests.post(url = url, data=data, auth=('raffukhondaker','hackgroup1'))
return(r.status_code) print("POST:" + str(r.status_code))
return(r.json())
def putDB(data, url): def putDB(data, url):
r = requests.put(url = url, data=data, auth=('raffukhondaker','hackgroup1')) r = requests.put(url = url, data=data, auth=('raffukhondaker','hackgroup1'))
return(r.status_code) print("PUT:" + str(r.status_code))
return(r.json())
def delDB(url):
r = requests.delete(url = url, auth=('raffukhondaker','hackgroup1'))
print("DELETE:" + str(r.status_code))
return None
def command(command): def command(command):
ar = [] ar = []
@ -42,7 +53,7 @@ def command(command):
ar.append(c) ar.append(c)
process = subprocess.Popen(ar, stdout=subprocess.PIPE,stderr=subprocess.PIPE) process = subprocess.Popen(ar, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p=process.poll() p=process.poll()
output = process.communicate()[0] output = process.communicate()[1]
#print(output.decode('utf-8')) #print(output.decode('utf-8'))
class Teacher: class Teacher:
@ -54,46 +65,75 @@ class Teacher:
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/teachers/" + self.username + "/" self.url= "http://127.0.0.1:8000/teachers/" + self.username + "/"
self.classes=data['classes'].split(" ") #classes in id form (Example: 4,5)
self.sclass=data['classes']
cid=data['classes'].split(",")
try:
cid.remove('')
except:
pass
try:
cid.remove("")
except:
pass
classes=[]
for c in cid:
url = "http://127.0.0.1:8000/classes/" + str(c) + "/"
classes.append(getDB(url))
self.classes = classes
self.sclass=str(data['classes'])
if(os.path.isdir(self.username)): if(os.path.isdir(self.username)):
print("Already synced to " + self.username) print("Synced to " + self.username)
else: else:
os.mkdir(self.username) os.mkdir(self.username)
def checkGit(self, ass):
for a in ass:
if a =='.git':
return True
return False
#update API and Github, all assignments / classes #update API and Github, all assignments / classes
def update(self): def update(self):
#lists all classes #lists all classes
ignore=['.git','.DS_Store']
classes = os.listdir(self.username) classes = os.listdir(self.username)
for i in ignore:
try:
classes.remove(i)
except:
pass
#list of classes that have been deleted (not with deleteClass)
extra = self.classes
#checks all class directories first #checks all class directories first
for c in classes: for c in classes:
path = self.username + "/" + c path = self.username + "/" + c
if(self.checkClass(path) == False): if(self.checkClass(path) == False):
return return
cdict = [] ind=0
for c in classes: for i in range(len(classes)):
print(extra)
c = classes[i]
path = self.username + "/" + c path = self.username + "/" + c
#lists all assignments and default files #lists all assignments and default files
ass = os.listdir(path)
#if no .git, directory not synced to git or API #if no .git, directory not synced to git or API
if (self.checkGit(ass)==False): if (self.checkInDB(path)==False):
data = self.addClasstoGit(path) self.addClass(path)
postDB(data, 'http://127.0.0.1:8000/classes/')
else: else:
#push to git #push to git
loc = os.getcwd() loc = os.getcwd()
os.chdir(path) os.chdir(path)
command('git fetch origin')
command('git pull origin master')
command('git add .') command('git add .')
command('git commit -m "Update"') command('git commit -m "Update"')
command('git push -u origin master') command('git push -u origin master')
os.chdir(loc) os.chdir(loc)
try:
extra.pop(ind)
ind=i-1
except:
pass
print(i)
print("EXTRA: " + str(extra))
ind=ind+1
for e in extra:
self.deleteClass(e['path'])
#class name format: <course-name>_<ion_user> #class name format: <course-name>_<ion_user>
@ -103,7 +143,7 @@ class Teacher:
cname = path.split("/") cname = path.split("/")
cname = cname[len(cname)-1] cname = cname[len(cname)-1]
if(("_" + self.username) in cname) == False: if(("_" + self.username) in cname) == False:
print("Incorrect class name: Must be in the format: <course-name>_<ion_user>") print("Incorrect class name: Must be in the format: " + self.username+ "/<course-name>_<ion_user>, not " + path)
return False return False
dirs = os.listdir(path) dirs = os.listdir(path)
#checks if there is a file (not within Assignments) in class, need at least 1 #checks if there is a file (not within Assignments) in class, need at least 1
@ -115,15 +155,15 @@ class Teacher:
for d in dirs: for d in dirs:
if(os.path.isfile(d)): if(os.path.isfile(d)):
deffile=True deffile=True
if(os.path.isdir(d)) and d != '.git': else:
#checks if there is a file in an Assignment, need at least 1 #checks if there is a file in an Assignment, need at least 1
as_file = False as_file = False
asdir = os.listdir(d) asdir = os.listdir(path + "/" + d)
for a in asdir: for a in asdir:
if(os.path.isfile(a)): if(os.path.isfile(path + "/" + d + "/" +a)):
as_file=True as_file=True
if(as_file==False): if(as_file==False):
as_bad = a as_bad = d
break break
if(as_file==False): if(as_file==False):
print("Assignment '" + as_bad + "' does not have a default file!") print("Assignment '" + as_bad + "' does not have a default file!")
@ -133,6 +173,14 @@ class Teacher:
print("Need a default file in the " + path + " Directory!") print("Need a default file in the " + path + " Directory!")
return False return False
return True return True
def checkInDB(self, path):
n = path.split("/")
n = n[len(n)-1]
for c in self.classes:
if(n == c['name']):
return True
return False
#adds class to git, not API #adds class to git, not API
def addClasstoGit(self, path): def addClasstoGit(self, path):
@ -143,6 +191,7 @@ class Teacher:
if(self.checkClass(path)): if(self.checkClass(path)):
if(requests.get(url).status_code != 200): if(requests.get(url).status_code != 200):
input("Make new Git Repo with name: " + cname + " (Press any key to continue)\n") input("Make new Git Repo with name: " + cname + " (Press any key to continue)\n")
time.sleep(2)
webbrowser.open('https://github.com/new') webbrowser.open('https://github.com/new')
input("Repo created? (Press any key to continue)\n") input("Repo created? (Press any key to continue)\n")
@ -152,14 +201,25 @@ class Teacher:
r = input("Repo not created yet. (Press any key to continue after repo created, or 'N' to exit)\n") r = input("Repo not created yet. (Press any key to continue after repo created, or 'N' to exit)\n")
if(r=="N" or r=="No"): if(r=="N" or r=="No"):
return None return None
cdir = os.getcwd() cdir = os.getcwd()
os.chdir(path) os.chdir(path)
command('git init') command('git init')
command('git add .') command('git add .')
command('git commit -m Hello_Class') command('git commit -m Hello_Class')
command('git remote add origin ' + url + '.git') command('git remote add origin ' + url + '.git')
command('git push -u origin master') command('git push -u origin master')
else:
cdir = os.getcwd()
os.chdir(path)
print("Repo already exists. Cloning instead.")
command('git clone')
command('git fetch origin')
command('git pull')
command('git add .')
command('git commit -m Hello_Class')
command('git push -u origin master')
os.chdir(cdir) os.chdir(cdir)
print(cdir)
data={ data={
'name':cname, 'name':cname,
'repo':url, 'repo':url,
@ -169,40 +229,21 @@ class Teacher:
return data return data
return None return None
#make class from existing directory, add to git and api
#make a new class from scratch def addClass(self, path):
#subject: string, assignments: list if (self.checkClass(path)):
#class name must be: <subject>_<ion_user>
def makeClass(self, cname, assignments):
cdir = os.getcwd()
os.chdir(self.username)
#check if class exists
if(os.path.exists(cname)):
print("Class already exists: " + cname)
return
else:
if((("_" + self.username) in cname) == False):
print("class name must be: "+ cname + "_" + self.username)
return
path = self.username + "/" + cname
os.mkdir(cname)
f=open(cname + "/README.md", "w")
f.close()
#push to remote repo
os.chdir(cname)
for a in assignments:
os.mkdir(a)
f=open(a + "/instructions.txt", "w")
f.close()
os.chdir(cdir)
data = self.addClasstoGit(path) data = self.addClasstoGit(path)
print(postDB(data, 'http://127.0.0.1:8000/classes/')) #add class to db
data = postDB(data, 'http://127.0.0.1:8000/classes/')
if(len(self.sclass)==0): if(len(self.sclass)==0):
classes = cname classes = data['id']
else: else:
classes = self.sclass + "," + cname classes = self.sclass + "," + str(data['id'])
self.sclass=classes
self.classes.append(data)
print(self.sclass)
print(self.classes)
data={ data={
'first_name':self.first_name, 'first_name':self.first_name,
'last_name':self.last_name, 'last_name':self.last_name,
@ -211,9 +252,114 @@ class Teacher:
'url':self.url, 'url':self.url,
'classes':classes 'classes':classes
} }
print(putDB(data, self.url)) print(putDB(data, self.url))
cid=getDB(self.url)['classes'].split(",")
try:
cid.remove('')
except:
pass
try:
cid.remove("")
except:
pass
classes=[]
for c in cid:
url = "http://127.0.0.1:8000/classes/" + str(c) + "/"
classes.append(getDB(url))
self.classes = classes
return data return data
#make a new class from scratch
#subject: string, assignments: list
#class name must be: <subject>_<ion_user>
def makeClass(self, cname, assignments):
#check if class exists
path = self.username + "/" + cname
if(os.path.exists(path)):
print("Class already exists: " + cname)
return
else:
if((("_" + self.username) in cname) == False):
print("class name must be: "+ cname + "_" + self.username)
return
cdir = os.getcwd()
os.mkdir(path)
f=open(path + "/README.md", "w")
f.close()
#push to remote repo
os.chdir(path)
for a in assignments:
os.mkdir(a)
f=open(a + "/instructions.txt", "w")
f.close()
os.chdir(cdir)
data = self.addClass(path)
return data
def deleteClass(self, path):
if(os.path.exists(path) == False):
print(path + " does not exist locally.")
resp = input("Do you want to delete " + path + " from the SkoolOS system? (y/N) ")
if(resp != 'y'):
return
cname = path.split("/")
cname = cname[len(cname)-1]
cid = None
repo = ''
for c in self.classes:
if cname == c['name']:
cid = str(c['id'])
repo = c['repo']
print(cid)
#remove from api
for i in range(len(self.classes)):
if(self.classes[i]['id'] == int(cid)):
del self.classes[i]
s=""
for c in self.classes:
s = s + str(c['id']) + ","
print(s)
s = s[:-1]
print(s)
data={
'first_name':self.first_name,
'last_name':self.last_name,
'git':self.git,
'ion_user':self.username,
'url':self.url,
'classes':s
}
print(putDB(data, self.url))
delDB("http://127.0.0.1:8000/classes/" +cid + "/")
break
#remove locally
try:
shutil.rmtree(path)
except:
pass
#remove from git
input("Delete repository: " + cname + ". Scroll to the bottom of the page and press 'Delete this repository' (Press any key to continue) ")
print(repo)
time.sleep(2)
webbrowser.open(repo + "/settings")
input("Repo deleted? (Press any key to continue) ")
print(repo)
while(requests.get(repo).status_code == 200):
print(requests.get(repo))
r = input("Repo still no deleted yet. (Press any key to continue after repo deleted, or 'N' to exit)\n")
if(r=="N" or r=="No" or r=='n'):
return None
#make student repo by student id #make student repo by student id
def addStudent(self,stid): def addStudent(self,stid):
print(stid) print(stid)
@ -223,6 +369,5 @@ class Teacher:
data = getTeacher("eharris1") data = getTeacher("eharris1")
t = Teacher(data) t = Teacher(data)
t.makeClass('Math4_eharris1', ['Week1_HW', 'Week2_HW']) t.update()

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-09 03:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='teacher',
name='classes',
field=models.CharField(blank=True, default='', max_length=100),
),
]

View File

@ -36,7 +36,7 @@ class Teacher(models.Model):
first_name = models.CharField(max_length=100) first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
# classes = models.ManyToManyField(Classes, default="") # classes = models.ManyToManyField(Classes, default="")
classes=models.CharField(max_length=100, default="") classes=models.CharField(max_length=100, default="", blank=True)
ion_user=models.CharField(primary_key=True, max_length=100) ion_user=models.CharField(primary_key=True, max_length=100)
git=models.CharField(max_length=100) git=models.CharField(max_length=100)