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

Submission #6

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/cd-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- "master"
- "submission"
jobs:
lint:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -33,6 +34,11 @@ jobs:
DYNACONF_DB_API: ${{ secrets.db_api }}
DYNACONF_DB_NAME: ${{ secrets.db_name }}
DYNACONF_DB_CONN_NAME: ${{ secrets.db_conn_name }}
DYNACONF_GITHUB_CLIENT_SECRET: ${{ secrets.client_secret }}
DYNACONF_GITHUB_CLIENT_ID: ${{ secrets.client_id }}
DYNACONF_GITHUB_URI_OAUTH: https://github.com/login/oauth/access_token
DYNACONF_GITHUB_URI_USER: https://api.github.com/user
DYNACONF_FLASK_SECRET: ${{ secrets.flask_secret }}
run: env | grep DYNACONF_ > .env
- name: Deploy to GCP AppEngine
uses: actions/gcloud/cli@master
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Byte-compiled / optimized / DLL files
dev_helpers/
settings.local.toml
__pycache__/
*.py[cod]
*$py.class

populate_helper.py
# C extensions
*.so
*.floo
Expand Down
15 changes: 0 additions & 15 deletions api/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions api/utils/__init__.py

This file was deleted.

29 changes: 0 additions & 29 deletions api/utils/validators.py

This file was deleted.

8 changes: 0 additions & 8 deletions api/views/__init__.py

This file was deleted.

9 changes: 5 additions & 4 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
runtime: python37

env: flex
entrypoint: uwsgi --http-socket :8080 --wsgi-file sharpener-admin.py --callable app

liveness_check:
path: "api/healthcheck"
path: "/api/healthcheck"
check_interval_sec: 30
timeout_sec: 4
failure_threshold: 2
success_threshold: 2

handlers:
- url: /.*
script: auto
- url: /.*
script: auto
26 changes: 13 additions & 13 deletions connectors/exercism/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
from models import User, Exercise, Artifact


def get_or_create_default_user(session):
default_user = session.query(User).filter(
def get_or_create_default_user(db_session):
default_user = db_session.query(User).filter(
User.email == '[email protected]').first()
if not default_user:
exercism_user = User(email="[email protected]",
name="exercism.io",
nickname="exercism",
is_teacher=True,
github_token="")
session.add(exercism_user)
session.flush()
github_token="sample-github-token",
token="sample-github-token")
db_session.add(exercism_user)
db_session.flush()
return exercism_user
return default_user

Expand Down Expand Up @@ -70,7 +71,7 @@ def process_exercise(name, starting_path, clone_dir,


def populate_exercises(mapper):
def populate_language(session, storage_client, bucket_name):
def populate_language(db_session, storage_client, bucket_name):
print(f"Populating {mapper.language} language")
clone_dir = f"/tmp/{uuid1()}"
starting_path = f"{clone_dir}/exercises"
Expand All @@ -80,21 +81,20 @@ def populate_language(session, storage_client, bucket_name):
print(f"Cloning {mapper.repo}")
Repo.clone_from(mapper.repo, clone_dir)
all_exercises = listdir(f"{starting_path}")
exercism_user = get_or_create_default_user(session)
exercism_user = get_or_create_default_user(db_session)

exercises, files = zip(*[
process_exercise(name, starting_path, clone_dir, blob_prefix,
mapper, bucket, exercism_user)
for name in tqdm(all_exercises, unit='exercise')
])

session.add_all(files)
session.commit()
session.expire_all()
db_session.add_all(files)
db_session.commit()
db_session.expire_all()
for (exercise, file_) in zip(exercises, files):
exercise.artifact_id = file_.id
session.add_all(exercises)
session.commit()
db_session.add_all(exercises)
db_session.commit()

return populate_language

50 changes: 24 additions & 26 deletions db/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@
from models import Base


def create_schema(api, username, password, name, conn_name=None, echo=False):
unix_socket = f'/cloudsql/{conn_name}'
engine = create_engine(
url.URL(
drivername=api,
username=username,
password=password,
database=name,
query={
'host': unix_socket
}),
client_encoding="utf8",
echo=echo)
def create_db_uri(api, username, password, name, conn_name,
production):
if production:
unix_socket = f'/cloudsql/{conn_name}'
db_uri = url.URL(drivername=api, username=username, password=password,
database=name, query={'host': unix_socket})

else:
db_uri = url.URL(drivername=api, username=username, host="localhost",
password=password, database=name)

return db_uri


def create_schema(api, username, password, name,
conn_name=None, echo=False, production=False):
db_uri = create_db_uri(api, username, password,
name, conn_name, production)

engine = create_engine(db_uri, client_encoding="utf8", echo=echo)
return Base.metadata.create_all(engine)


def database_setup(api, username, password, name, conn_name=None, echo=False):
unix_socket = f'/cloudsql/{conn_name}'
engine = create_engine(
url.URL(
drivername=api,
username=username,
password=password,
database=name,
query={
'host': unix_socket
}),
client_encoding="utf8",
echo=echo)
def database_setup(api, username, password, name, conn_name=None,
echo=False, production=False):

db_uri = create_db_uri(api, username, password,
name, conn_name, production)
engine = create_engine(db_uri, client_encoding="utf8", echo=echo)
sess = orm.sessionmaker(bind=engine)
return sess()
Binary file added docs/img/db_schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@
from .artifact import Artifact
from .exercise import Exercise
from .base import Base
from .languages import Language
from .language import Language
from .track import Track
from .classroom import Class
from .enrollment import Enrollment
from .attempt import Attempt
from .submission import Submission
from .track_exercise import TrackExerciseAssociation
from .track_classroom import TrackClassAssociation
from .user_classroom import UserClassAssociation

__all__ = ['User', 'Artifact', 'Exercise', 'Base', 'Language',
'Track', 'Class', 'Enrollment', 'Attempt', 'Submission',
'TrackExercise', 'TrackClassroom']
26 changes: 26 additions & 0 deletions models/attempt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from datetime import datetime
from .base import Base
from sqlalchemy import (Column, String, Integer, UniqueConstraint,
ForeignKey, DateTime, Enum)
from sqlalchemy.orm import relationship
from .language import Language


class Attempt(Base):
__tablename__ = 'attempts'
submission_id = Column(ForeignKey('submissions.id'), primary_key=True)
submission = relationship('Submission')
attempt_number = Column('attempt_number', Integer, primary_key=True)

solution_file = Column(String, nullable=False)
test_coverage = Column(String, nullable=False)
test_output = Column(String, nullable=False)
test_checksum = Column(String, nullable=False)
submitted_at = Column(DateTime, default=datetime.utcnow,
nullable=False)

def __repr__(self):
return ("<Attempt(user='%s', class_id='%s', \
track='%s', class='%s', exercise='%s', attempt_number='%s')>" %
(self.user, self.class_id, self.track, self.classroom,
self.exercise, self.attempt_number))
28 changes: 28 additions & 0 deletions models/classroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from datetime import datetime
from .base import Base
from sqlalchemy import (Column, String, Integer, ForeignKey, UniqueConstraint,
DateTime)
from sqlalchemy.orm import relationship


class Class(Base):
__tablename__ = 'classes'
__table_args__ = (UniqueConstraint('name', 'owner'),)

id = Column(Integer, primary_key=True)
owner = Column(String, ForeignKey('users.email'))
name = Column(String, nullable=False)
invite_token = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow,
nullable=False)
students = relationship("User", secondary='users_classes_association',
back_populates="classes")
tracks = relationship("Track", secondary='tracks_classes_association',
back_populates="classes")

tracks_classes = relationship("TrackClassAssociation",
back_populates="class_ref")

def __repr__(self):
return ("<Class(id='%s', name='%s', owner='%s')>"
% (self.id, self.name, self.owner))
25 changes: 25 additions & 0 deletions models/enrollment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from datetime import datetime
from .base import Base
from sqlalchemy import (Column, String, ForeignKey, DateTime)
from sqlalchemy.orm import relationship


class Enrollment(Base):
__tablename__ = 'enrollments'

user = Column(String, ForeignKey('users.email'), primary_key=True)
track_class_id = Column('track_class_id',
ForeignKey('tracks_classes_association.id'),
primary_key=True)

track_class = relationship('TrackClassAssociation', uselist=False,
back_populates="enrollments")
submissions = relationship('Submission')
enrolled_at = Column(DateTime, default=datetime.utcnow, nullable=False)

enrolled_user = relationship('User', back_populates="enrolled_in",
uselist=False)

def __repr__(self):
return ("<Enrollment(user='%s', track_class_id='%s'')>" %
(self.user, self.track_class_id))
7 changes: 6 additions & 1 deletion models/exercise.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .base import Base
from .languages import Language
from .language import Language
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey, Column, Integer, String, Text, ARRAY, Enum

Expand All @@ -17,3 +17,8 @@ class Exercise(Base):
artifact = relationship("Artifact",
back_populates="exercise",
uselist=False)
tracks = relationship("Track",
secondary="tracks_exercises_association",
back_populates="exercises")
created_by = relationship("User", uselist=False,
back_populates="created_exercises")
File renamed without changes.
41 changes: 41 additions & 0 deletions models/submission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from uuid import uuid4
from .base import Base
from sqlalchemy import (Column, String, Integer, ForeignKeyConstraint,
UniqueConstraint, Enum)
from sqlalchemy.orm import relationship
from .submission_state import SubmissionStatus
from .language import Language


class Submission(Base):
__tablename__ = 'submissions'
__table_args__ = (
ForeignKeyConstraint(
['exercise_name', 'exercise_language'],
['exercises.name', 'exercises.language']
),
ForeignKeyConstraint(
['user', 'track_class_id'],
['enrollments.user', 'enrollments.track_class_id']),
UniqueConstraint('user', 'track_class_id', 'exercise_name',
'exercise_language')
)

id = Column('id', Integer, primary_key=True)
user = Column('user', String)
track_class_id = Column('track_class_id', Integer)
enrollment = relationship('Enrollment', uselist=False)
exercise_name = Column('exercise_name', String)
exercise_language = Column('exercise_language', Enum(Language))
exercise = relationship('Exercise')
attempts = relationship('Attempt')
submission_token = Column('submission_token', String, nullable=False,
default=uuid4())
status = Column('status', Enum(SubmissionStatus),
default=SubmissionStatus('pending'))

def __repr__(self):
return ("<Submission(user='%s', class_id='%s', \
track='%s', class='%s', exercise='%s')>" %
(self.user, self.class_id, self.track, self.classroom,
self.exercise))
Loading