forked from ChildMindInstitute/mindlogger-backend-refactor
-
Notifications
You must be signed in to change notification settings - Fork 0
Create prolific subject #4
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
Draft
hamzace
wants to merge
4
commits into
add-prolific-completion-code-retrieval
Choose a base branch
from
create-prolific-subject
base: add-prolific-completion-code-retrieval
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| from gettext import gettext as _ | ||
|
|
||
| from pydantic import ValidationError | ||
|
|
||
| from apps.shared.exception import UnauthorizedError | ||
|
|
||
|
|
||
| class ProlificInvalidApiTokenError(UnauthorizedError): | ||
| message = _("Prolific token is invalid.") | ||
|
|
||
|
|
||
| class ProlificInvalidStudyError(UnauthorizedError): | ||
| message = _("Invalid Prolific study id.") | ||
|
|
||
|
|
||
| class ProlificIntegrationNotConfiguredError(ValidationError): | ||
| message = _("Prolific integration not configured.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import hashlib | ||
| import uuid | ||
|
|
||
| from fastapi import HTTPException | ||
| from apps.applets.crud.applets import AppletsCRUD | ||
| from apps.authentication.services.security import AuthenticationService | ||
| from apps.integrations.prolific.domain import ProlificParamsActivityAnswer | ||
| from apps.shared.hashing import hash_sha224 | ||
| from apps.subjects.domain import SubjectCreate | ||
| from apps.subjects.services.subjects import SubjectsService | ||
| from apps.users.cruds.user import UsersCRUD | ||
| from apps.users.db.schemas import UserSchema | ||
| from config import settings | ||
|
|
||
| class ProlificUserService: | ||
| def __init__(self, session, prolific_participant: ProlificParamsActivityAnswer) -> None: | ||
| self.session = session | ||
| self.prolific_pid = prolific_participant.prolific_pid | ||
| self.prolific_session_id = prolific_participant.session_id | ||
| self.prolific_study_id = prolific_participant.study_id | ||
|
|
||
|
|
||
| async def create_prolific_respondent(self) -> UserSchema: | ||
| hash_object = hashlib.sha256(f"{self.prolific_pid}-{self.prolific_study_id}".encode('utf-8')) | ||
| prolific_respondent_id = uuid.UUID(hash_object.hexdigest()[:32]) | ||
|
|
||
| crud = UsersCRUD(self.session) | ||
|
|
||
| prolific_respondent = await crud.get_prolific_respondent(prolific_respondent_id) | ||
| if not prolific_respondent: | ||
| prolific_respondent = UserSchema( | ||
| id=prolific_respondent_id, | ||
| email=hash_sha224(self._get_formated_email()), | ||
| first_name=settings.prolific_respondent.first_name, | ||
| last_name=settings.prolific_respondent.last_name, | ||
| hashed_password=AuthenticationService(self.session).get_password_hash( | ||
| settings.anonymous_respondent.password | ||
| ), | ||
| email_encrypted=self._get_formated_email(), | ||
| is_prolific_respondent=True, | ||
| ) | ||
|
|
||
| return await crud.save(prolific_respondent) | ||
|
|
||
| # As the id is generated from the prolific_pid and prolific_session_id | ||
| # that means the user already answered the survey with the session_id | ||
| raise HTTPException(status_code=400, detail="User already answered the survey") | ||
|
|
||
| async def create_subject_for_prolific_respondent(self, prolific_respondent: UserSchema, applet_id: uuid.UUID) -> None: | ||
| subject_service = SubjectsService(self.session, prolific_respondent.id) | ||
| subject = await subject_service.get_by_user_and_applet(prolific_respondent.id, applet_id) | ||
| applet = await AppletsCRUD(session=self.session).get_by_id(applet_id) | ||
|
|
||
| if not subject or subject.is_deleted: | ||
| await subject_service.create( | ||
| SubjectCreate( | ||
| applet_id=applet_id, | ||
| creator_id=applet.creator_id, | ||
| user_id=prolific_respondent.id, | ||
| first_name=prolific_respondent.first_name, | ||
| last_name=prolific_respondent.last_name, | ||
| secret_user_id=self._get_formated_secret_user_id(), | ||
| email=self._get_formated_email(), | ||
| # Storing prolific params as JSON for easy parse. | ||
| nickname=ProlificParamsActivityAnswer(prolific_pid=self.prolific_pid, session_id=self.prolific_session_id, study_id=self.prolific_study_id).json(), | ||
| ) | ||
| ) | ||
|
|
||
| def _get_formated_email(self): | ||
| return f"{self.prolific_pid}-{self.prolific_session_id}@{settings.prolific_respondent.domain}" | ||
|
|
||
| def _get_formated_secret_user_id(self): | ||
| return f"{settings.prolific_respondent.secret_user_id}{self.prolific_pid}-{self.prolific_study_id}-{self.prolific_session_id}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class ProlificRespondent(BaseModel): | ||
| domain: str = "prolific.com" | ||
| password: str = "prolificRespondentPassword!" | ||
| first_name: str = "Prolific" | ||
| last_name: str = "ChildMindInstitute" | ||
| secret_user_id: str = "Prolific PID: " |
29 changes: 29 additions & 0 deletions
29
...cture/database/migrations/versions/2025_01_24_20_55-add_is_prolific_respondant_in_user.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Add is Prolific Respondant in User | ||
|
|
||
| Revision ID: 1e448d205193 | ||
| Revises: dc2dd9e195d5 | ||
| Create Date: 2025-01-24 20:55:53.626692 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy.dialects import postgresql | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "1e448d205193" | ||
| down_revision = "dc2dd9e195d5" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.add_column("users", sa.Column("is_prolific_respondent", sa.Boolean(), server_default="false", nullable=True)) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_column("users", "is_prolific_respondent") | ||
| # ### end Alembic commands ### |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.