def get_instance_display(self) -> Optional[dp.InstanceDisplay]: return dp.InstanceDisplay( sections=tuple([ dp.Section( fields=dp.FieldSet( fields=tuple([ dp.FieldSet(fields=tuple(["image_field", tuple(["char_field", tuple(["integer_field", "float_field"])]),])), "tags", "text_field", "percent_field", "decimal_field", "datetime_field", "datetime_field1", "date_field", "time_field", "boolean_field", "choice_field", "status_field", "file_field", "star_rating", ]) ) ), dp.Section(title="Related Models", section_list=dp.SectionList(key="related_model")), ]) )
def get_instance_display(self) -> Optional[dp.InstanceDisplay]: return dp.InstanceDisplay(sections=(dp.Section(fields=dp.FieldSet( fields=( dp.FieldSet(fields=("timestamp_created", "timestamp_received")), dp.FieldSet(fields=("timestamp_received", "timestamp_mailed")), "title", "message", ))), ))
class PriceModelViewSet(viewsets.ModelViewSet): ENDPOINT = 'djangoapp:price-list' IDENTIFIER = 'djangoapp:price' INSTANCE_TITLE = "Price : {{price}} / Stock : {{_stock.symbol}}" LIST_TITLE = "Prices" CREATE_TITLE = "New Price" INSTANCE_DISPLAY = dp.InstanceDisplay(sections=[ dp.Section(fields=dp.FieldSet(fields=[ dp.FieldSet(fields=["stock"]), dp.FieldSet(fields=["price", "date", "datetime"]), dp.FieldSet(fields=["open_price", "high_price", "low_price"]), dp.FieldSet(fields=["volume"]), ])) ]) LIST_DISPLAY = dp.ListDisplay(fields=[ dp.Field(key="stock", label="Symbol"), dp.Field(key="price", label="Price"), dp.Field(key="date", label="Date"), dp.Field(key="datetime", label="Datetime"), ], ) queryset = Price.objects.all() serializer_class = PriceModelSerializer filter_backends = [ filters.OrderingFilter, filters.SearchFilter, DjangoFilterBackend ] ordering_fields = ['stock', 'price', 'date', 'datetime'] ordering = ['datetime', 'stock'] search_fields = ["stock", "price"] filterset_fields = { "stock": ["exact"], "price": ["exact", "icontains"], "date": ["gte", "lte"], "datetime": ["gte", "lte"] } def get_aggregates(self, queryset, **kwargs): return { "stock": { "#": format_number(queryset.count()) }, } def get_serializer_changes(self, serializer): pk = self.kwargs.get("pk", None) if hasattr(serializer, "fields") and pk: #if getattr(self.) serializer.fields["price"] = wb_serializers.FloatField( read_only=True) return serializer
class RelatedModelTestButtonConfig(ButtonConfig): FSM_DROPDOWN = True CUSTOM_INSTANCE_BUTTONS = CUSTOM_LIST_INSTANCE_BUTTONS = { bt.DropDownButton( label="Dropdown", icon="wb-icon-triangle-down", buttons=(bt.DropDownButton( label="Dropdown", icon="wb-icon-triangle-down", buttons=(bt.ActionButton( label="TestButton", icon="wb-icon-trash", endpoint="http://localhost:5000/relatedmodeltest/", instance_display=dp.InstanceDisplay(sections=(dp.Section( fields=dp.FieldSet(fields=("char_field", "custom_field"))), )), serializer=ActionButtonSerializer, ), ), ), ), ), bt.HyperlinkButton(key="html", icon="wb-icon-trash", label="Authenticated Subpage", weight=1000), } CUSTOM_BUTTONS = { bt.ActionButton( label="TestButton", icon="wb-icon-trash", endpoint="http://localhost:5000/relatedmodeltest/", instance_display=dp.InstanceDisplay(sections=(dp.Section( fields=dp.FieldSet(fields=("char_field", "custom_field"))), )), serializer=ActionButtonSerializer, ) }
def get_display(self): return dp.InstanceDisplay(sections=tuple([ dp.Section(fields=dp.FieldSet( fields=tuple(["image_field", "char_field"]))) ]))
class StockModelViewSet(viewsets.ModelViewSet): DELETE_ENDPOINT = None ENDPOINT = 'djangoapp:stock-list' IDENTIFIER = "djangoapp:stock" INSTANCE_TITLE = "Stock : {{symbol}}" LIST_TITLE = "Stocks" CREATE_TITLE = "New Stock" LIST_DISPLAY = dp.ListDisplay( fields=[dp.Field(key="symbol", label="Symbol")], ) INSTANCE_DISPLAY = dp.InstanceDisplay(sections=[ dp.Section(fields=dp.FieldSet(fields=["symbol"])), dp.Section(title="Prices", collapsed=True, section_list=dp.SectionList(key="prices")), ]) CUSTOM_LIST_INSTANCE_BUTTONS = CUSTOM_INSTANCE_BUTTONS = [ bt.DropDownButton( label="Quick Action", icon=WBIcon.TRIANGLE_DOWN.value, buttons=[ bt.WidgetButton(key="prices", label="Prices", icon=WBIcon.DOLLAR.value), bt.ActionButton( method=RequestType.PATCH, identifiers=["djangoapp:price"], action_label="Modify Prices", key="modifyprices", title="Modify the Prices of a stock", label="Modify Prices", icon=WBIcon.CIRCLE_NO.value, description_fields= "<p> Do you want to modify the prices of {{symbol}}? </p>", serializer=MultiplyPricesActionButtonSerializer, confirm_config=bt.ButtonConfig(label="Confirm"), cancel_config=bt.ButtonConfig(label="Cancel"), instance_display=dp.InstanceDisplay(sections=[ dp.Section(fields=dp.FieldSet( fields=["number_product"])) ])), bt.WidgetButton(key="chartprices", label="Prices Chart", icon=WBIcon.STATS.value), bt.HyperlinkButton(endpoint="https://www.alphavantage.co/", label="AlphaVantage", icon=WBIcon.BANK.value), ]) ] @action(methods=["PATCH"], permission_classes=[IsAuthenticated], detail=True) def modifyprices(self, request, pk=None): number_product = float(request.POST.get("number_product", 1)) nbprice = Price.objects.filter(stock__id=pk).update(price=F('price') * number_product) stock = Stock.objects.get(pk=pk) #print("Stock: " +str(stock)+ " -> nb of price: " + str(nbprice)) if nbprice > 0: #print("Stock: " + str(stock) + " -> successful modify stock -> price multiplied by " + str(number_product)) Notification.objects.create( title=f'Stock: {stock.symbol} Modify Prices', message= f'successful You have multiplied the prices of stock: {stock.symbol} -> price multiplied by ({number_product})', send_type=NotificationSendType.SYSTEM.value, recipient=request.user) return Response( { "__notification": { stock.symbol: "successful modify stock", 'updated': True, "number_product": number_product } }, status=status.HTTP_200_OK) queryset = Stock.objects.all() serializer_class = StockModelSerializer filter_backends = [ filters.OrderingFilter, filters.SearchFilter, DjangoFilterBackend ] ordering_fields = ['symbol'] ordering = ['symbol'] search_fields = ("symbol", ) filterset_fields = {"symbol": ["exact", "icontains"]} def get_aggregates(self, queryset, **kwargs): return { "symbol": { "#": format_number(queryset.count()) }, }
def get_instance_display(self) -> Optional[dp.InstanceDisplay]: return dp.InstanceDisplay(sections=(dp.Section(fields=dp.FieldSet( fields=("char_field", "tags", "model_test", "model_tests", "text_markdown"))), ))
class UserViewSet(ModelViewSet): INSTANCE_DISPLAY = dp.InstanceDisplay(sections=(dp.Section(fields=dp.FieldSet(fields=("username", "email"))),)) serializer_class = UserSerializer queryset = get_user_model().objects.all()