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

Login with GitHub #7

Merged
merged 75 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 70 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
b267d6f
Start api models for contribution agreement (#82)
SalmanAsh Jul 5, 2024
84fd2a1
api models for contribution
SalmanAsh Jul 8, 2024
1db562a
change import sorting and format
SalmanAsh Jul 8, 2024
b70bda6
fixture data for testing
SalmanAsh Jul 9, 2024
bdb7581
change model fields
SalmanAsh Jul 9, 2024
24dc96a
test string parsing
SalmanAsh Jul 9, 2024
46c522f
Migrate models
SalmanAsh Jul 9, 2024
8878e9f
fixture data for testing
SalmanAsh Jul 9, 2024
a7681a4
api models and tests files
SalmanAsh Jul 9, 2024
47306f6
fix import statements
SalmanAsh Jul 9, 2024
0af1004
fix code format
SalmanAsh Jul 9, 2024
7e69d68
Fix static types
SalmanAsh Jul 9, 2024
ef5f20c
fix linting test file format
SalmanAsh Jul 9, 2024
72c5654
import fix
SalmanAsh Jul 9, 2024
44dfbb6
Check static code
SalmanAsh Jul 9, 2024
cc05cc2
import sort
SalmanAsh Jul 9, 2024
77e5a80
update DateTime Field
SalmanAsh Jul 9, 2024
9a8f976
Tests for models
SalmanAsh Jul 9, 2024
269ace9
Repository testing
SalmanAsh Jul 9, 2024
6249929
fix class name
SalmanAsh Jul 10, 2024
20a9537
tests for models
SalmanAsh Jul 10, 2024
9f55c76
correct formatting and comments
SalmanAsh Jul 10, 2024
def8ff7
Change meta classes
SalmanAsh Jul 10, 2024
0a22567
Fix code format and imports
SalmanAsh Jul 10, 2024
b5ec844
Delete all fruit files
SalmanAsh Jul 10, 2024
93f7398
Delete last fruit migration
SalmanAsh Jul 10, 2024
9fba816
Fix migration issue
SalmanAsh Jul 10, 2024
5dab9d0
change data fields
SalmanAsh Jul 10, 2024
8b23360
Apply changes after review
SalmanAsh Jul 10, 2024
9fe496d
apply initial migrations
SalmanAsh Jul 10, 2024
7f3efa4
Fix contributor type error
SalmanAsh Jul 10, 2024
bf18e02
Add verbose names
SalmanAsh Jul 11, 2024
bf17776
Fix import error
SalmanAsh Jul 11, 2024
7fe08cd
Create api serializers
SalmanAsh Jul 11, 2024
f9029e3
Test the serializer's functionality
SalmanAsh Jul 12, 2024
b421334
Check static code
SalmanAsh Jul 12, 2024
c040e51
Add module doc strings
SalmanAsh Jul 12, 2024
276b709
Contributor tests
SalmanAsh Jul 12, 2024
6588722
Apply changes from second review
SalmanAsh Jul 12, 2024
519dfdd
Migrate models
SalmanAsh Jul 12, 2024
f7f88b4
Updated models from merge
SalmanAsh Jul 12, 2024
18decc5
Fix doc strings
SalmanAsh Jul 12, 2024
d4c9bd3
check if a contributor has signed latest agreement
SalmanAsh Jul 15, 2024
f5fb134
fix name arguments
SalmanAsh Jul 15, 2024
9aa7142
Add urls for views
SalmanAsh Jul 16, 2024
05d071f
allow any permission change
SalmanAsh Jul 16, 2024
31bc713
Endpoint to check agreement signature
SalmanAsh Jul 16, 2024
5799ca8
fix too many ancestors
SalmanAsh Jul 16, 2024
05bc96f
change test data
SalmanAsh Jul 16, 2024
0b019fa
Set up test files
SalmanAsh Jul 16, 2024
750d4ab
Merge dev content
SalmanAsh Jul 16, 2024
b6de29a
tests for contributor api
SalmanAsh Jul 16, 2024
49e5457
test endpoint action
SalmanAsh Jul 16, 2024
723f6ac
test no contributor in data table
SalmanAsh Jul 17, 2024
3f3b81e
Add validation on time of signature
SalmanAsh Jul 17, 2024
16ff882
sign agreement endpoint
SalmanAsh Jul 17, 2024
31e7f9f
Update fixture data for new tests
SalmanAsh Jul 18, 2024
97d3199
Complete and test second endpoint
SalmanAsh Jul 18, 2024
1e400cd
Improve test coverage
SalmanAsh Jul 18, 2024
2aba071
Apply review changes
SalmanAsh Jul 18, 2024
1f95859
Apply changed from second review
SalmanAsh Jul 19, 2024
77e52ee
ignore pylint warning
SalmanAsh Jul 19, 2024
cffe235
Set API version
SalmanAsh Jul 19, 2024
e393154
Merge dev to branch
SalmanAsh Jul 22, 2024
463eb78
Log into github and create new contributor
SalmanAsh Jul 23, 2024
e1d3b28
test login with github
SalmanAsh Jul 24, 2024
1061544
add new dependency
SalmanAsh Jul 25, 2024
0a24873
update user data during login
SalmanAsh Jul 25, 2024
a66fe7d
fix line too long issue
SalmanAsh Jul 25, 2024
eb23967
Refactor code
SalmanAsh Jul 25, 2024
a055538
Apply review changes
SalmanAsh Jul 30, 2024
58d4254
Override contributor create method
SalmanAsh Jul 30, 2024
6f12cab
Apply second review changes
SalmanAsh Jul 31, 2024
a86a808
apply review changes
SalmanAsh Aug 1, 2024
d63b126
Add method doc strings
SalmanAsh Aug 1, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,6 @@ cython_debug/

# Django
static/

# 0Auth app
.env
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ codeforlife = {ref = "v0.18.0", git = "https://github.com/ocadotechnology/codefo
codeforlife = {ref = "v0.18.0", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
# codeforlife = {file = "../codeforlife-package-python", editable = true, extras = ["dev"]}
# 🚫 Don't add [dev-packages] below that are inherited from the CFL package.
python-dotenv = "==1.0.1"

[requires]
python_version = "3.8"
11 changes: 10 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.25 on 2024-07-22 14:17
# Generated by Django 3.2.25 on 2024-07-23 13:03

import django.core.validators
from django.db import migrations, models
Expand All @@ -19,7 +19,7 @@ class Migration(migrations.Migration):
('id', models.IntegerField(help_text="The contributor's GitHub user-ID.", primary_key=True, serialize=False)),
('email', models.EmailField(max_length=254, verbose_name='email')),
('name', models.TextField(verbose_name='name')),
('location', models.TextField(verbose_name='location')),
('location', models.TextField(null=True, verbose_name='location')),
('html_url', models.TextField(verbose_name='html url')),
('avatar_url', models.TextField(verbose_name='avatar url')),
],
Expand Down
2 changes: 1 addition & 1 deletion api/models/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Contributor(models.Model):
)
email = models.EmailField(_("email"))
name = models.TextField(_("name"))
location = models.TextField(_("location"))
location = models.TextField(_("location"), null=True)
html_url = models.TextField(_("html url"))
avatar_url = models.TextField(_("avatar url"))

Expand Down
2 changes: 2 additions & 0 deletions api/serializers/agreement_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# pylint: disable=missing-function-docstring
# pylint: disable=too-many-ancestors

# pylint: disable-next=missing-function-docstring,too-many-ancestors


class AgreementSignatureSerializer(ModelSerializer[User, AgreementSignature]):
class Meta:
Expand Down
87 changes: 85 additions & 2 deletions api/views/contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,100 @@
Created on 16/07/2024 at 11:03:09(+01:00).
"""

import requests
from codeforlife.permissions import AllowAny
from codeforlife.request import Request
from codeforlife.response import Response
from codeforlife.user.models import User
from codeforlife.views import ModelViewSet
from codeforlife.views import ModelViewSet, action
from rest_framework import status

import settings

from ..models import Contributor
from ..serializers import ContributorSerializer


# pylint: disable-next=missing-class-docstring,too-many-ancestors
class ContributorViewSet(ModelViewSet[User, Contributor]):
http_method_names = ["get", "post"]
http_method_names = ["get", "post", "delete"]
permission_classes = [AllowAny]
serializer_class = ContributorSerializer
queryset = Contributor.objects.all()

@action(detail=False, methods=["get"])
def log_into_github(self, request: Request):
"""Users can login using their existing github account"""
# Get code from login request
if not request.GET.get("code"):
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

# Get user access Token
access_token_request = requests.post(
url="https://github.com/login/oauth/access_token",
headers={"Accept": "application/json"},
params={
"client_id": settings.GITHUB_CLIENT_ID,
"client_secret": settings.GITHUB_CLIENT_SECRET,
"code": request.GET.get("code"),
},
timeout=5,
)

if not access_token_request.ok:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
auth_data = access_token_request.json()

# Code expired
if "access_token" not in auth_data:
return Response(
status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS
)

access_token = auth_data["access_token"]
token_type = auth_data["token_type"]

# Get user's information
user_data_request = requests.get(
url="https://api.github.com/user",
headers={
"Accept": "application/json",
"Authorization": f"{token_type} {access_token}",
},
timeout=5,
)

user_data = user_data_request.json()
if not user_data["email"]:
return Response(
data="Email null",
status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS,
)

# Check if user is already a contributor
gh_id = user_data["id"]
contributor_data = {
"id": gh_id,
"email": user_data["email"],
"name": user_data["name"],
"location": user_data["location"],
"html_url": user_data["html_url"],
"avatar_url": user_data["avatar_url"],
}

try:
# Update an existing contributor
contributor = Contributor.objects.get(pk=gh_id)
serializer = ContributorSerializer(
contributor, data=contributor_data
)
except Contributor.DoesNotExist:
# Create a new contributor
serializer = ContributorSerializer(data=contributor_data)

if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_200_OK)
return Response(
status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS,
)
Loading
Loading