From c39a012fed181418b9e5104287bf021823a4f12c Mon Sep 17 00:00:00 2001 From: Raffu Khondaker <2022rkhondak@tjhsst.edu> Date: Sun, 7 Jun 2020 18:24:15 -0400 Subject: [PATCH] git updates --- CLI/run.py | 148 ------------------ CLI/s-git.py | 30 ++-- CLI/t-git.py | 74 +++++---- Website/api/maker.py | 1 - .../api/migrations/0003_auto_20200607_1519.py | 23 +++ Website/api/models.py | 1 + Website/api/serializers.py | 2 +- Website/api/views-back.py | 131 ++++++++++++++++ Website/api/views.py | 4 +- 9 files changed, 223 insertions(+), 191 deletions(-) delete mode 100644 CLI/run.py create mode 100644 Website/api/migrations/0003_auto_20200607_1519.py create mode 100644 Website/api/views-back.py diff --git a/CLI/run.py b/CLI/run.py deleted file mode 100644 index 6a51e78..0000000 --- a/CLI/run.py +++ /dev/null @@ -1,148 +0,0 @@ -from __future__ import print_function, unicode_literals -from PyInquirer import prompt, print_json -import argparse -import json -import os -import argparse - -def yesorno(question): - questions = [ - { - 'type': 'input', - 'name': 'response', - 'message': question, - }, - ] - answers = prompt(questions) - if(answers["response"] == "y"): - return True - return False - -#already ccrerrated account through website, has to login -def login(): - #enter username - #enter password - questions = [ - { - 'type': 'input', - 'name': 'webmail', - 'message': 'What\'s TJ Webmail', - }, - { - 'type': 'password', - 'name': 'password', - 'message': 'Password?', - }, - ] - user = prompt(questions) - #reading from json of users (replace w GET to database) to check if user is registered - with open('users.json', 'r') as json_file: - data = json.load(json_file) - for i in range(len(data)): - if user["webmail"] == data[i]["webmail"]: - if(user["password"] == data[i]["password"]): - print("Logged in!") - return data[i] - else: - print("Password incorrect. Try again.") - return None - print("User not found. Please Try again") - return None - -#did not create account through website, has to signup/login -def update(): - questions = [ - { - 'type': 'input', - 'name': 'first-name', - 'message': 'What\'s your first name', - }, - { - 'type': 'input', - 'name': 'last-name', - 'message': 'What\'s your last name?', - }, - { - 'type': 'list', - 'name': 'grade', - 'message': 'Grade?', - 'choices':["9","10","11","12"] - }, - { - 'type': 'input', - 'name': 'webmail', - 'message': 'What\'s your TJ Webmail?', - }, - { - 'type': 'password', - 'name': 'password', - 'message': 'Password?', - }, - ] - user = prompt(questions) - for i in user: - if user[i] == "": - print("Some forms were left blank. Try again.\n") - return None - if len(user["password"]) < 6: - print("Password is too short. Try again.") - return None - if (("@tjhsst.edu" in user['webmail']) == False): - print("Webmail entered was not a @tjhhsst.edu. Try again.") - return None - - #post user to json, replace w POST to db - user["classes"] = [] - with open('users.json', 'r') as json_file: - data = json.load(json_file) - data.append(user) - open("users.json", "w").write(str(json.dumps(data))) - return user - -def relogin(): - questions = [ - { - 'type': 'list', - 'name': 'choice', - 'message': '', - 'choices':["Continue as current user","Login into new user","Sign up into new account"] - }, - ] - answer = prompt(questions) - - -def setup(user): - #Read classes/assignenments and setup directory: - #SkoolOS/Math/Week1 - for c in user["classes"]: - os.makedirs("../" + c) - for a in user["classes"][c]: - os.makedirs("../" + c + "/" + a) - -def start(): - if(os.path.exists(".login.txt") == False): - b = yesorno("Do you have a SkoolOS account?(y/N)") - if(b): - user = login() - if(user != None): - setup(user) - open(".login.txt", "w").write(str(user)) - else: - user = update() - if(user != None): - open(".login.txt").write(str(user)) - -my_parser = argparse.ArgumentParser(prog='skool', description='Let SkoolOS control your system', epilog="Try again") -my_parser.add_argument('--init', action="store_true") #returns true if run argument -args = my_parser.parse_args() - -update() -outputs = vars(args) -if(outputs['init']): - start() - - - - - - diff --git a/CLI/s-git.py b/CLI/s-git.py index 34c7ef1..1203091 100644 --- a/CLI/s-git.py +++ b/CLI/s-git.py @@ -1,7 +1,8 @@ import subprocess import os import requests - +import pprint +import json #git clone student directory ==> <student-id>/classes/assignments def initStudent(ion_user): @@ -57,6 +58,8 @@ def initStudent(ion_user): else: print(r.status_code) + + #Teachers #make student repo by student id @@ -82,14 +85,17 @@ def updateAssignment(name): def comment(filename, text): print(text) -initStudent("2022rkhondak") - -os.chdir("2022rkhondak") -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) -process.wait() \ No newline at end of file +#initStudent("2022rkhondak") +ion_user = "2022rkhondak" +headers = {'Content-type': 'application/json'} +data = {'first_name': 'Jeff', + 'git': 'https://github.com/', + 'grade': 10, + 'ion_user': '2022jlol1', + 'last_name': 'lol.', + 'student_id': 11111 + } +data = json.dumps(data) +URL = "http://127.0.0.1:8000/students/" + ion_user + "/" +r = requests.put(url = URL, data= data, headers=headers ,auth=('raffukhondaker','hackgroup1')) +pprint.pprint(r.json()) \ No newline at end of file diff --git a/CLI/t-git.py b/CLI/t-git.py index 1da2d06..1241390 100644 --- a/CLI/t-git.py +++ b/CLI/t-git.py @@ -4,41 +4,60 @@ import requests #git clone student directory ==> <student-id>/classes/assignments -def initTeacher(student_id): +def initTeacher(ion_user): #check if git has already been initialized - if(os.path.exists(str(student_id) +"/" + ".git")): - print("Already synced to: " + str(student_id)) + if(os.path.exists(str(ion_user) +"/" + ".git")): + print("Already synced to: " + str(ion_user)) return #get student repo from API - URL = "http://127.0.0.1:8000/students/" + str(student_id) + "/" + URL = "http://127.0.0.1:8000/students/" + ion_user + "/" r = requests.get(url = URL, auth=('student','_$YFE#34.9_37jr')) - data = r.json() - repo = data['repo'] - classes = data['classes'] - print(classes) - #git clone repo - process = subprocess.Popen(['git', 'clone', repo], stdout=subprocess.PIPE,stderr=subprocess.PIPE) - process.communicate() + 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() - #make classes directory - for c in classes: - cpath = str(student_id) + "/" + c['name'] - if(os.path.exists(cpath)): - print(cpath + " already exists...") - else: - os.mkdir(str(student_id) + "/" + c['name']) - - #make assignments directory - for a in c['assignments']: - path = str(student_id) + "/" + c['name'] + "/" + a['name'] - print(path) - if(os.path.exists("/" +path)): - print(path + " already exists...") + # make classes directory + for c in classes: + cpath = str(ion_user) + "/" + c['name'] + if(os.path.exists(cpath)): + print(cpath + " already exists...") else: - os.mkdir(str(student_id) + "/" + c['name'] + "/" + a['name']) + 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) + process.wait() -#Teachers + 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) #make student repo by student id def addStudent(stid, teacher): @@ -64,4 +83,3 @@ def comment(filename, text): print(text) -initStudent(1579460) \ No newline at end of file diff --git a/Website/api/maker.py b/Website/api/maker.py index f960dba..dd31bb6 100644 --- a/Website/api/maker.py +++ b/Website/api/maker.py @@ -1,4 +1,3 @@ -from api.models import Assignment, Student, Classes, Teacher, DefFiles from datetime import datetime f1 = DefFiles( diff --git a/Website/api/migrations/0003_auto_20200607_1519.py b/Website/api/migrations/0003_auto_20200607_1519.py new file mode 100644 index 0000000..7bf2210 --- /dev/null +++ b/Website/api/migrations/0003_auto_20200607_1519.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.7 on 2020-06-07 15:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0002_auto_20200607_0751'), + ] + + operations = [ + migrations.AddField( + model_name='classes', + name='repo', + field=models.URLField(default=''), + ), + migrations.AlterField( + model_name='assignment', + name='files', + field=models.ManyToManyField(default='', to='api.DefFiles'), + ), + ] diff --git a/Website/api/models.py b/Website/api/models.py index cf31bb6..0b139e6 100644 --- a/Website/api/models.py +++ b/Website/api/models.py @@ -13,6 +13,7 @@ class Assignment(models.Model): class Classes(models.Model): name = models.CharField(max_length=100) assignments = models.ManyToManyField(Assignment, default="") + repo=models.URLField(default="") def save(self, *args, **kwargs): return super(Classes, self).save(*args, **kwargs) diff --git a/Website/api/serializers.py b/Website/api/serializers.py index 46d09cc..5eb1c63 100644 --- a/Website/api/serializers.py +++ b/Website/api/serializers.py @@ -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'] + fields = ['url', 'name','assignments', 'repo'] class StudentSerializer(serializers.HyperlinkedModelSerializer): classes = ClassesSerializer(many=True, read_only=True,allow_null=True) diff --git a/Website/api/views-back.py b/Website/api/views-back.py new file mode 100644 index 0000000..5c936f9 --- /dev/null +++ b/Website/api/views-back.py @@ -0,0 +1,131 @@ +# class StudentList(APIView): +# """ +# List all snippets, or create a new snippet. +# """ +# def get(self, request, format=None): +# snippets = Student.objects.all() +# serializer = StudentSerializer(snippets, many=True) +# return response.Response(serializer.data) + +# def post(self, request, format=None): +# serializer = StudentSerializer(data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data, status=status.HTTP_201_CREATED) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# class StudentDetail(APIView): +# """ +# Retrieve, update or delete a snippet instance. +# """ +# def get_object(self, pk): +# try: +# return Student.objects.get(pk=pk) +# except Student.DoesNotExist: +# raise Http404 + +# def get(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = StudentSerializer(snippet) +# return response.Response(serializer.data) + +# def put(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = StudentSerializer(snippet, data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# def delete(self, request, pk, format=None): +# snippet = self.get_object(pk) +# snippet.delete() +# return response.Response(status=status.HTTP_204_NO_CONTENT) + +# class TeacherList(APIView): +# """ +# List all snippets, or create a new snippet. +# """ +# def get(self, request, format=None): +# snippets = Teacher.objects.all() +# serializer = TeacherSerializer(snippets, many=True) +# return response.Response(serializer.data) + +# def post(self, request, format=None): +# serializer = TeacherSerializer(data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data, status=status.HTTP_201_CREATED) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# class TeacherDetail(APIView): +# """ +# Retrieve, update or delete a snippet instance. +# """ +# def get_object(self, pk): +# try: +# return Teacher.objects.get(pk=pk) +# except Teacher.DoesNotExist: +# raise Http404 + +# def get(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = TeacherSerializer(snippet) +# return response.Response(serializer.data) + +# def put(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = TeacherSerializer(snippet, data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# def delete(self, request, pk, format=None): +# snippet = self.get_object(pk) +# snippet.delete() +# return response.Response(status=status.HTTP_204_NO_CONTENT) + +# class ClassesList(APIView): +# """ +# List all snippets, or create a new snippet. +# """ +# def get(self, request, format=None): +# snippets = Classes.objects.all() +# serializer = ClassesSerializer(snippets, many=True) +# return response.Response(serializer.data) + +# def post(self, request, format=None): +# serializer = ClassesSerializer(data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data, status=status.HTTP_201_CREATED) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# class ClassesDetail(APIView): +# """ +# Retrieve, update or delete a snippet instance. +# """ +# def get_object(self, pk): +# try: +# return Classes.objects.get(pk=pk) +# except Classes.DoesNotExist: +# raise Http404 + +# def get(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = ClassesSerializer(snippet) +# return response.Response(serializer.data) + +# def put(self, request, pk, format=None): +# snippet = self.get_object(pk) +# serializer = ClassesSerializer(snippet, data=request.data) +# if serializer.is_valid(): +# serializer.save() +# return response.Response(serializer.data) +# return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +# def delete(self, request, pk, format=None): +# snippet = self.get_object(pk) +# snippet.delete() +# return response.Response(status=status.HTTP_204_NO_CONTENT) \ No newline at end of file diff --git a/Website/api/views.py b/Website/api/views.py index 10fe2a8..439c30b 100644 --- a/Website/api/views.py +++ b/Website/api/views.py @@ -1,6 +1,8 @@ from .models import Student, Teacher, Classes, Assignment, DefFiles from .serializers import StudentSerializer, TeacherSerializer, ClassesSerializer, AssignmentSerializer, DefFilesSerializer -from rest_framework import generics, viewsets, permissions +from rest_framework import generics, viewsets, permissions, response, status +from django.http import Http404 +from rest_framework.views import APIView class StudentViewSet(viewsets.ModelViewSet): """