{ "info": { "author": "Arsham Shirvani", "author_email": "arshamshirvani@gmail.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Topic :: Software Development :: Libraries" ], "description": "django-searchbar\n============\n\n# About\n\nA simple search bar and view handler to validate and get the results\n\n## Usage\n\nIn your view:\n\n```python\ndef my_view(request):\n search_bar = SearchBar(request, ['name', 'age'])\n\n #When the form is coming from posted page\n if search_bar.is_valid():\n my_name = search_bar['name']\n ....\n\n #If your url comes as: \"?name=my_name\" and you don't care about the age, do this instead:\n if search_bar.is_valid('name'):\n my_name = search_bar['name']\n #If you need to apply name to your queryset:\n Model.objects.filter(name__contains=my_name)\n ....\n```\n\nOr if you prefer Class Based Views:\n\n```python\nfrom django.views.generic import ListView\nfrom django_searchbar.mixins import SearchBarViewMixin\n\nclass MyView(SearchBarViewMixin, ListView):\n searchbar_fields = ['name']\n ....\n```\n\nand that's about it! The search bar get's validated on it's own, and it will use the value of 'name' in your queryset automatically.\nYou can access the search bar in your template by (It's automatically is injected into your context):\n\n```python\n{{ search_bar }}\n```\n\nYou can also change the form method, choices are 'get' and 'post':\n\n```python\n search_bar = SearchBar(request, ['name', 'age'], method='get')\n```\n\nIn CBV:\n\n```python\nclass MyView(SearchBarViewMixin, ListView):\n searchbar_fields = ['name']\n searchbar_method = 'get'\n```\n\n## Advanced Usage\n\nNotice: This is as far as it goes, if you need more advanced techniques you should use django's forms.\n\nIf you need to show choices or set a field required:\n\n```python\ndef my_view(request):\n search_bar = SearchBar(request, [\n 'name',\n {\n 'label': 'age',\n },\n ]) # this is the same as above, but....\n\n search_bar = SearchBar(request, [\n 'name',\n {\n 'label': 'age',\n 'required': True,\n },\n ])# Will fail the validation if user don't provide a value for age\n\n #If you need choices, do:\n search_bar = SearchBar(request, [\n 'name',\n {\n 'label': 'gender',\n 'choices': (\n ('none', 'N/A')\n ('m', 'Male'),\n ('f', 'Female'),\n ), #Same format as django's forms\n 'ignore_list': ['none'] #Ignores in query filters if the value is 'none' (as above)\n 'required': True, #Optional, default is false\n },\n ])\n\n #If you need custom widget:\n search_bar = SearchBar(request, [\n 'name',\n {\n 'label': 'age',\n 'required': True,\n 'widget': forms.Textarea(attrs={'placeholder': 'Enter your name here..'})\n },\n ])\n```\n\nOr in CBV:\n\n```python\nclass MyView(SearchBarViewMixin, ListView):\n searchbar_fields = [\n 'name',\n {'label': 'age', 'required': True},\n {\n 'label': 'gender',\n 'choices': (\n ('none', 'N/A')\n ('m', 'Male'),\n ('f', 'Female'),\n ), #Same format as django's forms\n 'ignore_list': ['none'] #Ignores in query filters if the value is 'none' (as above)\n 'required': True, #Optional, default is false\n },\n ]\n```\n\nYou can also define a replacement dictionary/callback, which transforms your input to something you can actually use in your db.model queries after getting filters (get_filters)\n\n```python\n search_bar = SearchBar(request, ['username'], replacement={'username': 'user__username'})\n```\n\nor a bit smarter:\n\n```python\n def replacement(item):\n return {\n 'username': 'user__username',\n 'foo': 'bar',\n }.get(item, 'user__username')\n\n search_bar = SearchBar(request, ['username'], replacement=replacement)\n```\n\nIn CBV:\n\n```python\nclass MyView(SearchBarViewMixin, ListView):\n searchbar_replacements = {\n 'username': 'user__username',\n }\n #or using above function:\n searchbar_replacements = replacement\n\n```\n\n## Recipes\n\nIf you have a field you are using in various places, e.g. if you have ordering on some of your searchbars, do this:\n\n```python\nfrom django_searchbar.utils import SearchBar\n\n\nclass OrderingSearchBar(SearchBar):\n\n def __init__(self, request, choices, replacements={}, fields=None, method='post'):\n\n ...do some checks here...\n\n self.__choices = (('none', '----'),) + choices\n self.__replacements = replacements\n fields.extend([{\n 'label': 'order_by',\n 'choices': self.__choices,\n }, {\n 'label': 'direction',\n 'choices': (\n ('asc', 'ASC'),\n ('desc', 'DESC'),\n ),\n },\n ])\n\n super().__init__(request=request, fields=fields, method=method)\n\n def __getitem__(self, index):\n if index == 'order_by':\n\n exam_dict = {}\n for choice in self.__choices:\n if choice[0] is 'none':\n exam_dict[choice[0]] = False\n else:\n exam_dict[choice[0]] = self.__replacements.get(choice[0], choice[0])\n\n order_by = exam_dict.get(self.form.cleaned_data['order_by'])\n\n if order_by and order_by != 'none':\n direction = {\n 'asc': '',\n 'desc': '-',\n }.get(self['direction'], '')\n\n return \"%s%s\" % (direction, order_by)\n\n return False\n\n else:\n return super().__getitem__(index)\n\n\n#Now the mixin\nclass OrderingSearchBarViewMixin(SearchBarViewMixin):\n\n def get_searchbar(self, request):\n ...do some checks here...\n return OrderingSearchBar(\n request=request,\n choices=self.searchbar_choices,\n fields=self.searchbar_fields,\n replacements=self.searchbar_replacements,\n method=self.searchbar_method)\n\n def get(self, request, *args, **kwargs):\n\n search_obj = self.get_searchbar(request)\n if search_obj.is_valid() and search_obj.get_ordering():\n self.queryset = self.queryset.order_by(search_obj.get_ordering())\n\n return super().get(request, *args, **kwargs)\n\n```\n\nNow in your view you can do this:\n\n```python\n choices = (\n ('username', 'Username'),\n )\n replacements = {\n 'username': 'user__username__icontains',\n }\n\n search_obj = OrderingSearchBar(request, fields=['username'], choices=choices, replacements=replacements)\n people = Person.objects.all()\n\n if search_obj.is_valid():\n people = people.filter(user__username__icontains=search_obj['username'])\n #Or\n people = people.filter(search_obj.get_filters())\n\n```\n\nIn CBV:\n\n```python\nclass MyView(SearchBarViewMixin, ListView):\n searchbar_choices = (\n ('username', 'Username'),\n )\n searchbar_replacements = {\n 'username': 'user__username__icontains',\n }\n\n```", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/arsham/django-searchbar", "keywords": "searchbar,django,forms,template", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "django-searchbar", "package_url": "https://pypi.org/project/django-searchbar/", "platform": "OS Independen", "project_url": "https://pypi.org/project/django-searchbar/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/arsham/django-searchbar" }, "release_url": "https://pypi.org/project/django-searchbar/0.1.7/", "requires_dist": null, "requires_python": null, "summary": "Simple searchbar and handler you can use in all your views and templates.", "version": "0.1.7" }, "last_serial": 1393159, "releases": { "0.1.1": [ { "comment_text": "", "digests": { "md5": "990c5cee419e727e84d6d9229a0f7abb", "sha256": "587030360fbf1f9e83935e43aaa5f68718085ebbd71514d6c31743fe68167e48" }, "downloads": -1, "filename": "django-searchbar-0.1.1.tar.gz", "has_sig": false, "md5_digest": "990c5cee419e727e84d6d9229a0f7abb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7788, "upload_time": "2014-10-17T16:33:47", "url": "https://files.pythonhosted.org/packages/b0/50/20ea5caa8763afbb5308f31e97d7db6e4c30b3b96ac744cd52b0b6a8d964/django-searchbar-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "ed286f2b9d1902165fe530f5e3e72b74", "sha256": "d98be3776618eedb5db48398f18d613da43f32167e43ab6bcc96d98665122624" }, "downloads": -1, "filename": "django-searchbar-0.1.2.tar.gz", "has_sig": false, "md5_digest": "ed286f2b9d1902165fe530f5e3e72b74", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10364, "upload_time": "2014-10-20T16:51:25", "url": "https://files.pythonhosted.org/packages/c8/e1/33eed7d3636e377c925fa85f96ec7da0c54efb5f345425bfb430f5ffe10e/django-searchbar-0.1.2.tar.gz" } ], "0.1.3": [ { "comment_text": "", "digests": { "md5": "39ce61d32d9d238259abec93bd0901d2", "sha256": "d8dfb0b736e181959d98e4dd76381153caa5b25e388fea22f2343f43c45ee8eb" }, "downloads": -1, "filename": "django-searchbar-0.1.3.tar.gz", "has_sig": false, "md5_digest": "39ce61d32d9d238259abec93bd0901d2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10755, "upload_time": "2014-10-28T09:25:33", "url": "https://files.pythonhosted.org/packages/0c/eb/15f5b4c7d680b2469548f3aad10e96cc2923762e667800fde68f125cea53/django-searchbar-0.1.3.tar.gz" } ], "0.1.4": [ { "comment_text": "", "digests": { "md5": "40b829bf2cc669c2a1e12358accc388f", "sha256": "761f0099ad2232c1687d400138416e01f65d4483343b977821262da588429edf" }, "downloads": -1, "filename": "django-searchbar-0.1.4.tar.gz", "has_sig": false, "md5_digest": "40b829bf2cc669c2a1e12358accc388f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10800, "upload_time": "2014-10-28T10:07:21", "url": "https://files.pythonhosted.org/packages/db/ba/87b68e5dea4df84f7410166ad4fb08338b7a4cbc7c7b2d01c9286a831eca/django-searchbar-0.1.4.tar.gz" } ], "0.1.5": [ { "comment_text": "", "digests": { "md5": "79c34edf42915c79c54727f33720697c", "sha256": "2d803930766c94188b6c42c9abdee01768731ebeb5efb5b72bd8db96790a0524" }, "downloads": -1, "filename": "django-searchbar-0.1.5.tar.gz", "has_sig": false, "md5_digest": "79c34edf42915c79c54727f33720697c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11461, "upload_time": "2014-11-14T17:18:00", "url": "https://files.pythonhosted.org/packages/ff/e0/d4975997d0dcd493028de24145fdf54fe364ec1d54375c19fd23424cd9d6/django-searchbar-0.1.5.tar.gz" } ], "0.1.6": [ { "comment_text": "", "digests": { "md5": "a687f7988836a830f935a6d59a48bac7", "sha256": "5ec4b4589d203b87dd8ae5593abc6db031c2cd633c1a956838f0e58d1f8f6c4a" }, "downloads": -1, "filename": "django-searchbar-0.1.6.tar.gz", "has_sig": false, "md5_digest": "a687f7988836a830f935a6d59a48bac7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11619, "upload_time": "2014-11-14T17:49:10", "url": "https://files.pythonhosted.org/packages/72/0e/192eea50c817db5efa4318946cb33b5e56fbdf840af14dfa28c1dffd3f4a/django-searchbar-0.1.6.tar.gz" } ], "0.1.7": [ { "comment_text": "", "digests": { "md5": "13119ecb18dafd7c17cd8ba756f17659", "sha256": "ef775036ded09a04210f04adac87f70ee720c6f37143d94172e887df185d3a99" }, "downloads": -1, "filename": "django-searchbar-0.1.7.tar.gz", "has_sig": false, "md5_digest": "13119ecb18dafd7c17cd8ba756f17659", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11636, "upload_time": "2015-01-23T12:53:26", "url": "https://files.pythonhosted.org/packages/10/44/8506c9c338c2d4336401bd9bb867aec77d5fd68e832e7770983b2c6ab42e/django-searchbar-0.1.7.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "13119ecb18dafd7c17cd8ba756f17659", "sha256": "ef775036ded09a04210f04adac87f70ee720c6f37143d94172e887df185d3a99" }, "downloads": -1, "filename": "django-searchbar-0.1.7.tar.gz", "has_sig": false, "md5_digest": "13119ecb18dafd7c17cd8ba756f17659", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11636, "upload_time": "2015-01-23T12:53:26", "url": "https://files.pythonhosted.org/packages/10/44/8506c9c338c2d4336401bd9bb867aec77d5fd68e832e7770983b2c6ab42e/django-searchbar-0.1.7.tar.gz" } ] }