This commit is contained in:
Raffu Khondaker 2020-06-09 17:41:31 -04:00
parent 266ed360f1
commit fc88e6c4c9

View File

@ -6,6 +6,7 @@ import pprint
import json import json
import shutil import shutil
import time import time
import prompt_toolkit.clipboard.pyperclip
#git clone student directory ==> <student-id>/classes/assignments #git clone student directory ==> <student-id>/classes/assignments
@ -56,6 +57,7 @@ def command(command):
output = process.communicate()[1] output = process.communicate()[1]
#print(output.decode('utf-8')) #print(output.decode('utf-8'))
#public methods: deleteClass, makeClass, update
class Teacher: class Teacher:
def __init__(self, data): def __init__(self, data):
# teacher info already stored in API # teacher info already stored in API
@ -99,15 +101,26 @@ class Teacher:
except: except:
pass pass
#list of classes that have been deleted (not with deleteClass) #list of classes that have been deleted (not with deleteClass)
extra = self.classes extra = []
for c in self.classes:
extra.append(c)
for i in range(len(extra)):
e = extra[i]['path']
extra[i] = e
print("Extra: "+str(extra))
print("Local:" + str(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
ind=0 extra.remove(path)
print("Current classes: " + path)
for e in extra:
self.deleteClass(e)
for i in range(len(classes)): for i in range(len(classes)):
print(extra)
c = classes[i] c = classes[i]
path = self.username + "/" + c path = self.username + "/" + c
#lists all assignments and default files #lists all assignments and default files
@ -124,16 +137,6 @@ class Teacher:
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>
@ -142,6 +145,9 @@ class Teacher:
def checkClass(self,path): def checkClass(self,path):
cname = path.split("/") cname = path.split("/")
cname = cname[len(cname)-1] cname = cname[len(cname)-1]
if(os.path.isfile(path)):
print(path + " must be in a Class directory.")
return False
if(("_" + self.username) in cname) == False: if(("_" + self.username) in cname) == False:
print("Incorrect class name: Must be in the format: " + self.username+ "/<course-name>_<ion_user>, not " + path) print("Incorrect class name: Must be in the format: " + self.username+ "/<course-name>_<ion_user>, not " + path)
return False return False
@ -183,92 +189,81 @@ class Teacher:
return False return False
#adds class to git, not API #adds class to git, not API
#Assuming valid class name
def addClasstoGit(self, path): def addClasstoGit(self, path):
cname = path.split("/") cname = path.split("/")
cname = cname[len(cname)-1] cname = cname[len(cname)-1]
#push to remote repo #push to remote repo
url='https://github.com/' + self.git + "/" + cname url='https://github.com/' + self.git + "/" + cname
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") try:
time.sleep(2) pyperclip.copy(cname)
webbrowser.open('https://github.com/new') spam = pyperclip.paste()
input("Repo created? (Press any key to continue)\n") print(cname + " copied to clipboard.")
except:
pass
time.sleep(2)
webbrowser.open('https://github.com/new')
input("Repo created? (Press any key to continue)\n")
print(url) print(url)
while(requests.get(url).status_code != 200): while(requests.get(url).status_code != 200):
print(requests.get(url)) 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:
else: cdir = os.getcwd()
cdir = os.getcwd() os.chdir(path)
os.chdir(path) print("Repo already exists. Cloning instead.")
print("Repo already exists. Cloning instead.") command('git clone')
command('git clone') command('git fetch origin')
command('git fetch origin') command('git pull')
command('git pull') command('git add .')
command('git add .') command('git commit -m Hello_Class')
command('git commit -m Hello_Class') command('git push -u origin master')
command('git push -u origin master') os.chdir(cdir)
os.chdir(cdir) print(cdir)
print(cdir) data={
data={ 'name':cname,
'name':cname, 'repo':url,
'repo':url, 'path':path,
'path':path, 'teacher':self.username
'teacher':self.username }
} return data
return data
return None
#make class from existing directory, add to git and api #make class from existing directory, add to git and api
def addClass(self, path): def addClass(self, path):
if (self.checkClass(path)): if (self.checkClass(path)):
data = self.addClasstoGit(path) data = self.addClasstoGit(path)
#add class to db #make class instance in db
data = postDB(data, 'http://127.0.0.1:8000/classes/') data = postDB(data, 'http://127.0.0.1:8000/classes/')
if(len(self.sclass)==0): #add to instance
classes = data['id'] #upate self.classes
else:
classes = self.sclass + "," + str(data['id'])
self.sclass=classes
self.classes.append(data) self.classes.append(data)
print(self.sclass) if(len(self.sclass)==0):
print(self.classes) self.sclass = data['id']
else:
self.sclass = self.sclass + "," + str(data['id'])
#update teacher instance in db, classes field
data={ data={
'first_name':self.first_name, 'first_name':self.first_name,
'last_name':self.last_name, 'last_name':self.last_name,
'git':self.git, 'git':self.git,
'ion_user':self.username, 'ion_user':self.username,
'url':self.url, 'url':self.url,
'classes':classes 'classes':self.sclass
} }
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
@ -317,14 +312,15 @@ class Teacher:
cid = str(c['id']) cid = str(c['id'])
repo = c['repo'] repo = c['repo']
print(cid) print(cid)
#remove from api
#remove from api
for i in range(len(self.classes)): for i in range(len(self.classes)):
if(self.classes[i]['id'] == int(cid)): if(self.classes[i]['id'] == int(cid)):
print("DELETE: " + self.classes[i]['name'])
del self.classes[i] del self.classes[i]
s="" s=""
for c in self.classes: for c in self.classes:
s = s + str(c['id']) + "," s = s + str(self.classes[i]['id']) + ","
print(s) print(s)
s = s[:-1] s = s[:-1]
print(s) print(s)
@ -337,7 +333,7 @@ class Teacher:
'classes':s 'classes':s
} }
print(putDB(data, self.url)) print(putDB(data, self.url))
delDB("http://127.0.0.1:8000/classes/" +cid + "/") delDB("http://127.0.0.1:8000/classes/" + cid + "/")
break break
#remove locally #remove locally
@ -355,7 +351,6 @@ class Teacher:
print(repo) print(repo)
while(requests.get(repo).status_code == 200): 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") 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'): if(r=="N" or r=="No" or r=='n'):
return None return None
@ -369,5 +364,5 @@ class Teacher:
data = getTeacher("eharris1") data = getTeacher("eharris1")
t = Teacher(data) t = Teacher(data)
t.deleteClass("eharris1/Crypto_eharris1")
t.update() t.update()