Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint test #13

Merged
merged 2 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/lint_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Normalize Repos
on:
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pycodestyle
- name: Run lint test
run: |
pycodestyle
env:
ADMIN_GITHUB_TOKEN: ${{ secrets.ADMIN_GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions rest_api/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DEPARTMENTS = Index('departments')
FILES = Index('files')


@COURSES.doc_type
class CourseDocument(Document):
class Django:
Expand All @@ -16,6 +17,7 @@ class Django:
'code',
]


@DEPARTMENTS.doc_type
class DepartmentDocument(Document):
class Django:
Expand All @@ -27,6 +29,7 @@ class Django:
'abbreviation',
]


@FILES.doc_type
class FileDocument(Document):
class Django:
Expand Down
28 changes: 15 additions & 13 deletions rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get(self, request):
return Response({
"department": serializer,
"courses": serializer_course
})
})
else:
return Response({"department": serializer_department.data})

Expand Down Expand Up @@ -104,10 +104,10 @@ def get_extra_actions(cls):

def get_size(size):
file_size = size
if round(file_size/(1024*1024), 2) == 0.00:
return str(round(file_size/(1024), 2))+" KB"
if round(file_size / (1024 * 1024), 2) == 0.00:
return str(round(file_size / (1024), 2)) + " KB"
else:
return str(round(file_size/(1024*1024), 2))+" MB"
return str(round(file_size / (1024 * 1024), 2)) + " MB"


def fileName(file):
Expand Down Expand Up @@ -158,7 +158,7 @@ def post(self, request):
fileext=get_fileext(data['title']),
filetype=data['filetype'],
finalized=data['finalized']
)
)
file.save()
return Response(file.save(), status=status.HTTP_201_CREATED)
else:
Expand All @@ -183,19 +183,19 @@ def get(self, request):
query=q,
type="phrase_prefix",
fields=['title', 'code']
)
)
departments = DepartmentDocument.search().query(
"multi_match",
query=q,
type="phrase_prefix",
fields=['title', 'abbreviation']
)
)
files = FileDocument.search().query(
"multi_match",
query=q,
type="phrase_prefix",
fields=['title', 'fileext', 'filetype']
)
)

response_courses = courses.execute()
queryset_courses = Course.objects.none()
Expand All @@ -206,8 +206,10 @@ def get(self, request):

for hit in response_files.hits.hits:
fileId = hit['_source']["id"]
query_files = File.objects.filter(id=fileId).filter(finalized=True)
queryset_files = list(itertools.chain(queryset_files, query_files))
query_files = File.objects.filter(id=fileId, finalized=True)
queryset_files = list(
itertools.chain(queryset_files, query_files)
)

for hit in response_departments.hits.hits:
departmentId = hit['_source']["id"]
Expand All @@ -225,9 +227,9 @@ def get(self, request):
serializer_files = FileSerializer(queryset_files, many=True).data

return Response({
"departments" : serializer_departments,
"courses" : serializer_courses,
"files" : serializer_files,
"departments": serializer_departments,
"courses": serializer_courses,
"files": serializer_files,
}, status=status.HTTP_200_OK)
else:
return Response(status.HTTP_400_BAD_REQUEST)
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pycodestyle]
count = False
ignore = W191
max-line-length = 160
statistics = True
exclude = venv/*, rest_api/migrations/*, users/migrations/*, studyportal/settings.py
1 change: 1 addition & 0 deletions studyportal/drive/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'token.pickle'
)


def driveinit():
creds = None
SCOPES = [
Expand Down
86 changes: 48 additions & 38 deletions studyportal/falcon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,77 @@


class DataResponse:
def __init__(self, AccessToken, TokenType, ExpiresIn):
self.AccessToken = AccessToken
self.TokenType = TokenType
self.ExpiresIn = ExpiresIn
def __init__(self, AccessToken, TokenType, ExpiresIn):
self.AccessToken = AccessToken
self.TokenType = TokenType
self.ExpiresIn = ExpiresIn


class FalconClient:
def __init__(self, ClientId, ClientSecret, URLAccessToken, URLResourceOwner, AccountsURL, RedirectURL):
self.ClientId = ClientId
self.ClientSecret = ClientSecret
self.URLAccessToken = URLAccessToken
self.URLResourceOwner = URLResourceOwner
self.AccountsURL = AccountsURL
self.RedirectURL = RedirectURL
def __init__(self, ClientId, ClientSecret, URLAccessToken, URLResourceOwner, AccountsURL, RedirectURL):
self.ClientId = ClientId
self.ClientSecret = ClientSecret
self.URLAccessToken = URLAccessToken
self.URLResourceOwner = URLResourceOwner
self.AccountsURL = AccountsURL
self.RedirectURL = RedirectURL


COOKIE_NAME = "sdslabs"


def make_request(url, token):
headers = { 'Authorization': "Bearer {}".format(token), "Content-Type": "application/json" }
response = requests.get(url, headers=headers)
return response.json()
headers = {
'Authorization': "Bearer {}".format(token),
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
return response.json()


def get_token(config):
data = { "client_id": config.ClientId, "client_secret": config.ClientSecret, "grant_type": "client_credentials", "scope": "email image_url" }
headers = { "Content-Type": "application/x-www-form-urlencoded" }
response = requests.post(config.URLAccessToken, data=data, headers=headers)
return response.json()['access_token']
data = {
"client_id": config.ClientId,
"client_secret": config.ClientSecret,
"grant_type": "client_credentials",
"scope": "email image_url"
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
response = requests.post(config.URLAccessToken, data=data, headers=headers)
return response.json()['access_token']


def get_user_by_id(id, config):
token = get_token(config)
response = make_request(config.URLResourceOwner+"id/"+id, token)
return response
token = get_token(config)
response = make_request(config.URLResourceOwner + "id/" + id, token)
return response


def get_user_by_username(username, config):
token = get_token(config)
response = make_request(config.URLResourceOwner+"username/"+username, token)
return response
token = get_token(config)
response = make_request(config.URLResourceOwner + "username/" + username, token)
return response


def get_user_by_email(email, config):
token = get_token(config)
response = make_request(config.URLResourceOwner+"email/"+email, token)
return response
token = get_token(config)
response = make_request(config.URLResourceOwner + "email/" + email, token)
return response


def get_logged_in_user(config, cookies):
cookie = cookies[COOKIE_NAME]
if cookie == "":
return ""
token = get_token(config)
user_data = make_request(config.URLResourceOwner+"logged_in_user/"+ cookie, token)
return user_data
cookie = cookies[COOKIE_NAME]
if cookie == "":
return ""
token = get_token(config)
user_data = make_request(config.URLResourceOwner + "logged_in_user/" + cookie, token)
return user_data


def login(config, cookies):
user_data = get_logged_in_user(config, cookies)
if user_data == None:
user_data = HttpResponseRedirect(config.AccountsURL+"/login?redirect=//"+config.RedirectURL)
return user_data

user_data = get_logged_in_user(config, cookies)
if user_data is None:
user_data = HttpResponseRedirect(config.AccountsURL + "/login?redirect=//" + config.RedirectURL)
return user_data
11 changes: 6 additions & 5 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'test/resources/structure.json'
)


def getUserFromJWT(token):
decoded_jwt = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
user = User.objects.get(username=decoded_jwt['username'])
Expand All @@ -54,7 +55,7 @@ def get(self, request):
'profile_image': user['image_url'],
'role': 'user'
}
requests.post(NEXUS_URL+'/users', data=data)
requests.post(NEXUS_URL + '/users', data=data)
queryset = User.objects.filter(falcon_id=user['id'])
serializer = UserSerializer(queryset, many=True)
user = serializer.data[0]
Expand Down Expand Up @@ -288,23 +289,23 @@ def post(self, request):
ext = type.split("/")[1].split(";")[0]
base64String = file.split(",")[1]
rand = str(random.randint(0, 100000))
temp = open("temp"+rand+"."+ext, "wb")
temp = open("temp" + rand + "." + ext, "wb")
temp.write(base64.b64decode(base64String))
file_details = {
'name': name,
'mime_type': mime_type,
'location': "temp"+rand+"."+ext
'location': "temp" + rand + "." + ext
}
# Get folder id from config
course = Course.objects.get(id=request.data['course'])
folder_identifier = request.data['filetype'].lower().replace(" ","") + "_review"
folder_identifier = request.data['filetype'].lower().replace(" ", "") + "_review"
folder_id = structure['study'][course.department.abbreviation][course.code][folder_identifier]
driveid = uploadToDrive(
driveinit(),
folder_id,
file_details
)
os.remove("temp"+rand+"."+ext)
os.remove("temp" + rand + "." + ext)
# end of manipulation
token = request.headers['Authorization'].split(' ')[1]
username = getUserFromJWT(token)['username']
Expand Down