コード例 #1
0
ファイル: create.py プロジェクト: somair/grocerylist
 def form_valid(self, form):
     self.object = form.save(commit=False)
     self.object.user = self.request.user
     self.object.store = get_object_or_404(Store,
                                           user=self.request.user,
                                           slug=self.kwargs.get('slug'))
     if List.objects.filter(user=self.request.user).count() > 25:
         messages.error(self.request,
                        'Sorry, you have too many lists already!',
                        extra_tags='danger')
         return redirect(self.object.store.get_absolute_url())
     if not self.object.name:
         self.object.name = str(
             time.strftime("%a %b %d %Y", time.localtime()))
     item_list = self.request.POST.getlist('content', False)
     if not item_list:
         messages.error(
             self.request,
             'Failed to create new grocery list. No items were selected.',
             extra_tags='danger')
         return redirect(self.object.store.get_absolute_url())
     for this_item in item_list:
         # For each item the user checked create a new Tobuy object,
         # then link that new object to the List they are creating.
         item_match = get_object_or_404(Item, id=this_item)
         new_tobuy = Tobuy(name=item_match,
                           quantity=1,
                           user=self.request.user)
         new_tobuy.save()
         self.object.save()
         self.object.content.add(new_tobuy)
     log_form_valid(self, form, action='create')
     return super(do, self).form_valid(form)
コード例 #2
0
ファイル: update.py プロジェクト: somair/grocerylist
 def get(self, request, *args, **kwargs):
     if request.GET.get('inc'):
         try:
             this_tobuy = Tobuy.objects.filter(user=self.request.user).get(
                 id=request.GET.get('inc'))
             this_tobuy.quantity += 1
             this_tobuy.save()
             retval = '{0}#{1}'.format(
                 reverse('lists:update', kwargs={'pk': kwargs.get('pk')}),
                 this_tobuy.id)
             return redirect(retval)
         except Exception as e:
             print(e)
     elif request.GET.get('dec'):
         try:
             this_tobuy = Tobuy.objects.filter(user=self.request.user).get(
                 id=request.GET.get('dec'))
             this_tobuy.quantity -= 1
             this_tobuy.save()
             retval = '{0}#{1}'.format(
                 reverse('lists:update', kwargs={'pk': kwargs.get('pk')}),
                 this_tobuy.id)
             if this_tobuy.quantity < 1:
                 this_tobuy.delete()
             return redirect(retval)
         except Exception as e:
             print(e)
     elif request.GET.get('insert'):
         try:
             # Get item from db
             item_match = Item.objects.filter(user=self.request.user).get(
                 id=request.GET.get('insert'))
             #
             # Create a new Tobuy object with that item
             new_tobuy = Tobuy(name=item_match,
                               quantity=1,
                               user=self.request.user)
             new_tobuy.save()
             #
             # find current List
             this_list = List.objects.filter(user=self.request.user).get(
                 id=kwargs.get('pk'))
             #
             # Attach new Item to List.content
             this_list.content.add(new_tobuy)
             this_list.save()
             #
             # Go back to /mylists/PK/update
             return redirect(
                 reverse('lists:update', kwargs={'pk': kwargs.get('pk')}))
         except Exception as e:
             print(e)
     # all-else
     return super(do, self).get(self, request, *args, **kwargs)
コード例 #3
0
ファイル: update.py プロジェクト: nullpass/grocerylist
 def get(self, request, *args, **kwargs):
     if request.GET.get('inc'):
         try:
             this_tobuy = Tobuy.objects.filter(user=self.request.user).get(id=request.GET.get('inc'))
             this_tobuy.quantity += 1
             this_tobuy.save()
             retval = '{0}#{1}'.format(
                 reverse('lists:update', kwargs={'pk' : kwargs.get('pk')}),
                 this_tobuy.id
                 )
             return redirect(retval)
         except Exception as e:
             print(e)
     elif request.GET.get('dec'):
         try:
             this_tobuy = Tobuy.objects.filter(user=self.request.user).get(id=request.GET.get('dec'))
             this_tobuy.quantity -= 1
             this_tobuy.save()
             retval = '{0}#{1}'.format(
                 reverse('lists:update', kwargs={'pk' : kwargs.get('pk')}),
                 this_tobuy.id
                 )
             if this_tobuy.quantity < 1:
                 this_tobuy.delete()
             return redirect(retval)
         except Exception as e:
             print(e)
     elif request.GET.get('insert'):
         try:
             # Get item from db
             item_match = Item.objects.filter(user=self.request.user).get(id=request.GET.get('insert'))
             #
             # Create a new Tobuy object with that item
             new_tobuy = Tobuy(name=item_match, quantity=1, user=self.request.user)
             new_tobuy.save()
             #
             # find current List
             this_list = List.objects.filter(user=self.request.user).get(id=kwargs.get('pk'))
             #
             # Attach new Item to List.content
             this_list.content.add(new_tobuy)
             this_list.save()
             #
             # Go back to /mylists/PK/update
             return redirect(reverse('lists:update', kwargs={'pk' : kwargs.get('pk')}))
         except Exception as e:
             print(e)
     # all-else
     return super(do, self).get(self, request, *args, **kwargs)
コード例 #4
0
ファイル: create.py プロジェクト: nullpass/grocerylist
 def form_valid(self, form):
     self.object = form.save(commit=False)
     self.object.user = self.request.user
     self.object.store = get_object_or_404(Store, user=self.request.user, slug=self.kwargs.get('slug'))
     if List.objects.filter(user=self.request.user).count() > 25:
         messages.error(self.request, 'Sorry, you have too many lists already!', extra_tags='danger')
         return redirect(self.object.store.get_absolute_url())
     if not self.object.name:
         self.object.name = str( time.strftime("%a %b %d %Y", time.localtime()) )
     item_list = self.request.POST.getlist('content', False)
     if not item_list:
         messages.error(self.request, 'Failed to create new grocery list. No items were selected.', extra_tags='danger')
         return redirect(self.object.store.get_absolute_url())
     for this_item in item_list:
         # For each item the user checked create a new Tobuy object,
         # then link that new object to the List they are creating.
         item_match = get_object_or_404(Item, id=this_item)
         new_tobuy = Tobuy(name=item_match, quantity=1, user=self.request.user)
         new_tobuy.save()
         self.object.save()
         self.object.content.add(new_tobuy)
     log_form_valid(self, form, action='create')
     return super(do, self).form_valid(form)