adding event_comment and avail contract
This commit is contained in:
parent
23ffa71032
commit
907728fdfb
@ -1,10 +1,19 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from api.serializers.event_comment import EventCommentSerializer
|
||||
from api.services.generate_availability_contract import AvailabilityContractGenerator
|
||||
from web.models import Event
|
||||
|
||||
|
||||
class EventSerializer(serializers.ModelSerializer):
|
||||
|
||||
comments = EventCommentSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = Event
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
data = super(EventSerializer, self).to_representation(instance)
|
||||
data['availability_contract'] = AvailabilityContractGenerator(data['id']).main()
|
||||
return data
|
||||
|
10
api/serializers/event_comment.py
Normal file
10
api/serializers/event_comment.py
Normal file
@ -0,0 +1,10 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from web.models import EventComment
|
||||
|
||||
|
||||
class EventCommentSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = EventComment
|
||||
fields = '__all__'
|
10
api/serializers/event_division.py
Normal file
10
api/serializers/event_division.py
Normal file
@ -0,0 +1,10 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from web.models import EventDivision
|
||||
|
||||
|
||||
class EventDivisionSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = EventDivision
|
||||
fields = '__all__'
|
51
api/services/generate_availability_contract.py
Normal file
51
api/services/generate_availability_contract.py
Normal file
@ -0,0 +1,51 @@
|
||||
import copy
|
||||
from datetime import date, timedelta, time
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from web.models.event import Event
|
||||
|
||||
|
||||
DIVISION_MAP = {
|
||||
1: [time(i).strftime('%I %p') for i in range(24)],
|
||||
2: ["Morning", "Afternoon", "Night"]
|
||||
}
|
||||
|
||||
|
||||
class AvailabilityContractGenerator:
|
||||
def __init__(self, event_id):
|
||||
self.event_id = event_id
|
||||
self.event = get_object_or_404(Event, pk=event_id)
|
||||
|
||||
self.availability_contract = {
|
||||
'unavailable': False,
|
||||
'dates': {}
|
||||
}
|
||||
|
||||
def main(self):
|
||||
dates = self.generate_date_list()
|
||||
self.generate_availability_contract(dates)
|
||||
return self.availability_contract
|
||||
|
||||
def generate_date_list(self):
|
||||
start_date = copy.deepcopy(self.event.start_date)
|
||||
delta = timedelta(days=1)
|
||||
|
||||
dates = []
|
||||
|
||||
while start_date <= self.event.end_date:
|
||||
dates.append(start_date.isoformat())
|
||||
start_date += delta
|
||||
|
||||
return dates
|
||||
|
||||
def generate_availability_contract(self, dates):
|
||||
for event_date in dates:
|
||||
self.availability_contract['dates'][event_date] = {'divisions': []}
|
||||
for division in DIVISION_MAP.get(self.event.division.id):
|
||||
self.availability_contract['dates'][event_date]['divisions'].append(
|
||||
{
|
||||
"name": division,
|
||||
"status": None
|
||||
}
|
||||
)
|
40
api/views/availability.py
Normal file
40
api/views/availability.py
Normal file
@ -0,0 +1,40 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from api.serializers.event import EventSerializer
|
||||
from common.build_response import build_response
|
||||
from web.models import Event, EventComment
|
||||
|
||||
|
||||
class AvailabilityView(APIView):
|
||||
def post(self, request, event_id):
|
||||
try:
|
||||
event = get_object_or_404(Event, pk=event_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find related event!')
|
||||
|
||||
data = request.POST
|
||||
|
||||
try:
|
||||
user = User.objects.filter(email=data.get('created_by')).first()
|
||||
except Exception:
|
||||
return build_response(500, message="Could not find user!")
|
||||
|
||||
try:
|
||||
new_comment = EventComment(
|
||||
event=event,
|
||||
content=data.get('content'),
|
||||
submitted_by=user
|
||||
)
|
||||
|
||||
new_comment.save()
|
||||
except Exception:
|
||||
return build_response(500, message='Unable to create new comment!')
|
||||
|
||||
try:
|
||||
event_serializer = EventSerializer(event)
|
||||
except Exception:
|
||||
return build_response(422)
|
||||
|
||||
return build_response(data=event_serializer.data)
|
@ -57,7 +57,7 @@ class EventView(APIView):
|
||||
|
||||
return build_response(200, data=event_serializer.data)
|
||||
|
||||
def put(self,request, event_id):
|
||||
def put(self, request, event_id):
|
||||
try:
|
||||
event = get_object_or_404(Event, pk=event_id)
|
||||
except Exception:
|
||||
|
90
api/views/event_comment.py
Normal file
90
api/views/event_comment.py
Normal file
@ -0,0 +1,90 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from api.serializers.event import EventSerializer
|
||||
from common.build_response import build_response
|
||||
from web.models import Event, EventComment
|
||||
|
||||
|
||||
class EventCommentView(APIView):
|
||||
def post(self, request, event_id):
|
||||
try:
|
||||
event = get_object_or_404(Event, pk=event_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find related event!')
|
||||
|
||||
data = request.POST
|
||||
|
||||
try:
|
||||
user = User.objects.filter(email=data.get('created_by')).first()
|
||||
except Exception:
|
||||
return build_response(500, message="Could not find user!")
|
||||
|
||||
try:
|
||||
new_comment = EventComment(
|
||||
event=event,
|
||||
content=data.get('content'),
|
||||
submitted_by=user
|
||||
)
|
||||
|
||||
new_comment.save()
|
||||
except Exception:
|
||||
return build_response(500, message='Unable to create new comment!')
|
||||
|
||||
try:
|
||||
event_serializer = EventSerializer(event)
|
||||
except Exception:
|
||||
return build_response(422)
|
||||
|
||||
return build_response(data=event_serializer.data)
|
||||
|
||||
def put(self, request, event_id, comment_id):
|
||||
try:
|
||||
event = get_object_or_404(Event, pk=event_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find related event!')
|
||||
|
||||
data = request.POST
|
||||
|
||||
try:
|
||||
comment = get_object_or_404(EventComment, pk=comment_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find comment!')
|
||||
|
||||
try:
|
||||
comment.content = data.get('content', comment.content)
|
||||
comment.save()
|
||||
except Exception:
|
||||
return build_response(500, message='Unable to edit comment')
|
||||
|
||||
try:
|
||||
event_serializer = EventSerializer(event)
|
||||
except Exception:
|
||||
return build_response(422)
|
||||
|
||||
return build_response(data=event_serializer.data)
|
||||
|
||||
def delete(request, event_id, comment_id):
|
||||
try:
|
||||
event = get_object_or_404(Event, pk=event_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find related event!')
|
||||
|
||||
try:
|
||||
comment = get_object_or_404(EventComment, pk=comment_id)
|
||||
except Exception:
|
||||
return build_response(404, message='Unable to find comment!')
|
||||
|
||||
try:
|
||||
comment.active = False
|
||||
comment.save()
|
||||
except Exception:
|
||||
return build_response(500, message='Unable to delete comment!')
|
||||
|
||||
try:
|
||||
event_serializer = EventSerializer(event)
|
||||
except Exception:
|
||||
return build_response(422)
|
||||
|
||||
return build_response(data=event_serializer.data)
|
24
api/views/event_division.py
Normal file
24
api/views/event_division.py
Normal file
@ -0,0 +1,24 @@
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from api.serializers.event_division import EventDivisionSerializer
|
||||
from common.build_response import build_response
|
||||
from web.models import EventDivision
|
||||
|
||||
|
||||
class EventDivisionView(APIView):
|
||||
def get(self, request):
|
||||
try:
|
||||
event_divisions = EventDivision.objects.all()
|
||||
except Exception:
|
||||
return build_response(404)
|
||||
|
||||
try:
|
||||
parsed_event_divisions = []
|
||||
for event_division in event_divisions:
|
||||
parsed_event_divisions.append(
|
||||
EventDivisionSerializer(event_division).data
|
||||
)
|
||||
except Exception:
|
||||
return build_response(422)
|
||||
|
||||
return build_response(data=parsed_event_divisions)
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
@ -1,32 +1,21 @@
|
||||
"""
|
||||
URL configuration for korabo project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/5.1/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
from api.views.event import EventView
|
||||
from api.views.event_division import EventDivisionView
|
||||
from api.views.event_comment import EventCommentView
|
||||
from web import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/event/', EventView.as_view(), name='event'),
|
||||
path('api/event/<int:event_id>', EventView.as_view(), name='event'),
|
||||
path("accounts/", include("django.contrib.auth.urls")),
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/event/<int:event_id>/comment/', EventCommentView.as_view(), name='event'),
|
||||
path('api/event/<int:event_id>/comment/<int:comment_id>', EventCommentView.as_view(), name='event'),
|
||||
path('api/event_division/', EventDivisionView.as_view(), name='event_division'),
|
||||
path("", views.index, name="index"),
|
||||
path("accounts/", include("django.contrib.auth.urls")),
|
||||
path("event/<int:event_id>", views.event, name="event"),
|
||||
path('change_password/', views.change_password, name='change_password'),
|
||||
path('password_change_done/', views.password_change_done, name='password_change_done')
|
||||
|
@ -8,7 +8,7 @@ from web.models.event import Event
|
||||
|
||||
class EventComment(BaseModel):
|
||||
id = models.AutoField(primary_key=True)
|
||||
event = models.ForeignKey(Event, on_delete=models.CASCADE)
|
||||
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='comments')
|
||||
content = models.TextField(blank=False, null=False)
|
||||
submitted_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user