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

WIP - Documentation api, addressing warnings #220

Open
wants to merge 14 commits into
base: dev
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
13 changes: 8 additions & 5 deletions mcserver/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.auth.admin import UserAdmin
from django.shortcuts import render, redirect
from django.contrib.auth.admin import UserAdmin, GroupAdmin

from mcserver.models import (
User,
Session,
Expand All @@ -17,14 +19,14 @@
SubjectTags,
TrialTags
)

from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from django.contrib.admin.models import LogEntry
from datetime import timedelta


#admin.site.unregister(Group)
#admin.site.register(Group, GroupAdmin)
# admin.site.unregister(Group)
# admin.site.register(Group, GroupAdmin)


@admin.register(User)
Expand Down Expand Up @@ -165,6 +167,7 @@ class SubjectAdmin(admin.ModelAdmin):
)
raw_id_fields = ('user',)


@admin.register(SubjectTags)
class SubjectTagsAdmin(admin.ModelAdmin):
search_fields = ['tag', 'subject__name']
Expand All @@ -191,7 +194,7 @@ class ResetPasswordAdmin(admin.ModelAdmin):

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):
# to have a date-based drilldown navigation in the admin page
# to have a date-based drill-down navigation in the admin page
date_hierarchy = 'action_time'

# to filter the resultes by users, content types and action flags
Expand Down
2 changes: 1 addition & 1 deletion mcserver/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def authenticate_credentials(self, key):
Token.objects.create(user=token.user)
raise AuthenticationFailed("Token has expired")

return (token.user, token)
return token.user, token
2 changes: 1 addition & 1 deletion mcserver/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ResultTag(Enum):
CALIBRATION_IMAGE = "calibration-img"
CAMERA_CALIBRATION_OPTS = "calibration_parameters_options"
IK_RESULTS= "ik_results"
IK_RESULTS = "ik_results"
MARKER_DATA = "marker_data"
OPENSIM_MODEL = "opensim_model"
POSE_PICKLE = "pose_pickle"
Expand Down
5 changes: 3 additions & 2 deletions mcserver/customEmailDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.template import Template, Context
from django.template.loader import get_template


class CustomEmailDevice(EmailDevice):

def generate_challenge(self, extra_context=None):
Expand All @@ -25,8 +26,8 @@ def generate_challenge(self, extra_context=None):
send_mail(settings.OTP_EMAIL_SUBJECT,
strip_tags(body),
settings.OTP_EMAIL_SENDER,
[self.email or self.user.email]
,html_message=body)
[self.email or self.user.email],
html_message=body)

message = "sent by email"

Expand Down
35 changes: 14 additions & 21 deletions mcserver/models.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import json

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import MinValueValidator, MaxValueValidator
import os
import uuid
import base64
import pathlib
from http import HTTPStatus
from django.utils import timezone

from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from django.utils import timezone
from django.utils.translation import gettext as _
from rest_framework import status

from django.conf import settings
from django_otp.plugins.otp_email.models import EmailDevice


def random_filename(instance, filename):
Expand Down Expand Up @@ -185,10 +177,11 @@ def is_public(self):
def get_user(self):
return self.trial.get_user()


class Result(models.Model):
trial = models.ForeignKey(Trial, blank=False, null=False, on_delete=models.CASCADE)
device_id = models.CharField(max_length=36, blank=True, null=True)
media = models.FileField(blank=True, null=True, upload_to=random_filename,max_length=500)
media = models.FileField(blank=True, null=True, upload_to=random_filename, max_length=500)
tag = models.CharField(max_length=32, blank=True, null=True)
meta = models.JSONField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
Expand Down Expand Up @@ -217,9 +210,12 @@ def commit(cls, trial, device_id, tag, media_path, meta=None):
)

@classmethod
def reset(cls, trial, tag=None, selected=[]):
def reset(cls, trial, tag=None, selected=None):
""" Deletes selected results, or all for trial with the tag
"""
if selected is None:
selected = []

if selected:
cls.objects.filter(id__in=selected).delete()
elif tag:
Expand Down Expand Up @@ -248,15 +244,12 @@ class ResetPassword(models.Model):
email = models.CharField(max_length=255)
datetime = models.DateField(default=timezone.now)

from django_otp.plugins.otp_email.models import EmailDevice
from django.template.loader import render_to_string
from mcserver.customEmailDevice import CustomEmailDevice

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
"""Create a matching profile whenever a user object is created."""
if created:
device = EmailDevice(user = instance, name = "default e-mail")
device = EmailDevice(user=instance, name="default e-mail")
device.save()


Expand Down Expand Up @@ -423,7 +416,7 @@ class AnalysisResult(models.Model):
help_text='Trial function was called with. Set automatically.',
)
response = models.JSONField(
'Response', default=dict, help_text='Data function responsed with.'
'Response', default=dict, help_text='Data function responded with.'
)
result = models.ForeignKey(
to=Result,
Expand All @@ -436,7 +429,7 @@ class AnalysisResult(models.Model):
'Status',
choices=[(status.value, status.phrase) for status in list(HTTPStatus)],
default=HTTPStatus.OK.value,
help_text='Status code function responsed with.'
help_text='Status code function responded with.'
)
state = models.CharField(
'Invokation state',
Expand Down
19 changes: 11 additions & 8 deletions mcserver/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json

from django.db.models import Prefetch, Q
from django.utils.translation import gettext as _
from rest_framework import serializers
from rest_framework import pagination
from rest_framework.validators import UniqueValidator

from mcserver.models import (
Session,
User,
Expand All @@ -15,9 +19,6 @@
SubjectTags,
TrialTags
)
from rest_framework.validators import UniqueValidator
from django.db.models import Prefetch, Q
from django.utils.translation import gettext as _


class UserSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -88,8 +89,8 @@ def create(self, validated_data):

class Meta:
model = User
fields = ('id', 'username', 'first_name', 'last_name', 'email', 'country', 'institution', 'profession', 'reason',
'website', 'newsletter')
fields = ('id', 'username', 'first_name', 'last_name', 'email', 'country', 'institution', 'profession',
'reason', 'website', 'newsletter')


class ProfilePictureSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -122,16 +123,18 @@ class NewPasswordSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = ('password','token',)
fields = ('password', 'token',)


# Serializers define the API representation.
class VideoSerializer(serializers.ModelSerializer):
video_url = serializers.CharField(max_length=256,
required=False)

class Meta:
model = Video
fields = ['id', 'trial', 'device_id', 'video', 'video_url', 'video_thumb', 'parameters', 'created_at', 'updated_at']
fields = ['id', 'trial', 'device_id', 'video', 'video_url', 'video_thumb', 'parameters', 'created_at',
'updated_at']


# Serializers define the API representation.
Expand Down
47 changes: 21 additions & 26 deletions mcserver/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

from pathlib import Path
from decouple import config
from datetime import timedelta
from celery.schedules import crontab

import os.path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

Expand All @@ -31,7 +33,7 @@
PROTOCOL = config("PROTOCOL", "http")
HOST_URL = "{}://{}".format(PROTOCOL, HOST)

ALLOWED_HOSTS = [HOST,"*"]
ALLOWED_HOSTS = [HOST, "*"]

CORS_ALLOW_ALL_ORIGINS = True

Expand Down Expand Up @@ -85,7 +87,7 @@
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware',
'django_otp.middleware.OTPMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Expand All @@ -110,29 +112,27 @@

WSGI_APPLICATION = 'mcserver.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'data/db.sqlite3',
}
}
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'data/db.sqlite3',
# }
# }

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config("DB_NAME",default="opencap"),
'NAME': config("DB_NAME", default="opencap"),
'USER': config("DB_USER"),
'PASSWORD': config("DB_PASS"),
'HOST': config("DB_HOST"),
'PORT': '5432',
}
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

Expand All @@ -151,7 +151,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand All @@ -165,7 +164,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

Expand All @@ -183,15 +181,15 @@
AWS_S3_REGION_NAME = config("REGION", default="us-west-2")

AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL', default=None)
#AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}

# s3 static settings
# STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

#MEDIA_LOCATION = 'media'
#MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'
#DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# MEDIA_LOCATION = 'media'
# MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'
# DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
ARCHIVES_ROOT = config('ARCHIVES_ROOT', default=os.path.join(MEDIA_ROOT, 'archives'))

Expand All @@ -209,8 +207,6 @@

LOGO_LINK = "https://app.opencap.ai/images/opencap-logo.png"

from datetime import timedelta

AUTH_TOKEN_VALIDITY = timedelta(days=90)

OTP_EMAIL_SENDER = DEFAULT_FROM_EMAIL
Expand All @@ -220,7 +216,6 @@
OTP_EMAIL_SUBJECT = ""
OTP_EMAIL_BODY_TEMPLATE = ""


# Sentry support

SENTRY_DSN = config('SENTRY_DSN', default='')
Expand All @@ -234,7 +229,7 @@

def strip_sensitive_data(event, hint):
""" This function removes the DisallowedHost errors from
the Sentry logs for avoiding excedding the quota.
the Sentry logs for avoiding exceeding the quota.
"""
if 'log_record' in hint:
if hint['log_record'].name == 'django.security.DisallowedHost':
Expand Down Expand Up @@ -266,8 +261,6 @@ def strip_sensitive_data(event, hint):
CELERY_BEAT_SCHEDULER = 'redbeat.RedBeatScheduler'
CELERY_IMPORTS = ['mcserver.tasks']

from celery.schedules import crontab

CELERY_BEAT_SCHEDULE = {
'cleanup_trashed_sessions': {
'task': 'mcserver.tasks.cleanup_trashed_sessions',
Expand Down Expand Up @@ -301,7 +294,9 @@ def strip_sensitive_data(event, hint):
# },
}

GRAPH_MODELS ={
GRAPH_MODELS = {
'all_applications': True,
'graph_models': True,
}
}

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
Loading