def home(request): """view for the homepage of the Product DB :param request: :return: """ if login_required_if_login_only_mode(request): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) # if the user is a super user, send a message if no backend worker process is running if request.user.is_authenticated and request.user.is_superuser: if not is_worker_active(): messages.add_message( request, level=messages.ERROR, message="No backend worker process is running on the server. Please check the state of the application." ) today_date = datetime.now().date() context = cache.get(HOMEPAGE_CONTEXT_CACHE_KEY) if not context: all_products_query = Product.objects.all() context = { "recent_events": NotificationMessage.objects.filter( created__gte=datetime.now(get_current_timezone()) - timedelta(days=30) ).order_by('-created')[:5], "vendors": [x.name for x in Vendor.objects.all() if x.name != "unassigned"], "product_count": all_products_query.count(), "product_lifecycle_count": all_products_query.filter(eox_update_time_stamp__isnull=False).count(), "product_no_eol_announcement_count": all_products_query.filter( eox_update_time_stamp__isnull=False, eol_ext_announcement_date__isnull=True ).count(), "product_eol_announcement_count": all_products_query.filter( eol_ext_announcement_date__isnull=False, end_of_sale_date__gt=today_date ).count(), "product_eos_count": all_products_query.filter( Q(end_of_sale_date__lte=today_date, end_of_support_date__gt=today_date)| Q(end_of_sale_date__lte=today_date, end_of_support_date__isnull=True) ).count(), "product_eol_count": all_products_query.filter( end_of_support_date__lte=today_date ).count(), "product_price_count": all_products_query.filter(list_price__isnull=False).count(), } cache.set(HOMEPAGE_CONTEXT_CACHE_KEY, context, timeout=60*10) context.update({ "TB_HOMEPAGE_TEXT_BEFORE_FAVORITE_ACTIONS": TextBlock.objects.filter(name=TextBlock.TB_HOMEPAGE_TEXT_BEFORE_FAVORITE_ACTIONS).first(), "TB_HOMEPAGE_TEXT_AFTER_FAVORITE_ACTIONS": TextBlock.objects.filter(name=TextBlock.TB_HOMEPAGE_TEXT_AFTER_FAVORITE_ACTIONS).first(), }) return render(request, "productdb/home.html", context=context)
def status(request): """ Status page for the Product Database """ app_config = AppSettings() is_cisco_api_enabled = app_config.is_cisco_api_enabled() context = { "is_cisco_api_enabled": is_cisco_api_enabled } if is_cisco_api_enabled: # test access (once every 30 minutes) cisco_eox_api_test_successful = cache.get("CISCO_EOX_API_TEST", False) # defaults, overwritten if an exception is thrown cisco_eox_api_available = True cisco_eox_api_message = "successful connected to the Cisco EoX API" if not cisco_eox_api_test_successful: try: result = utils.check_cisco_eox_api_access(client_id=app_config.get_cisco_api_client_id(), client_secret=app_config.get_cisco_api_client_secret(), drop_credentials=False) cache.set("CISCO_EOX_API_TEST", result, 60 * 30) except Exception as ex: cisco_eox_api_available = True cisco_eox_api_message = str(ex) context["cisco_eox_api_available"] = cisco_eox_api_available context["cisco_eox_api_message"] = cisco_eox_api_message # determine worker status state = celery.is_worker_active() if state and not settings.DEBUG: worker_status = """ <div class="alert alert-success" role="alert"> <span class="fa fa-info-circle"></span> Backend worker found. </div>""" else: worker_status = """ <div class="alert alert-danger" role="alert"> <span class="fa fa-exclamation-circle"></span> No backend worker found, asynchronous and scheduled tasks are not executed. </div>""" context['worker_status'] = mark_safe(worker_status) return render(request, "config/status.html", context=context)
def get_celery_worker_state_html(): state = celery.is_worker_active() if state: worker_status = """ <div class="alert alert-success" role="alert"> <span class="fa fa-info-circle"></span> Backend worker found. </div>""" else: worker_status = """ <div class="alert alert-danger" role="alert"> <span class="fa fa-exclamation-circle"></span> No backend worker found, asynchronous and scheduled tasks are not executed. </div>""" return worker_status
def create_product_check(request): """ create a Product Check and schedule task :param request: :return: """ if login_required_if_login_only_mode(request): return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path)) if request.method == "POST": form = ProductCheckForm(request.POST) if form.is_valid(): if form.cleaned_data["public_product_check"]: form.instance.create_user = None form.save() # dispatch task eta = now() + timedelta(seconds=3) task = tasks.perform_product_check.apply_async( eta=eta, args=(form.instance.id, )) set_meta_data_for_task( task_id=task.id, title="Product check", auto_redirect=True, redirect_to=reverse( "productdb:detail-product_check", kwargs={"product_check_id": form.instance.id})) logger.info("create product check with ID %d on task %s" % (form.instance.id, task.id)) return redirect( reverse("task_in_progress", kwargs={"task_id": task.id})) else: form = ProductCheckForm( initial={ "create_user": request.user.id, "public_product_check": False if request.user.id else True }) # if user is not logged in, create always a public check if not request.user.id: form.fields["public_product_check"].widget.attrs['disabled'] = True choose_migration_source = request.user.profile.choose_migration_source if request.user.is_authenticated( ) else False worker_is_active = is_worker_active() if getattr(settings, "CELERY_ALWAYS_EAGER", False): # if celery always eager is enabled, it works without worker worker_is_active = True return render(request, "productdb/product_check/create-product_check.html", context={ "form": form, "choose_migration_source": choose_migration_source, "worker_is_active": worker_is_active })