mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 13:49:40 +00:00
18
account/migrations/0009_alter_userdevicedetails_device_id.py
Normal file
18
account/migrations/0009_alter_userdevicedetails_device_id.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-20 11:38
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0008_userdevicedetails'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='userdevicedetails',
|
||||
name='device_id',
|
||||
field=models.CharField(blank=True, max_length=500, null=True),
|
||||
),
|
||||
]
|
@ -152,7 +152,7 @@ class UserDeviceDetails(models.Model):
|
||||
"""
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_device_details')
|
||||
"""Device ID"""
|
||||
device_id = models.CharField(max_length=500)
|
||||
device_id = models.CharField(max_length=500, null=True, blank=True)
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
@ -96,3 +96,10 @@ BYTE_IMAGE_SIZE = 1024
|
||||
|
||||
# validate file size
|
||||
MAX_FILE_SIZE = 1024 * 1024 * 5
|
||||
|
||||
ARTICLE_SURVEY_POINTS = 5
|
||||
MAX_ARTICLE_CARD = 6
|
||||
|
||||
# min and max survey
|
||||
MIN_ARTICLE_SURVEY = 5
|
||||
MAX_ARTICLE_SURVEY = 10
|
||||
|
@ -3,12 +3,12 @@
|
||||
import oss2
|
||||
"""Import setting"""
|
||||
from django.conf import settings
|
||||
import logging
|
||||
import requests
|
||||
"""Import tempfile"""
|
||||
import tempfile
|
||||
# Import date time module's function
|
||||
from datetime import datetime, time
|
||||
# Import real time client module
|
||||
import ntplib
|
||||
# import Number constant
|
||||
from base.constants import NUMBER
|
||||
# Import Junior's model
|
||||
@ -44,16 +44,19 @@ def upload_image_to_alibaba(image, filename):
|
||||
new_filename = filename.replace(' ', '%20')
|
||||
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
||||
|
||||
def real_time():
|
||||
""" real time """
|
||||
# Fetch real time.
|
||||
# time is not depend on system time
|
||||
# Get the current datetime
|
||||
ntp_client = ntplib.NTPClient()
|
||||
ntp_server = 'pool.ntp.org'
|
||||
response = ntp_client.request(ntp_server)
|
||||
current_datetime = datetime.fromtimestamp(response.tx_time)
|
||||
return current_datetime
|
||||
|
||||
def real_time(timezone='Asia/Riyadh'):
|
||||
url = f'http://worldtimeapi.org/api/timezone/{timezone}'
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
time_str = data['datetime']
|
||||
realtime = datetime.fromisoformat(time_str.replace('Z', '+00:00')).replace(tzinfo=None)
|
||||
return realtime
|
||||
else:
|
||||
logging.error("Could not fetch error")
|
||||
return None
|
||||
|
||||
|
||||
def convert_timedelta_into_datetime(time_difference):
|
||||
"""convert date time"""
|
||||
|
18
web_admin/migrations/0002_alter_articlecard_image.py
Normal file
18
web_admin/migrations/0002_alter_articlecard_image.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.2 on 2023-07-20 11:51
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('web_admin', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='articlecard',
|
||||
name='image',
|
||||
field=models.URLField(blank=True, default=None, null=True),
|
||||
),
|
||||
]
|
@ -28,7 +28,7 @@ class ArticleCard(models.Model):
|
||||
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='article_cards')
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField()
|
||||
image = models.ImageField(upload_to='card_images/')
|
||||
image = models.URLField(null=True, blank=True, default=None)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
@ -3,10 +3,11 @@ web_admin serializers file
|
||||
"""
|
||||
# django imports
|
||||
from rest_framework import serializers
|
||||
from django.conf import settings
|
||||
|
||||
from base.constants import ARTICLE_SURVEY_POINTS, MAX_ARTICLE_CARD, MIN_ARTICLE_SURVEY, MAX_ARTICLE_SURVEY
|
||||
# local imports
|
||||
from base.messages import ERROR_CODE
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from web_admin.models import Article, ArticleCard, SurveyOption, ArticleSurvey
|
||||
from web_admin.utils import pop_id
|
||||
|
||||
@ -16,14 +17,23 @@ class ArticleCardSerializer(serializers.ModelSerializer):
|
||||
Article Card serializer
|
||||
"""
|
||||
id = serializers.IntegerField(required=False)
|
||||
image = serializers.FileField(required=False)
|
||||
|
||||
class Meta:
|
||||
"""
|
||||
meta class
|
||||
"""
|
||||
model = ArticleCard
|
||||
fields = ('id', 'title', 'description')
|
||||
fields = ('id', 'title', 'description', 'image')
|
||||
|
||||
def create(self, validated_data):
|
||||
image = validated_data.get('image', '')
|
||||
filename = f"article/{image.name}"
|
||||
# upload image on ali baba
|
||||
validated_data['image'] = upload_image_to_alibaba(image, filename)
|
||||
|
||||
article_card = ArticleCard.objects.create(article_id='1', **validated_data)
|
||||
return article_card
|
||||
|
||||
class SurveyOptionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
@ -51,7 +61,7 @@ class ArticleSurveySerializer(serializers.ModelSerializer):
|
||||
meta class
|
||||
"""
|
||||
model = ArticleSurvey
|
||||
fields = ('id', 'question', 'points', 'survey_options')
|
||||
fields = ('id', 'question', 'survey_options')
|
||||
|
||||
|
||||
class ArticleSerializer(serializers.ModelSerializer):
|
||||
@ -76,10 +86,10 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
article_cards = attrs.get('article_cards', None)
|
||||
article_survey = attrs.get('article_survey', None)
|
||||
if article_cards is None or len(article_cards) > int(settings.MAX_ARTICLE_CARD):
|
||||
if article_cards is None or len(article_cards) > int(MAX_ARTICLE_CARD):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2039']})
|
||||
if article_survey is None or len(article_survey) < int(settings.MIN_ARTICLE_SURVEY) or int(
|
||||
settings.MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
if article_survey is None or len(article_survey) < int(MIN_ARTICLE_SURVEY) or int(
|
||||
MAX_ARTICLE_SURVEY) < len(article_survey):
|
||||
raise serializers.ValidationError({'details': ERROR_CODE['2040']})
|
||||
return attrs
|
||||
|
||||
@ -102,7 +112,7 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||
for survey in article_survey:
|
||||
survey = pop_id(survey)
|
||||
options = survey.pop('survey_options')
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, **survey)
|
||||
survey_obj = ArticleSurvey.objects.create(article=article, points=ARTICLE_SURVEY_POINTS, **survey)
|
||||
for option in options:
|
||||
option = pop_id(option)
|
||||
SurveyOption.objects.create(survey=survey_obj, **option)
|
||||
|
@ -13,7 +13,7 @@ from account.utils import custom_response, custom_error_response
|
||||
from base.messages import SUCCESS_CODE, ERROR_CODE
|
||||
from web_admin.models import Article, ArticleCard, ArticleSurvey
|
||||
from web_admin.permission import AdminPermission
|
||||
from web_admin.serializers import ArticleSerializer
|
||||
from web_admin.serializers import ArticleSerializer, ArticleCardSerializer
|
||||
|
||||
|
||||
class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModelMixin,
|
||||
@ -22,7 +22,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
article api
|
||||
"""
|
||||
serializer_class = ArticleSerializer
|
||||
permission_classes = [IsAuthenticated, AdminPermission]
|
||||
# permission_classes = [IsAuthenticated, AdminPermission]
|
||||
queryset = Article.objects.prefetch_related('article_cards',
|
||||
'article_survey',
|
||||
'article_survey__survey_options').order_by('-created_at')
|
||||
@ -140,3 +140,12 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
return custom_response(SUCCESS_CODE["3031"])
|
||||
except AttributeError:
|
||||
return custom_error_response(ERROR_CODE["2043"], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@action(methods=['post'], url_name='test-add-card', url_path='test-add-card',
|
||||
detail=False, serializer_class=ArticleCardSerializer)
|
||||
def add_card(self, request):
|
||||
print(request.data)
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
@ -265,9 +265,6 @@ ALIYUN_OSS_BUCKET_NAME = os.getenv('ALIYUN_OSS_BUCKET_NAME')
|
||||
ALIYUN_OSS_ENDPOINT = os.getenv('ALIYUN_OSS_ENDPOINT')
|
||||
ALIYUN_OSS_REGION = os.getenv('ALIYUN_OSS_REGION')
|
||||
|
||||
MAX_ARTICLE_CARD = os.getenv('MAX_ARTICLE_CARD', 6)
|
||||
MIN_ARTICLE_SURVEY = os.getenv('MIN_ARTICLE_SURVEY', 5)
|
||||
MAX_ARTICLE_SURVEY = os.getenv('MAX_ARTICLE_SURVEY', 10)
|
||||
|
||||
# define static url
|
||||
STATIC_URL = 'static/'
|
||||
|
Reference in New Issue
Block a user