Created
March 5, 2021 14:00
-
-
Save umarhussain88/14c447de40f07d998d3fa5fd496bfeb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .models import Carbon | |
from django.db.models import Q | |
from django.views.generic import ListView | |
from datetime import datetime | |
class CarbonDataView(ListView): | |
model = Carbon | |
context_object_name = 'carbon' | |
template_name = 'carbon/chart.html' | |
def get_context_data(self, **kwargs): | |
context = super().get_context_data(**kwargs) | |
query = self.request.GET.get('q') | |
context['date'] = Carbon.objects.values('reading_date').order_by('reading_date').distinct() | |
if query: | |
context['carbon'] = Carbon.objects.filter( | |
Q(reading_date=datetime.strptime(query,"%Y,%b,%d")) | |
) | |
context['selected_date'] = datetime.strptime(query,"%Y,%b,%d") | |
else: | |
context['carbon'] = Carbon.objects.filter( | |
Q(reading_date=datetime(2017,1,1)) | |
) | |
context['selected_date'] = datetime(2017,1,1) | |
return context | |
# class CarbonExportView() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment