git update

This commit is contained in:
Raffu Khondaker 2020-06-08 19:15:33 -04:00
parent 8060fad3ec
commit c50c305b86
10 changed files with 190 additions and 28 deletions

View File

@ -45,7 +45,7 @@ import json
}, },
''' '''
#get teacher info from api #get teacher info from api
def getData(ion_user): def getTeacher(ion_user):
URL = "http://127.0.0.1:8000/teachers/" + ion_user + "/" URL = "http://127.0.0.1:8000/teachers/" + ion_user + "/"
r = requests.get(url = URL, auth=('raffukhondaker','hackgroup1')) r = requests.get(url = URL, auth=('raffukhondaker','hackgroup1'))
if(r.status_code == 200): if(r.status_code == 200):
@ -81,7 +81,7 @@ class Teacher:
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()[0]
print(output.decode('utf-8')) #print(output.decode('utf-8'))
def initTeacher(self): def initTeacher(self):
if(os.path.exists(self.username )): if(os.path.exists(self.username )):
@ -139,6 +139,26 @@ class Teacher:
return True return True
return False return False
def compareDB(self, fdict, url):
URL = url
r = requests.get(url = URL, auth=('raffukhondaker','hackgroup1'))
data = r.json()['results']
allfiles = data
far = []
for f in allfiles:
far.append(f['path'])
for f in fdict:
in_db = False
for dbf in far:
if(dbf == f['path']):
in_db=True
break
if(in_db==False):
r = requests.post(url = URL, data=f , auth=('raffukhondaker','hackgroup1'))
print("POST: (" + url + "): " + f['path'])
print(r.status_code)
#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
@ -148,7 +168,6 @@ class Teacher:
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):
print(path)
return return
cdict = [] cdict = []
for c in classes: for c in classes:
@ -157,20 +176,21 @@ class Teacher:
ass = os.listdir(path) 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.checkGit(ass)==False):
print(path)
self.addClass(path) self.addClass(path)
else: else:
#push to git #push to git
loc = os.getcwd() loc = os.getcwd()
os.chdir(path) os.chdir(path)
print(path)
print(ass)
self.command('git add .') self.command('git add .')
self.command('git commit -m "Update"') self.command('git commit -m "Update"')
self.command('git push -u origin master') self.command('git push -u origin master')
os.chdir(loc) os.chdir(loc)
ass.remove('.git') ass.remove('.git')
print(ass)
loc = os.getcwd()
os.chdir(path)
repo = self.command('git config --get remote.origin.url')
os.chdir(loc)
#assignments #assignments
adict = [] adict = []
@ -186,40 +206,53 @@ class Teacher:
#check for default file #check for default file
if(os.path.isfile(path)): if(os.path.isfile(path)):
fdict.append({ fdict.append({
'name':a 'name':a,
'path':path
}) })
elif(os.path.isdir(path)): elif(os.path.isdir(path)):
for af in os.listdir(path): for af in os.listdir(path):
path = path+ "/" + af path = path+ "/" + af
if(os.path.isfile(path)): if(os.path.isfile(path)):
afdict.append({ afdict.append({
'name':af 'name':af,
'path':path
}) })
path = self.username + "/" + c + "/" + a
adict.append({ adict.append({
'name':aname, 'name':aname,
'due_date': due_date[0], 'due_date': due_date[0],
'path':path,
'files':afdict 'files':afdict
}) })
self.compareDB(fdict, "http://127.0.0.1:8000/files/")
self.compareDB(afdict,"http://127.0.0.1:8000/files/")
self.compareDB(adict, 'http://127.0.0.1:8000/files/')
path = self.username + "/" + c
print(fdict)
cdict.append({ cdict.append({
'name':c, 'name':c,
'repo': 'https://github.com:' + self.git + "/" + c + ".git", 'repo': repo,
'path':path,
'assignments':adict, 'assignments':adict,
'default_file':fdict 'default_file':fdict,
}) })
self.compareDB(cdict,'http://127.0.0.1:8000/classes/')
mdict= { mdict= {
'first_name':self.first_name, 'first_name':self.first_name,
'last_name':self.last_name, 'last_name':self.last_name,
'classes':cdict,
'git':self.git, 'git':self.git,
'ion_user':self.username 'ion_user':self.username,
'classes':cdict,
} }
data = json.dumps(mdict)
print(pprint.pprint(mdict))
r = requests.put(url = self.url, data= data, headers={'Content-type': 'application/json'} ,auth=('raffukhondaker','hackgroup1')) data = json.dumps(mdict)
print(pprint.pprint(r.json())) # r = requests.put(url = 'http://127.0.0.1:8000/teachers/eharris1/', data=data, headers={'Content-type': 'application/json'} , auth=('raffukhondaker','hackgroup1'))
# print(print(r.json()))
#class name format: <course-name>_<ion_user> #class name format: <course-name>_<ion_user>
@ -261,7 +294,7 @@ class Teacher:
return True return True
#adds class to git, not API #adds class to git, not API
def addClass(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
@ -276,7 +309,7 @@ class Teacher:
print(requests.get(url)) 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 return None
cdir = os.getcwd() cdir = os.getcwd()
os.chdir(path) os.chdir(path)
self.command('git init') self.command('git init')
@ -287,7 +320,6 @@ class Teacher:
os.chdir(cdir) os.chdir(cdir)
#make a new class from scratch #make a new class from scratch
def makeClass(self, subject): def makeClass(self, subject):
cname = subject + "_" + self.username cname = subject + "_" + self.username
@ -342,7 +374,8 @@ class Teacher:
def comment(self): def comment(self):
print("heheheh") print("heheheh")
data = getData("eharris1") # data = getData("eharris1")
data={'url': 'http://127.0.0.1:8000/teachers/eharris1/', 'first_name': 'Errin', 'last_name': 'Harris', 'git': 'therealraffi', 'ion_user': 'eharris1', 'classes': [{'url': 'http://127.0.0.1:8000/classes/1/', 'name': 'Math5_eharris1', 'repo': 'http://127.0.0.1:8000/assignments/3/', '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'}]}], 'default_file': [{'name': 'instructions.txt'}]}]}
t = Teacher(data) t = Teacher(data)
t.update() t.update()

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 18:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0010_auto_20200608_0510'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='files',
field=models.ManyToManyField(to='api.DefFiles'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 21:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0011_auto_20200608_1853'),
]
operations = [
migrations.AddField(
model_name='deffiles',
name='path',
field=models.FilePathField(default=''),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 21:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0012_deffiles_path'),
]
operations = [
migrations.AlterField(
model_name='deffiles',
name='path',
field=models.CharField(default='', max_length=100),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 21:22
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0013_auto_20200608_2117'),
]
operations = [
migrations.AlterField(
model_name='deffiles',
name='path',
field=models.CharField(max_length=100),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 22:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0014_auto_20200608_2122'),
]
operations = [
migrations.AddField(
model_name='assignment',
name='path',
field=models.CharField(default='', max_length=100),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 22:03
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0015_assignment_path'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='path',
field=models.CharField(max_length=100),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-08 22:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0016_auto_20200608_2203'),
]
operations = [
migrations.AddField(
model_name='classes',
name='path',
field=models.CharField(default='', max_length=100),
),
]

View File

@ -2,18 +2,21 @@ from django.db import models
class DefFiles(models.Model): class DefFiles(models.Model):
name=models.CharField(max_length=100) name=models.CharField(max_length=100)
path=models.CharField(max_length=100)
class Assignment(models.Model): class Assignment(models.Model):
name=models.CharField(max_length=100) name=models.CharField(max_length=100)
due_date=models.DateTimeField() due_date=models.DateTimeField()
files = models.ManyToManyField(DefFiles, default="") files = models.ManyToManyField(DefFiles)
path=models.CharField(max_length=100)
def __str__(self): def __str__(self):
return '%s' % (self.name) return '%s' % (self.name)
class Classes(models.Model): class Classes(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
assignments = models.ManyToManyField(Assignment, default="")
repo=models.URLField(default="") repo=models.URLField(default="")
path=models.CharField(max_length=100, default="")
assignments = models.ManyToManyField(Assignment, default="")
default_file = models.ManyToManyField(DefFiles) 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)

View File

@ -6,32 +6,32 @@ class DefFilesSerializer(serializers.HyperlinkedModelSerializer):
permissions_classes = [permissions.IsAuthenticatedOrReadOnly] permissions_classes = [permissions.IsAuthenticatedOrReadOnly]
class Meta: class Meta:
model = DefFiles model = DefFiles
fields = ['name'] fields = ['name', 'path','url']
class AssignmentSerializer(serializers.HyperlinkedModelSerializer): class AssignmentSerializer(serializers.HyperlinkedModelSerializer):
permissions_classes = [permissions.IsAuthenticatedOrReadOnly] permissions_classes = [permissions.IsAuthenticatedOrReadOnly]
files = DefFilesSerializer(many=True, read_only=True,allow_null=True) files = DefFilesSerializer(many=True, read_only=True,allow_null=True)
class Meta: class Meta:
model = Assignment model = Assignment
fields = ['name', 'due_date', 'url', 'files'] fields = ['name', 'due_date', 'url', 'path' ,'files']
class ClassesSerializer(serializers.HyperlinkedModelSerializer): class ClassesSerializer(serializers.HyperlinkedModelSerializer):
assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True) assignments = AssignmentSerializer(many=True, read_only=True,allow_null=True)
default_file=DefFilesSerializer(many=True, read_only=True,allow_null=True) default_file=DefFilesSerializer(many=True, read_only=True,allow_null=True)
class Meta: class Meta:
model = Classes model = Classes
fields = ['url', 'name','assignments', 'repo',"default_file"] fields = ['url', 'name', 'repo','path', 'assignments',"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)
class Meta: class Meta:
model = Student model = Student
fields = ['url', 'first_name', 'last_name', 'grade','webmail','student_id','classes', 'git','repo','ion_user'] fields = ['url', 'first_name', 'last_name', 'grade','webmail','student_id', 'git','repo','ion_user','classes']
class TeacherSerializer(serializers.ModelSerializer): class TeacherSerializer(serializers.ModelSerializer):
classes = ClassesSerializer(many=True, read_only=True,allow_null=True) classes = ClassesSerializer(many=True, read_only=True,allow_null=True)
class Meta: class Meta:
model = Teacher model = Teacher
fields = ['url', 'first_name', 'last_name', 'classes','git','ion_user'] fields = ['url', 'first_name', 'last_name','git','ion_user','classes' ]