# Path: chamber/shortcuts.py
# def bulk_change(iterable, **changed_fields):
#     """
#     Changes a given `changed_fields` on each object in a given `iterable`, returns the changed objects.
#     """
#     return [change(obj, **changed_fields) for obj in iterable]
# 
# def bulk_change_and_save(iterable, update_only_changed_fields=False, save_kwargs=None, **changed_fields):
#     """
#     Changes a given `changed_fields` on each object in a given `iterable`, saves objects
#     and returns the changed objects.
#     """
#     return [
#         change_and_save(obj, update_only_changed_fields=update_only_changed_fields, save_kwargs=save_kwargs,
#                         **changed_fields)
#         for obj in iterable
#     ]
# 
# def bulk_save(iterable):
#     """
#     Saves a objects in a given `iterable`.
#     """
#     return [obj.save() for obj in iterable]
# 
# def change(obj, **changed_fields):
#     """
#     Changes a given `changed_fields` on object and returns changed object.
#     """
#     obj_field_names = get_model_field_names(obj)
# 
#     for field_name, value in changed_fields.items():
#         if field_name not in obj_field_names:
#             raise ValueError("'{}' is an invalid field name".format(field_name))
#         setattr(obj, field_name, value)
#     return obj
# 
# def change_and_save(obj, update_only_changed_fields=False, save_kwargs=None, **changed_fields):
#     """
#     Changes a given `changed_fields` on object, saves it and returns changed object.
#     """
#     from chamber.models import SmartModel
# 
#     save_kwargs = save_kwargs if save_kwargs is not None else {}
#     if update_only_changed_fields:
#         if isinstance(obj, SmartModel):
#             save_kwargs['update_only_changed_fields'] = True
#         else:
#             save_kwargs['update_fields'] = get_update_fields(obj, **changed_fields)
# 
#     change(obj, **changed_fields)
#     obj.save(**save_kwargs)
#     return obj
# 
# def distinct_field(klass, *args, **kwargs):
#     return _get_queryset(klass).order_by().values_list(*args, **kwargs).distinct()
# 
# def exclude_by_date(klass, **kwargs):
#     return filter_or_exclude_by_date(True, klass, **kwargs)
# 
# def filter_by_date(klass, **kwargs):
#     return filter_or_exclude_by_date(False, klass, **kwargs)
# 
# def get_object_or_404(klass, *args, **kwargs):
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except (queryset.model.DoesNotExist, ValueError, ValidationError):
#         raise Http404
# 
# def get_object_or_none(klass, *args, **kwargs):
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except (queryset.model.DoesNotExist, ValueError, ValidationError):
#         return None


from datetime import date, datetime
from django.core.exceptions import FieldError, MultipleObjectsReturned
from django.http.response import Http404
from django.test import TestCase
from django.utils import timezone
from chamber.shortcuts import (
    bulk_change, bulk_change_and_save, bulk_save, change, change_and_save, distinct_field,
    exclude_by_date, filter_by_date, get_object_or_404, get_object_or_none
)
from germanium.tools import assert_equal, assert_is_none, assert_raises  # pylint: disable=E0401
from test_chamber.models import ShortcutsModel, DiffModel

        assert_raises(FieldError, get_object_or_none, ShortcutsModel, non_field='test2')
        assert_raises(MultipleObjectsReturned, get_object_or_none, ShortcutsModel, name='test2')

    def test_get_object_or_404(self):
        obj = ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=2)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=3)

        assert_equal(get_object_or_404(ShortcutsModel, name='test1'), obj)
        assert_raises(Http404, get_object_or_404, ShortcutsModel, name='test3')
        assert_raises(Http404, get_object_or_404, ShortcutsModel, number='test3')
        assert_raises(Http404, get_object_or_404, ShortcutsModel, datetime='test3')

        assert_raises(FieldError, get_object_or_none, ShortcutsModel, non_field='test2')
        assert_raises(MultipleObjectsReturned, get_object_or_none, ShortcutsModel, name='test2')

    def test_distinct_fields(self):
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=2)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=3)
        assert_equal(tuple(distinct_field(ShortcutsModel, 'name')), (('test1',), ('test2',)))
        assert_equal(list(distinct_field(ShortcutsModel, 'name', flat=True)), ['test1', 'test2'])

    def test_filter_and_exclude_by_date(self):
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test1', datetime=timezone.make_aware(datetime(2014, 1, 1, 12, 12),
                                                                                 timezone.get_default_timezone()),
                                      number=1)

# Path: chamber/shortcuts.py
# def bulk_change(iterable, **changed_fields):
#     """
#     Changes a given `changed_fields` on each object in a given `iterable`, returns the changed objects.
#     """
#     return [change(obj, **changed_fields) for obj in iterable]
# 
# def bulk_change_and_save(iterable, update_only_changed_fields=False, save_kwargs=None, **changed_fields):
#     """
#     Changes a given `changed_fields` on each object in a given `iterable`, saves objects
#     and returns the changed objects.
#     """
#     return [
#         change_and_save(obj, update_only_changed_fields=update_only_changed_fields, save_kwargs=save_kwargs,
#                         **changed_fields)
#         for obj in iterable
#     ]
# 
# def bulk_save(iterable):
#     """
#     Saves a objects in a given `iterable`.
#     """
#     return [obj.save() for obj in iterable]
# 
# def change(obj, **changed_fields):
#     """
#     Changes a given `changed_fields` on object and returns changed object.
#     """
#     obj_field_names = get_model_field_names(obj)
# 
#     for field_name, value in changed_fields.items():
#         if field_name not in obj_field_names:
#             raise ValueError("'{}' is an invalid field name".format(field_name))
#         setattr(obj, field_name, value)
#     return obj
# 
# def change_and_save(obj, update_only_changed_fields=False, save_kwargs=None, **changed_fields):
#     """
#     Changes a given `changed_fields` on object, saves it and returns changed object.
#     """
#     from chamber.models import SmartModel
# 
#     save_kwargs = save_kwargs if save_kwargs is not None else {}
#     if update_only_changed_fields:
#         if isinstance(obj, SmartModel):
#             save_kwargs['update_only_changed_fields'] = True
#         else:
#             save_kwargs['update_fields'] = get_update_fields(obj, **changed_fields)
# 
#     change(obj, **changed_fields)
#     obj.save(**save_kwargs)
#     return obj
# 
# def distinct_field(klass, *args, **kwargs):
#     return _get_queryset(klass).order_by().values_list(*args, **kwargs).distinct()
# 
# def exclude_by_date(klass, **kwargs):
#     return filter_or_exclude_by_date(True, klass, **kwargs)
# 
# def filter_by_date(klass, **kwargs):
#     return filter_or_exclude_by_date(False, klass, **kwargs)
# 
# def get_object_or_404(klass, *args, **kwargs):
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except (queryset.model.DoesNotExist, ValueError, ValidationError):
#         raise Http404
# 
# def get_object_or_none(klass, *args, **kwargs):
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except (queryset.model.DoesNotExist, ValueError, ValidationError):
#         return None


# Path: example/dj/apps/test_chamber/tests/shortcuts.py
from datetime import date, datetime
from django.core.exceptions import FieldError, MultipleObjectsReturned
from django.http.response import Http404
from django.test import TestCase
from django.utils import timezone
from chamber.shortcuts import (
    bulk_change, bulk_change_and_save, bulk_save, change, change_and_save, distinct_field,
    exclude_by_date, filter_by_date, get_object_or_404, get_object_or_none
)
from germanium.tools import assert_equal, assert_is_none, assert_raises  # pylint: disable=E0401
from test_chamber.models import ShortcutsModel, DiffModel


        assert_raises(FieldError, get_object_or_none, ShortcutsModel, non_field='test2')
        assert_raises(MultipleObjectsReturned, get_object_or_none, ShortcutsModel, name='test2')

    def test_get_object_or_404(self):
        obj = ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=2)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=3)

        assert_equal(get_object_or_404(ShortcutsModel, name='test1'), obj)
        assert_raises(Http404, get_object_or_404, ShortcutsModel, name='test3')
        assert_raises(Http404, get_object_or_404, ShortcutsModel, number='test3')
        assert_raises(Http404, get_object_or_404, ShortcutsModel, datetime='test3')

        assert_raises(FieldError, get_object_or_none, ShortcutsModel, non_field='test2')
        assert_raises(MultipleObjectsReturned, get_object_or_none, ShortcutsModel, name='test2')

    def test_distinct_fields(self):
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=2)
        ShortcutsModel.objects.create(name='test2', datetime=timezone.now(), number=3)
        assert_equal(tuple(distinct_field(ShortcutsModel, 'name')), (('test1',), ('test2',)))
        assert_equal(list(distinct_field(ShortcutsModel, 'name', flat=True)), ['test1', 'test2'])

    def test_filter_and_exclude_by_date(self):
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
        ShortcutsModel.objects.create(name='test1', datetime=timezone.make_aware(datetime(2014, 1, 1, 12, 12),
                                                                                 timezone.get_default_timezone()),
                                      number=1)

        assert_equal(filter_by_date(ShortcutsModel, datetime=date.today()).count(), 2)