Raschiando condizioni meteorologiche per le cime delle montagne

Aug 20 2020

Ho realizzato un sito web in Django in cui ho provato per la prima volta a creare un'app complessa. È un'app che registra le condizioni meteorologiche su un picco in montagna, che sono 17. Volevo mostrare una previsione dettagliata in modelli separati, quindi ho 17 visualizzazioni che sembrano quasi uguali.

Solo 4 visualizzazioni:

class KasprowyWierchForecastView(TemplateView):
    """
    class for rendering view of detailed weather forecast for
    Kasprowy Wierch peak
    """
    template_name = 'mountain_base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak='Kasprowy').order_by('date')
        print(context['Peak'])
        return context


class KoscielecForecastView(TemplateView):
    """
    class for rendering view of detailed weather forecast for
        Koscielec peak
    """
    template_name = 'mountain_base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak='Koscielec').order_by('date')
        return context


class KrivanForecastView(TemplateView):
    """
    class for rendering view of detailed weather forecast for Krivan peak
    """
    template_name = 'mountain_base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak='Krivan').order_by('date')
        return context


class MieguszowieckiForecastView(TemplateView):
    """
    class for rendering view of detailed weather forecast for
    Mieguszowiecki peak
    """
    template_name = 'mountain_base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak='Mieguszowiecki').order_by('date')
        return context


class MnichForecastView(TemplateView):
    """
    class for rendering view of detailed weather forecast for
    Mnich peak
    """
    template_name = 'mountain_base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak='Mnich').order_by('date')
        return context

Solo il nome della classe di visualizzazione e context['Peak']sono diversi. Il resto del codice è ridondante. Poiché la mia esperienza si basa solo sul mio processo di autoapprendimento, non ho trovato alcuna buona soluzione su come correggere o refactoring.

Risposte

2 fabrizzio_gz Aug 20 2020 at 09:34

Un'opzione è modificare la tua TemplateViewclasse per includere il nome del picco come attributo di istanza (diciamo peak_name) e fare in modo che il get_context_datametodo ne faccia uso:

class  TemplateView():
    # Class attributes

    def __init__(self, peak_name):
        # Instance attributes
        # ...
        self.peak_name = peak_name

    # Class methods

    # Modification of the get_context_data method
    def get_context_data(self, **kwargs):
        context['Peak'] = PeakForecast.objects.filter(
            name_of_peak=self.peak_name).order_by('date')  #<- Modification here
        print(context['Peak'])
        return context

E poi puoi generare le tue visualizzazioni di picco come istanze di quella classe:

krivan_view = TemplateView('Krivan')
krivan_view.get_context_data(arg1='arg1', arg2='arg2')