Commit a1b233cb authored by Jakob Lund Johannessen's avatar Jakob Lund Johannessen
Browse files

chore: refactor workouts/models

parent ee753da3
Pipeline #171249 failed with stages
in 35 seconds
......@@ -7,7 +7,7 @@ from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.db import models
from django.contrib.auth import get_user_model
from workouts.models import Workout
from workouts.models.workouts import Workout
# Create your models here.
......
from rest_framework import serializers
from rest_framework.serializers import HyperlinkedRelatedField
from comments.models import Comment, Like
from workouts.models import Workout
from workouts.models.workouts import Workout
class CommentSerializer(serializers.HyperlinkedModelSerializer):
......
# Generated by Django 3.1 on 2022-04-02 13:04
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0010_user_profile_visible'),
]
operations = [
migrations.CreateModel(
name='RememberMe',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('remember_me', models.CharField(max_length=500)),
],
),
]
......@@ -80,3 +80,16 @@ class Offer(models.Model):
choices=STATUS_CHOICES,
default=PENDING)
timestamp = models.DateTimeField(auto_now_add=True)
class RememberMe(models.Model):
"""Django model for an remember_me cookie used for remember me functionality.
Attributes:
remember_me: Value of cookie used for remember me
"""
remember_me = models.CharField(max_length=500)
def __str__(self):
return self.remember_me
......@@ -3,7 +3,9 @@
from django.contrib import admin
# Register your models here.
from .models import Exercise, ExerciseInstance, Workout, WorkoutFile
from .models.workouts import Workout
from .models.exercises import Exercise, ExerciseInstance
from .models.storage import WorkoutFile
admin.site.register(Exercise)
admin.site.register(ExerciseInstance)
......
......@@ -3,7 +3,7 @@
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import workouts.models
import workouts.models.workouts
class Migration(migrations.Migration):
......@@ -84,7 +84,7 @@ class Migration(migrations.Migration):
(
"file",
models.FileField(
upload_to=workouts.models.workout_directory_path),
upload_to=workouts.models.storage.workout_directory_path),
),
(
"owner",
......
# Generated by Django 3.1 on 2022-04-02 13:04
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('workouts', '0004_auto_20211020_0950'),
]
operations = [
migrations.DeleteModel(
name='RememberMe',
),
]
"""Contains the models for the workouts Django application. Users
log workouts (Workout), which contain instances (ExerciseInstance) of various
type of exercises (Exercise). The user can also upload files (WorkoutFile) .
"""
import os
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.contrib.auth import get_user_model
class OverwriteStorage(FileSystemStorage):
"""Filesystem storage for overwriting files. Currently unused."""
def get_available_name(self, name, max_length=None):
"""https://djangosnippets.org/snippets/976/
Returns a filename that's free on the target storage system, and
available for new content to be written to.
Args:
name (str): Name of the file
max_length (int, optional): Maximum length of a file name. Defaults to None.
"""
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
# Create your models here.
class Workout(models.Model):
"""Django model for a workout that users can log.
A workout has several attributes, and is associated with one or more exercises
(instances) and, optionally, files uploaded by the user.
Attributes:
name: Name of the workout
date: Date the workout was performed or is planned
notes: Notes about the workout
owner: User that logged the workout
visibility: The visibility level of the workout: Public, Coach, or Private
"""
name = models.CharField(max_length=100)
date = models.DateTimeField()
notes = models.TextField()
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="workouts"
)
# Visibility levels
PUBLIC = "PU" # Visible to all authenticated users
COACH = "CO" # Visible only to owner and their coach
PRIVATE = "PR" # Visible only to owner
VISIBILITY_CHOICES = [
(PUBLIC, "Public"),
(COACH, "Coach"),
(PRIVATE, "Private"),
] # Choices for visibility level
visibility = models.CharField(
max_length=2, choices=VISIBILITY_CHOICES, default=COACH
)
class Meta:
ordering = ["-date"]
def __str__(self):
return self.name
from .workouts import Workout
class Exercise(models.Model):
"""Django model for an exercise type that users can create.
Each exercise instance must have an exercise type, e.g., Pushups, Crunches, or Lunges.
......@@ -115,48 +56,3 @@ class ExerciseInstance(models.Model):
)
sets = models.IntegerField()
number = models.IntegerField()
def workout_directory_path(instance, filename):
"""Return path for which workout files should be uploaded on the web server
Args:
instance (WorkoutFile): WorkoutFile instance
filename (str): Name of the file
Returns:
str: Path where workout file is stored
"""
return f"workouts/{instance.workout.id}/{filename}"
class WorkoutFile(models.Model):
"""Django model for file associated with a workout. Basically a wrapper.
Attributes:
workout: The workout for which this file has been uploaded
owner: The user who uploaded the file
file: The actual file that's being uploaded
"""
workout = models.ForeignKey(
Workout,
on_delete=models.CASCADE,
related_name="files")
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="workout_files"
)
file = models.FileField(upload_to=workout_directory_path)
class RememberMe(models.Model):
"""Django model for an remember_me cookie used for remember me functionality.
Attributes:
remember_me: Value of cookie used for remember me
"""
remember_me = models.CharField(max_length=500)
def __str__(self):
return self.remember_me
"""Contains the models for the workouts Django application. Users
log workouts (Workout), which contain instances (ExerciseInstance) of various
type of exercises (Exercise). The user can also upload files (WorkoutFile) .
"""
import os
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.contrib.auth import get_user_model
from .workouts import Workout
def workout_directory_path(instance, filename):
"""Return path for which workout files should be uploaded on the web server
Args:
instance (WorkoutFile): WorkoutFile instance
filename (str): Name of the file
Returns:
str: Path where workout file is stored
"""
return f"workouts/{instance.workout.id}/{filename}"
class OverwriteStorage(FileSystemStorage):
"""Filesystem storage for overwriting files. Currently unused."""
def get_available_name(self, name, max_length=None):
"""https://djangosnippets.org/snippets/976/
Returns a filename that's free on the target storage system, and
available for new content to be written to.
Args:
name (str): Name of the file
max_length (int, optional): Maximum length of a file name. Defaults to None.
"""
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
class WorkoutFile(models.Model):
"""Django model for file associated with a workout. Basically a wrapper.
Attributes:
workout: The workout for which this file has been uploaded
owner: The user who uploaded the file
file: The actual file that's being uploaded
"""
workout = models.ForeignKey(
Workout,
on_delete=models.CASCADE,
related_name="files")
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="workout_files"
)
file = models.FileField(upload_to=workout_directory_path)
"""Contains the models for the workouts Django application. Users
log workouts (Workout), which contain instances (ExerciseInstance) of various
type of exercises (Exercise). The user can also upload files (WorkoutFile) .
"""
from django.db import models
from django.contrib.auth import get_user_model
class Workout(models.Model):
"""Django model for a workout that users can log.
A workout has several attributes, and is associated with one or more exercises
(instances) and, optionally, files uploaded by the user.
Attributes:
name: Name of the workout
date: Date the workout was performed or is planned
notes: Notes about the workout
owner: User that logged the workout
visibility: The visibility level of the workout: Public, Coach, or Private
"""
name = models.CharField(max_length=100)
date = models.DateTimeField()
notes = models.TextField()
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="workouts"
)
# Visibility levels
PUBLIC = "PU" # Visible to all authenticated users
COACH = "CO" # Visible only to owner and their coach
PRIVATE = "PR" # Visible only to owner
VISIBILITY_CHOICES = [
(PUBLIC, "Public"),
(COACH, "Coach"),
(PRIVATE, "Private"),
] # Choices for visibility level
visibility = models.CharField(
max_length=2, choices=VISIBILITY_CHOICES, default=COACH
)
class Meta:
ordering = ["-date"]
def __str__(self):
return self.name
"""Contains custom DRF permissions classes for the workouts app
"""
from rest_framework import permissions
from workouts.models import Workout
from workouts.models.workouts import Workout
class IsOwner(permissions.BasePermission):
......
......@@ -2,7 +2,10 @@
"""
from rest_framework import serializers
from rest_framework.serializers import HyperlinkedRelatedField
from workouts.models import Workout, Exercise, ExerciseInstance, WorkoutFile, RememberMe
from workouts.models.workouts import Workout
from workouts.models.exercises import Exercise, ExerciseInstance
from workouts.models.storage import WorkoutFile
from users.models import RememberMe
class ExerciseInstanceSerializer(serializers.HyperlinkedModelSerializer):
......
......@@ -26,7 +26,9 @@ from workouts.permissions import (
IsWorkoutPublic,
)
from workouts.mixins import CreateListModelMixin
from workouts.models import Workout, Exercise, ExerciseInstance, WorkoutFile
from workouts.models.workouts import Workout
from workouts.models.exercises import Exercise, ExerciseInstance
from workouts.models.storage import WorkoutFile
from workouts.serializers import WorkoutSerializer, ExerciseSerializer
from workouts.serializers import RememberMeSerializer
from workouts.serializers import ExerciseInstanceSerializer, WorkoutFileSerializer
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment