def get_list_display(self) -> Optional[dp.ListDisplay]: return dp.ListDisplay(fields=[ dp.Field(key="char_field", label="Char1"), dp.Field(key="model_test", label="Model"), dp.Field(key="model_tests", label="Model(M2M)"), dp.Field(key="text_markdown", label="Markdown"), dp.Field(key="tags", label="Tags"), ])
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 PriceStockModelViewSet(PriceModelViewSet): LIST_DISPLAY = dp.ListDisplay(fields=[ dp.Field(key="price", label="Price"), dp.Field(key="date", label="Date"), dp.Field(key="datetime", label="Datetime"), ], ) def get_endpoint(self, request, endpoint=None): return "djangoapp:stock-prices-list", [self.kwargs["stock_id"]], {} def get_list_title(self, request, field=None): stock = Stock.objects.get(id=self.kwargs["stock_id"]) return f'Prices for {stock.symbol}' def get_queryset(self): return super().get_queryset().filter(stock__id=self.kwargs["stock_id"])
class PricePandasModelViewSet(PandasAPIView): ENDPOINT = 'djangoapp:price-list' #ENDPOINT = None IDENTIFIER = 'djangoapp:price' #LIST_ENDPOINT = 'djangoapp:pricelist-list' metadata_class = PandasMetadata LIST_TITLE = 'Pandas Prices' 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"), ], ) pandas_fields = pf.PandasFields(fields=[ pf.PKField(key="id", label="ID"), pf.CharField(key="stock", label="stock"), pf.FloatField(key="price", label="Price", precision=2, percent=False), pf.CharField(key="date", label="Date"), pf.CharField(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, request, df): return { "stock": { "#": format_number(df.shape[0]) }, }
def get_list_display(self) -> Optional[dp.ListDisplay]: return dp.ListDisplay( fields=( dp.Field(key="title", label="Title"), dp.Field(key="timestamp_created", label="Created"), dp.Field(key="message", label="Message"), ), formatting=[ dp.Formatting( column="timestamp_read", formatting_rules=[ dp.FormattingRule(icon=WBIcon.EYE.value, condition=("∃", True)), ], ), ], )
class NbPriceStockModelViewSet(viewsets.ModelViewSet): ENDPOINT = 'djangoapp:price-list' IDENTIFIER = 'djangoapp:price' INSTANCE_ENDPOINT = 'djangoapp:stock-list' LIST_TITLE = "Nb Stocks" queryset = Stock.objects.all() serializer_class = NbPriceStockModelSerializer LIST_DISPLAY = dp.ListDisplay(fields=[ dp.Field(key="symbol", label="Symbol"), dp.Field(key="nb_prices", label="Total Prices"), dp.Field(key="nb_prices_today", label="Total Prices today"), ], ) filter_backends = [ filters.OrderingFilter, filters.SearchFilter, DjangoFilterBackend ] ordering_fields = ['symbol'] ordering = ['symbol'] search_fields = ("symbol", ) filterset_fields = {"symbol": ["exact", "icontains"]} def get_queryset(self): #date = timezone.now().date() - timedelta(days=1) today = timezone.now().date() return Stock.objects.annotate( nb_prices=Count(F("prices")), nb_prices_today=Coalesce(Stock.get_nb_prices_stock_date(today), 0)) def get_aggregates(self, queryset, **kwargs): return { "symbol": { "#": format_number(queryset.count()) }, "nb_prices": { "#": queryset.aggregate(s=Sum(F("nb_prices")))["s"] }, "nb_prices_today": { "#": queryset.aggregate(s=Sum(F("nb_prices_today")))["s"] } }
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_list_display(self) -> Optional[dp.ListDisplay]: return dp.ListDisplay(fields=[ dp.Field(key="char_field", label="Char"), dp.Field(key="integer_field", label="Integer"), dp.Field(key="integer_annotated", label="Integer Anno"), ], )
def get_list_display(self) -> Optional[dp.ListDisplay]: return dp.ListDisplay( fields=[ dp.Field(key="tags", label="Tags"), dp.Field(key="char_field", label="Char"), dp.Field(key="annotated_char_field", label="A-Char"), dp.Field( key="float_field", label="Float", formatting_rules=[ dp.FormattingRule(icon="wb-icon-trash", condition=dp.Condition(operator=Operator.LESS, value=0),) ], ), dp.Field(key="percent_field", label="Percent"), dp.Field(key="decimal_field", label="Decimal"), dp.Field(key="datetime_field", label="DateTime"), dp.Field(key="date_field", label="Date"), dp.Field(key="time_field", label="Time"), dp.Field( key="boolean_field", label="Boolean", formatting_rules=[ dp.FormattingRule(style={"color": "red"}, condition=dp.Condition(operator=Operator.EQUAL, value=True),) ], ), dp.Field(key="choice_field", label="Choice"), dp.Field(key="status_field", label="Status"), dp.Field(key="image_field", label="Image"), dp.Field(key="related_models", label="Related"), dp.Field(key="file_field", label="File"), dp.Field(key="star_rating", label="Star"), ], legends=[ dp.Legend(items=[dp.LegendItem(icon="wb-icon-thumbs-up-full", label="Good Stuff")]), dp.Legend( key="status_field", items=[ dp.LegendItem(icon="wb-icon-thumbs-up-full", label="Status1", value="status1"), dp.LegendItem(icon="wb-icon-thumbs-up-full", label="Status2", value="status2"), dp.LegendItem(icon="wb-icon-thumbs-up-full", label="Status3", value="status3"), ], ), ], formatting=[ dp.Formatting( column="integer_field", formatting_rules=[ dp.FormattingRule(icon="wb-icon-thumbs-up-full", condition=("<", 5000)), dp.FormattingRule(style={"backgroundColor": "rgb(80,220,100)"}, condition=("<", 5000),), ], ), ], )