From ed6bee6755ff12d486f77a55821fe672582fe8e3 Mon Sep 17 00:00:00 2001 From: Thibault Deutsch Date: Wed, 17 Jun 2015 12:25:45 +0200 Subject: [PATCH] custom user-agent --- AlgoliaSearch/models.py | 15 +++-- AlgoliaSearch/registration.py | 2 + AlgoliaSearch/version.py | 1 + setup.py | 22 ++++++-- tests/__init__.py | 0 tests/settings.py | 102 ---------------------------------- 6 files changed, 28 insertions(+), 114 deletions(-) create mode 100644 AlgoliaSearch/version.py delete mode 100644 tests/__init__.py delete mode 100644 tests/settings.py diff --git a/AlgoliaSearch/models.py b/AlgoliaSearch/models.py index 5b1abf1..210c5c7 100644 --- a/AlgoliaSearch/models.py +++ b/AlgoliaSearch/models.py @@ -49,7 +49,7 @@ def __init__(self, model, client): for field in self.fields: if not (hasattr(model, field) or (field in all_fields)): raise AlgoliaIndexError( - '{} is not a attributes of {}.'.format(field, model)) + '{} is not an attribute of {}.'.format(field, model)) # If no fields are specified, index all the fields of the model if not self.fields: @@ -62,10 +62,10 @@ def __init__(self, model, client): attr = getattr(model, self.geo_field) if not (isinstance(attr, tuple) or callable(attr)): raise AlgoliaIndexError( - '`geo_field` should be a tuple or a callable that returns a tuple.') + '`geo_field` should be a tuple or a callable that returns a tuple.') else: - raise AlgoliaIndexError( - '{} is not an attributes of {}.'.format(self.geo_field, model)) + raise AlgoliaIndexError('{} is not an attribute of {}.'.format( + self.geo_field, model)) # Check custom_objectID if self.custom_objectID: @@ -73,11 +73,10 @@ def __init__(self, model, client): attr = getattr(model, elf.custom_objectID) if not isinstance(attr, str): raise AlgoliaIndexError( - '`custom_objectID` should be a string.') + '`custom_objectID` should be a string.') else: - raise AlgoliaIndex( - '`{}` is not an attribute of {}.'.format(self.custom_objectID, model)) - + raise AlgoliaIndex('`{}` is not an attribute of {}.'.format( + self.custom_objectID, model)) def __set_index(self, client): '''Get an instance of Algolia Index''' diff --git a/AlgoliaSearch/registration.py b/AlgoliaSearch/registration.py index 668fbdf..ba6c674 100644 --- a/AlgoliaSearch/registration.py +++ b/AlgoliaSearch/registration.py @@ -5,6 +5,7 @@ from django.db.models.signals import post_save, pre_delete from AlgoliaSearch.models import AlgoliaIndex +from AlgoliaSearch.version import VERSION from algoliasearch import algoliasearch @@ -24,6 +25,7 @@ def __init__(self, '''Initializes Algolia engine.''' self.__registered_models = {} self.client = algoliasearch.Client(app_id, api_key) + self.client.set_extra_header('User-Agent', 'Algolia for Django {}'.format(VERSION)) def is_registered(self, model): '''Checks whether the given models is registered with Algolia engine.''' diff --git a/AlgoliaSearch/version.py b/AlgoliaSearch/version.py new file mode 100644 index 0000000..1e5a605 --- /dev/null +++ b/AlgoliaSearch/version.py @@ -0,0 +1 @@ +VERSION = '1.0.0' diff --git a/setup.py b/setup.py index 0a65050..6ee5fe0 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,30 @@ +#!/usr/bin/env python + import os -from setuptools import setup +import sys +try: + from setuptools import setup +except ImportError: + from distutils.core import setup -with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme: - README = readme.read() # Allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) +with open(os.path.join(os.path.dirname(__file__), 'README.md')) as readme: + README = readme.read() + +path_version = os.path.join(os.path.dirname(__file__), 'AlgoliaSearch/version.py') +if sys.version_info[0] == 3: + execfile(path_version) +else: + exec(open(path_version).read()) + + setup( name = 'algoliasearch-django', - version = '1.0.0', + version = VERSION, license = 'MIT License', packages = ['AlgoliaSearch'], install_requires = ['django', 'algoliasearch'], diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/settings.py b/tests/settings.py deleted file mode 100644 index bd380d1..0000000 --- a/tests/settings.py +++ /dev/null @@ -1,102 +0,0 @@ -""" -Django settings for AlgoliaSearchTests project. - -Generated by 'django-admin startproject' using Django 1.8.2. - -For more information on this file, see -https://docs.djangoproject.com/en/1.8/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.8/ref/settings/ -""" - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -import os - -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'ccrz6ph0^ywgjc1hxh5%(se43seysw9x-#j+a7=9e_n8(bb8s^' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', -) - -MIDDLEWARE_CLASSES = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'django.middleware.security.SecurityMiddleware', -) - -ROOT_URLCONF = 'AlgoliaSearchTests.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'AlgoliaSearchTests.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.8/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} - - -# Internationalization -# https://docs.djangoproject.com/en/1.8/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.8/howto/static-files/ - -STATIC_URL = '/static/'