Skip to content
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
37 changes: 37 additions & 0 deletions CPorBit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,40 @@
## Frontend domain (used for mails)
DOMAIN = env.str('DOMAIN')
SITE_NAME = env.str('SITE_NAME')

# logger configuration

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

LOGGING = {
'version': 1,
'loggers': {
'django': {
'handlers': ['file', 'fileinfo'],
'level': 'INFO',
'propagate': True,
},
},
'handlers': {
'file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'Errors.log'),
'formatter': 'detailed',
},
'fileinfo': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': os.path.join(BASE_DIR, 'logs', 'Infos.log'),
'formatter': 'detailed',
},
},
'formatters': {
'detailed': {
'format': '\n{levelname} {asctime} {message}',
'style': '{',
},
}
}
19 changes: 16 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ RUN apk add --no-cache \
libffi-dev \
musl-dev \
make \
openblas-dev
openblas-dev \
bash \
dos2unix

ENV BLAS=OPENBLAS \
LAPACK=OPENBLAS
Expand All @@ -22,9 +24,20 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . .

RUN chmod +x ./server.sh
COPY server.sh /app/server.sh

RUN ./server.sh
# Fix Windows line endings and make the script executable
RUN dos2unix /app/server.sh
RUN chmod +x /app/server.sh

# Create staticfiles directory
RUN mkdir -p /app/staticfiles

# Ensure logs directory and files exist
RUN mkdir -p /app/logs && touch /app/logs/Errors.log /app/logs/Infos.log

# Run the script
RUN /bin/bash /app/server.sh

EXPOSE 8000

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
image: nginx:alpine
container_name: proxy
ports:
- "80:80"
- "8080:80"
volumes:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Nginx should run on docker port 80 without any errors.

- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./public/staticfiles:/staticfiles
Expand Down
18 changes: 18 additions & 0 deletions gamification_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)

from re import sub

from django.contrib.auth.models import User
Expand Down Expand Up @@ -37,6 +40,9 @@ def get_user_score(request,username):

except User.DoesNotExist:
return Response({'message': 'User not found'} ,status=404)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting user score.'})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, you can raise the exception without having to return a 500 error. However, that's optional.



@api_view(['GET'])
Expand Down Expand Up @@ -73,6 +79,9 @@ def get_user_stats(request, username):

except User.DoesNotExist:
return Response({'message': 'User not found'} ,status=404)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting user stats.'})


@api_view(['GET'])
Expand All @@ -85,6 +94,9 @@ def get_user_strength(request, username):
return Response(strengths[:5], status=200)
except User.DoesNotExist:
return Response({"message": "User not found"}, status=404)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting user strength.'})


@api_view(['GET'])
Expand All @@ -97,6 +109,9 @@ def get_user_weakness(request, username):
return Response(weaknesses[-5:], status=200)
except User.DoesNotExist:
return Response({"message": "User not found"}, status=404)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting user weakness.'})


@api_view(['GET'])
Expand All @@ -117,3 +132,6 @@ def get_hardest_problem_solved(request, username):
return Response(problem, status=200)
except User.DoesNotExist:
return Response({"message": "User not found"}, status=404)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting user hardest solved problem.'})
57 changes: 40 additions & 17 deletions notification_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)

from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
Expand All @@ -17,35 +20,55 @@ class NotificationViewSet(viewsets.ModelViewSet):
allowed_methods = ['GET', 'PUT', 'DELETE']

def get_queryset(self):
notifications = Notification.objects.filter(user=self.request.user).order_by('-id')
self.unread = notifications.filter(is_read=False).count()
return notifications[:5]
try:
notifications = Notification.objects.filter(user=self.request.user).order_by('-id')
self.unread = notifications.filter(is_read=False).count()
return notifications[:5]
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting notification data.'})

def get_paginated_response(self, data):
return self.paginator.get_paginated_response(data, self.unread)

@action(detail=False, methods=['GET'])
def all(self, request):
notifications = Notification.objects.filter(user=self.request.user)
serializer = NotificationSerializer(notifications, many=True)
return Response(serializer.data)
try:
notifications = Notification.objects.filter(user=self.request.user)
serializer = NotificationSerializer(notifications, many=True)
return Response(serializer.data)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting all notification data.'})

@action(detail=True, methods=['PUT'])
def mark_as_read(self, request, pk=None):
if pk:
notification = Notification.objects.get(id=pk)
notification.is_read = True
notification.save()
return Response(status=200)
try:
if pk:
notification = Notification.objects.get(id=pk)
notification.is_read = True
notification.save()
return Response(status=200)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while marking notification as read.'})

@action(detail=False, methods=['PUT'])
def mark_all_as_read(self, request):
notifications = Notification.objects.filter(user=self.request.user)
notifications.update(is_read=True)
return Response(status=200)
try:
notifications = Notification.objects.filter(user=self.request.user)
notifications.update(is_read=True)
return Response(status=200)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while marking all notification as read.'})

@action(detail=False, methods=['DELETE'])
def clear_all(self, request):
notifications = Notification.objects.filter(user=self.request.user)
notifications.delete()
return Response(status=200)
try:
notifications = Notification.objects.filter(user=self.request.user)
notifications.delete()
return Response(status=200)
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while clearing all notification.'})
137 changes: 74 additions & 63 deletions problem_app/problem_urls/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)

from problem_app.models import AcceptedSubmission, Problem
from problem_app.serializers.ProblemSerializer import (ProblemListSerializer,
ProblemSerializer)
Expand All @@ -16,78 +19,86 @@ def get_score(self, problem):
return problem.get('score')

def get(self, request):
orderby = request.GET.get('order')
tag = request.GET.get('tag')
score_from = request.GET.get('from')
score_to = request.GET.get('to')
problem_id = request.GET.get('id')
problem_name = request.GET.get('name')
limit = request.GET.get('limit')

if problem_id is None:
problem_id = ''
if problem_name is None:
problem_name = ''

try:
score_min = int(score_from)
except:
score_min = 0
try:
score_max = int(score_to)
except:
score_max = 4000

queryset = []

if tag:
queryset = Problem.objects.filter(cf_problem_id__contains = problem_id, cf_problem_name__contains = problem_name, tag__name=tag, score__range=(score_min, score_max))
else:
queryset = Problem.objects.filter(cf_problem_id__contains = problem_id, cf_problem_name__contains = problem_name, score__range=(score_min, score_max))

serializer = ProblemListSerializer(queryset, many = True, context={'username': request.user.username})

problemlist = []

if orderby == 'SOLVED_ASC':
problemlist.extend(sorted(serializer.data, key=self.get_total_solved))
elif orderby == 'SOLVED_DESC':
problemlist.extend(sorted(serializer.data, key=self.get_total_solved, reverse=True))
elif orderby == 'SCORE_ASC':
problemlist.extend(sorted(serializer.data, key=self.get_score))
elif orderby == 'SCORE_DESC':
problemlist.extend(sorted(serializer.data, key=self.get_score, reverse=True))
else:
problemlist.extend(serializer.data)

if limit:
orderby = request.GET.get('order')
tag = request.GET.get('tag')
score_from = request.GET.get('from')
score_to = request.GET.get('to')
problem_id = request.GET.get('id')
problem_name = request.GET.get('name')
limit = request.GET.get('limit')

if problem_id is None:
problem_id = ''
if problem_name is None:
problem_name = ''

try:
score_min = int(score_from)
except:
score_min = 0
try:
problemlist = problemlist[:int(limit)]
score_max = int(score_to)
except:
score_max = 4000

queryset = []

if tag:
queryset = Problem.objects.filter(cf_problem_id__contains = problem_id, cf_problem_name__contains = problem_name, tag__name=tag, score__range=(score_min, score_max))
else:
queryset = Problem.objects.filter(cf_problem_id__contains = problem_id, cf_problem_name__contains = problem_name, score__range=(score_min, score_max))

except Exception:
return Response({'error': 'Invalid limit'}, status=400)
serializer = ProblemListSerializer(queryset, many = True, context={'username': request.user.username})

return Response({
'status': 'OK',
'problems': problemlist
})
problemlist = []

if orderby == 'SOLVED_ASC':
problemlist.extend(sorted(serializer.data, key=self.get_total_solved))
elif orderby == 'SOLVED_DESC':
problemlist.extend(sorted(serializer.data, key=self.get_total_solved, reverse=True))
elif orderby == 'SCORE_ASC':
problemlist.extend(sorted(serializer.data, key=self.get_score))
elif orderby == 'SCORE_DESC':
problemlist.extend(sorted(serializer.data, key=self.get_score, reverse=True))
else:
problemlist.extend(serializer.data)

if limit:
try:
problemlist = problemlist[:int(limit)]

except Exception as ex:
logger.exception(ex)
return Response({'error': 'Invalid limit'}, status=400)

return Response({
'status': 'OK',
'problems': problemlist
})
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting problem list.'})


class ProblemAV(APIView):
permission_classes = [IsAuthenticated]

def get(self, request, cf_problem_id):
try:
queryset = Problem.objects.get(cf_problem_id = cf_problem_id)
except Exception as e:
print(e)
print('Problem not found')
try:
queryset = Problem.objects.get(cf_problem_id = cf_problem_id)
except Exception as ex:
logger.exception(ex)
return Response({
'status': 'FAILED',
'message': 'Problem not found'
}, status=404)
serializer = ProblemSerializer(queryset, context={'username': request.user.username})
return Response({
'status': 'FAILED',
'message': 'Problem not found'
}, status=404)
serializer = ProblemSerializer(queryset, context={'username': request.user.username})
return Response({
'status': 'OK',
'problem': serializer.data
})
'status': 'OK',
'problem': serializer.data
})
except Exception as ex:
logger.exception(ex)
return Response(status=500, data={'message': 'Some error occurred while getting problem.'})
Loading