make creation and deletion work

This commit is contained in:
Tobias Brunner 2025-01-31 16:17:16 +01:00
parent 4ae237e10a
commit 7c7f315e52
No known key found for this signature in database
4 changed files with 28 additions and 5 deletions

View file

@ -4,7 +4,19 @@ from .views import ServiceBrokerViewSet
app_name = "broker" app_name = "broker"
router = DefaultRouter(trailing_slash=False)
class ServiceBrokerRouter(DefaultRouter):
def get_method_map(self, viewset, method_map):
result = super().get_method_map(viewset, method_map)
# Add PUT method for provision_instance
if hasattr(viewset, "provision_instance"):
result.update({"put": "provision_instance"})
return result
router = ServiceBrokerRouter(trailing_slash=False)
router.register("v2", ServiceBrokerViewSet, basename="broker") router.register("v2", ServiceBrokerViewSet, basename="broker")
urlpatterns = [ urlpatterns = [

View file

@ -1,9 +1,11 @@
import logging import logging
from django.urls import reverse
from django.conf import settings
from rest_framework import viewsets, status from rest_framework import viewsets, status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import action from rest_framework.decorators import action
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from hub.services.models import ServiceOffering, Lead from hub.services.models import Lead
from hub.services.odoo import OdooAPI from hub.services.odoo import OdooAPI
from .models import ServiceInstance, ServiceBrokerUser from .models import ServiceInstance, ServiceBrokerUser
from .serializers import ServiceInstanceProvisioningSerializer, CatalogSerializer from .serializers import ServiceInstanceProvisioningSerializer, CatalogSerializer
@ -52,7 +54,7 @@ class ServiceBrokerViewSet(viewsets.ViewSet):
try: try:
odoo = OdooAPI() odoo = OdooAPI()
lead.odoo_lead_id = odoo.create_lead(lead) lead.odoo_lead_id = odoo.create_lead(lead, source="osbapi")
lead.save() lead.save()
return lead return lead
except Exception as e: except Exception as e:
@ -110,7 +112,10 @@ class ServiceBrokerViewSet(viewsets.ViewSet):
return Response( return Response(
{ {
"dashboard_url": f"/services/{offering.service.slug}", "dashboard_url": settings.WEBSITE_URL
+ reverse(
"services:offering_detail", kwargs={"slug": offering.slug}
),
"operation": "provision", "operation": "provision",
}, },
status=status.HTTP_201_CREATED, status=status.HTTP_201_CREATED,

View file

@ -53,7 +53,7 @@ class OdooAPI:
logger.debug("Full error details:", exc_info=True) logger.debug("Full error details:", exc_info=True)
raise raise
def create_lead(self, lead): def create_lead(self, lead, source="form"):
"""Create a lead in Odoo""" """Create a lead in Odoo"""
try: try:
logger.info( logger.info(
@ -74,6 +74,11 @@ class OdooAPI:
if lead.plan: if lead.plan:
service_details.append(f"Plan: {lead.plan.name}") service_details.append(f"Plan: {lead.plan.name}")
if source == "form":
service_details.append(f"Source: Servala Website")
elif source == "osbapi":
service_details.append(f"Source: OSB API")
# Prepare lead data # Prepare lead data
lead_data = { lead_data = {
"name": f"Servala - Interest in {lead.service.name}", "name": f"Servala - Interest in {lead.service.name}",

View file

@ -43,6 +43,7 @@ DEBUG = env.bool("DEBUG", default=False)
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[]) ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])
CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS] CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS]
WEBSITE_URL = env.str("WEBSITE_URL", default="https://poc.serva.la")
# Application definition # Application definition