celery & attributes list by product
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
from .celery import app as celery_app
|
||||
|
||||
__all__ = ('celery_app',)
|
||||
44
Rasaddam_Backend/celery.py
Normal file
44
Rasaddam_Backend/celery.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
import os
|
||||
from celery.schedules import crontab
|
||||
from celery import Celery
|
||||
import django
|
||||
from django.conf import settings
|
||||
|
||||
# Set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Rasaddam_Backend.settings")
|
||||
|
||||
django.setup()
|
||||
|
||||
app = Celery("Rasaddam_Backend")
|
||||
|
||||
# Using a string here means the worker doesn't have to serialize
|
||||
# the configuration object to child processes.
|
||||
# - namespace='CELERY' means all celery-related configuration keys
|
||||
# should have a `CELERY_` prefix.
|
||||
app.config_from_object("django.conf:settings", namespace="CELERY")
|
||||
|
||||
"""
|
||||
celery beat conf
|
||||
"""
|
||||
app.conf.beat_schedule = {}
|
||||
|
||||
# Load task modules from all registered Django apps.
|
||||
app.autodiscover_tasks()
|
||||
|
||||
|
||||
@app.task(bind=True, ignore_result=True)
|
||||
def debug_task(self):
|
||||
print(f"Request: {self.request!r}")
|
||||
|
||||
|
||||
"""
|
||||
celery beat conf
|
||||
"""
|
||||
app.conf.beat_schedule = {
|
||||
'add-every-30-seconds': {
|
||||
'task': 'apps.authentication.tasks.task_func',
|
||||
'schedule': 5.0,
|
||||
# 'args': (16, 16),
|
||||
},
|
||||
}
|
||||
@@ -86,7 +86,9 @@ INSTALLED_APPS = [
|
||||
'apps.product.apps.ProductConfig',
|
||||
'rest_captcha',
|
||||
'captcha',
|
||||
'drf_yasg'
|
||||
'drf_yasg',
|
||||
"django_celery_results",
|
||||
"django_celery_beat",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -162,6 +164,17 @@ SWAGGER_SETTINGS = {
|
||||
"DEFAULT_AUTO_SCHEMA_CLASS": "drf_yasg.inspectors.SwaggerAutoSchema"
|
||||
}
|
||||
|
||||
CELERY_BROKER_URL = "redis://:ydnW4hwzuDRYcTX3FWCHgQ1f@apo.liara.cloud:33740/0" # Requires Redis server
|
||||
accept_content = ["application/json"]
|
||||
result_serializer = "json"
|
||||
task_serializer = "json"
|
||||
timezone = "UTC"
|
||||
CELERY_RESULT_BACKEND = "redis://:ydnW4hwzuDRYcTX3FWCHgQ1f@apo.liara.cloud:33740/0"
|
||||
CELERY_CACHE_BACKEND = 'default'
|
||||
|
||||
# Celery Beat settings
|
||||
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
|
||||
|
||||
LOGIN_URL = 'rest_framework:login'
|
||||
LOGOUT_URL = 'rest_framework:logout'
|
||||
|
||||
|
||||
12
apps/authentication/tasks.py
Normal file
12
apps/authentication/tasks.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from celery import shared_task
|
||||
from apps.product.models import SaleUnit
|
||||
|
||||
|
||||
@shared_task(bind=True)
|
||||
def task_func(self):
|
||||
print("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss")
|
||||
sale_unit = SaleUnit.objects.get(id=1)
|
||||
print(sale_unit.unit)
|
||||
sale_unit.trash = True
|
||||
sale_unit.save()
|
||||
return 'Done'
|
||||
@@ -28,12 +28,12 @@ class BaseModel(models.Model):
|
||||
abstract = True
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
user = get_current_user() # get user object
|
||||
self.modified_by = user
|
||||
if not self.creator_info:
|
||||
self.created_by = user
|
||||
self.creator_info = user.first_name + ' ' + user.last_name
|
||||
self.modifier_info = user.first_name + ' ' + user.last_name
|
||||
# user = get_current_user() # get user object
|
||||
# self.modified_by = user
|
||||
# if not self.creator_info:
|
||||
# self.created_by = user
|
||||
# self.creator_info = user.first_name + ' ' + user.last_name
|
||||
# self.modifier_info = user.first_name + ' ' + user.last_name
|
||||
super(BaseModel, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||
if quota.is_closed:
|
||||
raise QuotaClosedException()
|
||||
|
||||
# check if assigned organization is in quota limitation
|
||||
if assigned_organization not in quota.limit_by_organizations.all():
|
||||
raise QuotaLimitByOrganizationException()
|
||||
|
||||
|
||||
@@ -118,11 +118,26 @@ class ProductViewSet(viewsets.ModelViewSet):
|
||||
class AttributeViewSet(viewsets.ModelViewSet):
|
||||
""" attributes of reference product """ #
|
||||
|
||||
queryset = product_models.Attribute.objects.all()
|
||||
queryset = product_models.Attribute.objects.select_related('product').all()
|
||||
serializer_class = product_serializers.AttributeSerializer
|
||||
filter_backends = [filters.SearchFilter]
|
||||
search_fields = ['name']
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=True,
|
||||
url_path='by_product',
|
||||
url_name='by_product',
|
||||
name='by_product'
|
||||
)
|
||||
def get_attributes_by_product(self, request, pk=None):
|
||||
""" get attributes by product with global ones """
|
||||
|
||||
attributes = self.queryset.filter(Q(is_global=True) | Q(product__id=pk))
|
||||
|
||||
serializer = self.serializer_class(attributes, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
|
||||
Reference in New Issue
Block a user