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

File upload #306

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0fcc2ca
file upload
benjmnxu Mar 17, 2024
86085cd
file upload
benjmnxu Mar 23, 2024
2e8d2ed
deployment test
benjmnxu Mar 23, 2024
106b212
why is everything so deprecated
benjmnxu Mar 23, 2024
2926225
created pipfile.lock
benjmnxu Mar 23, 2024
a040f81
corrected django-storages default
benjmnxu Mar 24, 2024
fa502b9
modify test syntax / uncomment permissions
benjmnxu Mar 24, 2024
986eb2e
static file test
benjmnxu Mar 24, 2024
ddb0d70
Delete Pipfile
benjmnxu Mar 24, 2024
37efc74
staticstorage test
benjmnxu Mar 24, 2024
8d87055
Merge branch 'file-upload' of https://github.com/pennlabs/office-hour…
benjmnxu Mar 24, 2024
21d9e31
modify installed apps
benjmnxu Mar 24, 2024
361bd80
revert base.py
benjmnxu Mar 24, 2024
360eee5
revert pips
benjmnxu Mar 24, 2024
54a1a19
new pipfile
benjmnxu Mar 24, 2024
990271f
package version test
benjmnxu Mar 24, 2024
86a1fe4
remove storages from install_apps
benjmnxu Mar 24, 2024
66d6d63
modify pipfile
benjmnxu Mar 24, 2024
6abd8cb
storages version hardcode
benjmnxu Mar 24, 2024
85fe091
django-storages move back
benjmnxu Mar 24, 2024
c44463f
specify python version
benjmnxu Mar 24, 2024
3dc57d5
improvements: migrate to django 5.0.3 and channels 3.0.5
trangiabach Mar 24, 2024
5c6ff82
improvement: fixed to django 5.0.3
trangiabach Mar 24, 2024
76a2580
migrate python to 3.11
trangiabach Mar 24, 2024
d7378b9
change base image to python 3.11
trangiabach Mar 24, 2024
8111e4b
Merge branch 'django-migrate' into file-upload
benjmnxu Mar 26, 2024
04f1d5d
file-upload with updated django
benjmnxu Mar 26, 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
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM pennlabs/django-base:a142aa6975ee293bbc8a09ef0b81998ce7063dd3
FROM pennlabs/django-base:b269ea1613686b1ac6370154debbb741b012de1a-3.11

LABEL maintainer="Penn Labs"

Expand Down
12 changes: 7 additions & 5 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ dj-database-url = "*"
djangorestframework = "*"
psycopg2 = "*"
sentry-sdk = "*"
django = "==3.1.7"
django = "~=5.0.3"
django-cors-headers = "*"
pyyaml = "*"
uritemplate = "*"
uwsgi = "*"
django-labs-accounts = "==0.7.1"
django-phonenumber-field = {extras = ["phonenumbers"],version = "*"}
django-phonenumber-field = {extras = ["phonenumbers"]}
drf-nested-routers = "*"
django-email-tools = "*"
twilio = "*"
Expand All @@ -36,13 +36,15 @@ celery = "*"
redis = "*"
django-auto-prefetching = "*"
django-rest-live = ">=0.7.0"
channels = "<3"
channels = "==3.0.5"
channels-redis = "*"
uvicorn = {extras = ["standard"],version = "*"}
uvicorn = {extras = ["standard"]}
gunicorn = "*"
django-scheduler = "*"
typing-extensions = "*"
drf-excel = "*"
django-storages = "*"
boto3 = "*"

[requires]
python_version = "3"
python_version = "3.11"
2,485 changes: 1,312 additions & 1,173 deletions backend/Pipfile.lock

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion backend/officehoursqueue/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

ALLOWED_HOSTS = ["*"]

CSRF_TRUSTED_ORIGINS = ["http://127.0.0.1:3000", "https://ohq.io"]

# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
Expand All @@ -51,6 +51,7 @@
"accounts.apps.AccountsConfig",
"ohq.apps.OhqConfig",
"schedule",
"storages"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -185,3 +186,19 @@
# Default to in-memory Channel Layer for dev and CI.

CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}

AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY") #NEEDS SETUP
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SAK") #NEEDS SETUP
AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_BUCKET_NAME") #NEEDS SETUP
AWS_S3_REGION_NAME = "us-east-1"

STORAGES = {
"default": {
"BACKEND": "storages.backends.s3.S3Storage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
}
}
AWS_S3_FILE_OVERWRITE = False
AWS_S3_VERITY = True
2 changes: 2 additions & 0 deletions backend/ohq/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MembershipInvite,
Profile,
Question,
QuestionFile,
Queue,
QueueStatistic,
Semester,
Expand All @@ -21,6 +22,7 @@
admin.site.register(MembershipInvite)
admin.site.register(Profile)
admin.site.register(Question)
admin.site.register(QuestionFile)
admin.site.register(Queue)
admin.site.register(Semester)
admin.site.register(QueueStatistic)
Expand Down
22 changes: 22 additions & 0 deletions backend/ohq/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django import forms

class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True


class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)

def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result


class UploadFileForm(forms.Form):
file_field = MultipleFileField()
19 changes: 19 additions & 0 deletions backend/ohq/migrations/0020_question_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.4 on 2024-03-17 04:03

from django.db import migrations, models
import ohq.models


class Migration(migrations.Migration):

dependencies = [
('ohq', '0019_auto_20211114_1800'),
]

operations = [
migrations.AddField(
model_name='question',
name='image',
field=models.FileField(blank=True, upload_to=ohq.models.Question.get_course_upload_path),
),
]
26 changes: 26 additions & 0 deletions backend/ohq/migrations/0021_auto_20240317_1650.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.1.7 on 2024-03-17 16:50

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('ohq', '0020_question_image'),
]

operations = [
migrations.RemoveField(
model_name='question',
name='image',
),
migrations.CreateModel(
name='QuestionFile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to='question_files/')),
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ohq.question')),
],
),
]
22 changes: 22 additions & 0 deletions backend/ohq/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from django.conf import settings
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
Expand Down Expand Up @@ -258,6 +260,10 @@ class Question(models.Model):
(STATUS_REJECTED, "Rejected"),
(STATUS_ANSWERED, "Answered"),
]

def get_course_upload_path(instance, filename):
return f"{instance.queue.course.__str__()}/{filename}"

text = models.TextField()
queue = models.ForeignKey(Queue, on_delete=models.CASCADE)
video_chat_url = models.URLField(blank=True, null=True)
Expand All @@ -283,6 +289,22 @@ class Question(models.Model):
tags = models.ManyToManyField(Tag, blank=True)
student_descriptor = models.CharField(max_length=255, blank=True, null=True)

class QuestionFile(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
file = models.FileField(upload_to="question_files/")

# Delete file on delete of QuestionFile object
# @receiver(models.signals.post_delete, sender=QuestionFile)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""

Deletes file from filesystem
when corresponding `QuestionFile` object is deleted.
"""
...
# if instance.file:
# if os.path.isfile(instance.file.path):
# os.remove(instance.file.path)

class CourseStatistic(models.Model):
"""
Expand Down
2 changes: 1 addition & 1 deletion backend/ohq/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@


websocket_urlpatterns = [
path("api/ws/subscribe/", realtime_router.as_consumer(), name="subscriptions"),
path("api/ws/subscribe/", realtime_router.as_consumer().as_asgi(), name="subscriptions"),
]
17 changes: 17 additions & 0 deletions backend/ohq/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MembershipInvite,
Profile,
Question,
QuestionFile,
Queue,
QueueStatistic,
Semester,
Expand Down Expand Up @@ -214,6 +215,7 @@ class QuestionSerializer(QueueRouteMixin):
responded_to_by = UserSerializer(read_only=True)
tags = TagSerializer(many=True)
position = serializers.IntegerField(default=-1, read_only=True)
files = serializers.ListField(child=serializers.CharField(), read_only=True, required=False)

class Meta:
model = Question
Expand All @@ -234,6 +236,7 @@ class Meta:
"resolved_note",
"position",
"student_descriptor",
"files",
)
read_only_fields = (
"time_asked",
Expand All @@ -244,6 +247,7 @@ class Meta:
"should_send_up_soon_notification",
"resolved_note",
"position",
"files"
)

def update(self, instance, validated_data):
Expand Down Expand Up @@ -294,6 +298,9 @@ def update(self, instance, validated_data):
raise serializers.ValidationError(
detail={"detail": "Students can only withdraw a question"}
)
# if "file" in validated_data:
# new_question_file = QuestionFile.objects.create(question=instance, file=validated_data["file"])
# instance.file = new_question_file
if "text" in validated_data:
instance.text = validated_data["text"]
if "video_chat_url" in validated_data:
Expand Down Expand Up @@ -327,6 +334,7 @@ def update(self, instance, validated_data):

def create(self, validated_data):
tags = validated_data.pop("tags")
file = validated_data.pop("file", None)
queue = Queue.objects.get(pk=self.context["view"].kwargs["queue_pk"])
questions_ahead = Question.objects.filter(
queue=queue, status=Question.STATUS_ASKED, time_asked__lt=timezone.now()
Expand All @@ -335,13 +343,22 @@ def create(self, validated_data):
validated_data["status"] = Question.STATUS_ASKED
validated_data["asked_by"] = self.context["request"].user
question = super().create(validated_data)
if file:
QuestionFile.objects.create(question=question, file=file)
for tag_data in tags:
try:
tag = Tag.objects.get(course=queue.course, **tag_data)
question.tags.add(tag)
except ObjectDoesNotExist:
continue
return question

def to_representation(self, instance):
representation = super().to_representation(instance)
question_files = QuestionFile.objects.filter(question=instance).all()
question_file_ids = [question_file.id for question_file in question_files]
representation["files"] = question_file_ids
return representation


class MembershipPrivateSerializer(CourseRouteMixin):
Expand Down
8 changes: 4 additions & 4 deletions backend/ohq/statistics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.auth import get_user_model
from django.db.models import Avg, Case, Count, F, Sum, When
from django.db.models import Avg, Case, Count, F, Sum, When, FloatField, ExpressionWrapper
from django.db.models.functions import TruncDate
from django.utils import timezone

Expand Down Expand Up @@ -217,10 +217,10 @@ def queue_calculate_questions_per_ta_heatmap(queue, weekday, hour):
questions=Count("date", distinct=False),
tas=Count("responded_to_by", distinct=True),
)
.annotate(
q_per_ta=Case(When(tas=0, then=F("questions")), default=1.0 * F("questions") / F("tas"))
.annotate(
q_per_ta=Case(When(tas=0, then=ExpressionWrapper(F("questions"), output_field=FloatField())), default=1.0 * F("questions") / F("tas")),
)
.aggregate(avg=Avg(F("q_per_ta")))
.aggregate(avg=Avg(F("q_per_ta"), output_field=FloatField()))
)

statistic = interval_stats["avg"]
Expand Down
Loading
Loading