def save(self, *args, **kwargs): instance = Datebook( author=self.cleaned_data['owner'], period=self.cleaned_data['period'] ) instance.save() return instance
def get_datebook(self, filters): try: obj = Datebook.objects.get(author__username=self.kwargs['author'], **filters) except Datebook.DoesNotExist: author = get_object_or_404(User, username=self.kwargs['author']) obj = Datebook(author=author, period=datetime.date(self.year, self.month, self.day)) obj.save() return obj
def get_datebook(self): """ Get or Create datebook """ try: obj = Datebook.objects.get(author__username=self.kwargs['author'], period__year=self.year, period__month=self.month) except Datebook.DoesNotExist: self.author = get_object_or_404(User, username=self.kwargs['author']) obj = Datebook(author=self.author, period=datetime.date(self.year, self.month, 1)) obj.save() else: self.author = obj.author return obj
def save(self, *args, **kwargs): year = int(self.cleaned_data['year']) # Default opened datebook is for the first month of the given year period = [year, 1, 1] # Open the datebook for the current month if the given year is the current year if year == self.today.year: period = [year, self.today.month, 1] instance = Datebook( author=self.author, period=datetime.date(*period) ) instance.save() return instance