├── README.md ├── group ├── __init__.py ├── views.py ├── models.pyc ├── __init__.pyc ├── tests.py ├── models.py └── api.py ├── oval ├── __init__.py ├── api.pyc ├── forms.pyc ├── urls.pyc ├── views.pyc ├── wsgi.pyc ├── __init__.pyc ├── settings.pyc ├── forms.py ├── urls.py ├── wsgi.py ├── api.py ├── views.py └── settings.py ├── .gitignore ├── requirements.txt └── manage.py /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /group/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /oval/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .venv/ 2 | *.pyc 3 | -------------------------------------------------------------------------------- /group/views.py: -------------------------------------------------------------------------------- 1 | # Create your views here. 2 | -------------------------------------------------------------------------------- /oval/api.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/api.pyc -------------------------------------------------------------------------------- /oval/forms.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/forms.pyc -------------------------------------------------------------------------------- /oval/urls.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/urls.pyc -------------------------------------------------------------------------------- /oval/views.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/views.pyc -------------------------------------------------------------------------------- /oval/wsgi.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/wsgi.pyc -------------------------------------------------------------------------------- /group/models.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/group/models.pyc -------------------------------------------------------------------------------- /oval/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/__init__.pyc -------------------------------------------------------------------------------- /oval/settings.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/oval/settings.pyc -------------------------------------------------------------------------------- /group/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrshll/oval_back/master/group/__init__.pyc -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.4.5 2 | django-tastypie==0.9.12 3 | mimeparse==0.1.3 4 | psycopg2==2.4.6 5 | python-dateutil==2.1 6 | six==1.2.0 7 | wsgiref==0.1.2 8 | -------------------------------------------------------------------------------- /oval/forms.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | 3 | class LoginForm(forms.Form): 4 | email = forms.CharField(max_length=100) 5 | password = forms.PasswordField() 6 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oval.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /group/tests.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file demonstrates writing tests using the unittest module. These will pass 3 | when you run "manage.py test". 4 | 5 | Replace this with more appropriate tests for your application. 6 | """ 7 | 8 | from django.test import TestCase 9 | 10 | 11 | class SimpleTest(TestCase): 12 | def test_basic_addition(self): 13 | """ 14 | Tests that 1 + 1 always equals 2. 15 | """ 16 | self.assertEqual(1 + 1, 2) 17 | -------------------------------------------------------------------------------- /group/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.contrib.auth.models import User 3 | 4 | 5 | class Group(models.Model): 6 | name = models.CharField(max_length=120) 7 | users = models.ManyToManyField(User) 8 | 9 | class Tag(models.Model): 10 | name = models.CharField(max_length=40) 11 | 12 | class Post (models.Model): 13 | kind = models.CharField(max_length = 30) 14 | url = models.URLField(max_length = 400) 15 | date = models.DateTimeField(auto_now_add = True) 16 | group = models.ForeignKey(Group) 17 | user = models.ForeignKey(User) 18 | tags = models.ManyToManyField(Tag) 19 | 20 | 21 | from django.contrib import admin 22 | admin.site.register(Group) 23 | admin.site.register(Tag) 24 | admin.site.register(Post) 25 | 26 | -------------------------------------------------------------------------------- /oval/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import patterns, include, url 2 | 3 | # Uncomment the next two lines to enable the admin: 4 | from django.contrib import admin 5 | admin.autodiscover() 6 | # register our api resources 7 | from tastypie.api import Api 8 | # from recommendation_item.api import RestaurantResource 9 | from oval.api import UserResource 10 | from group.api import GroupResource, PostResource, TagResource 11 | 12 | v1_api = Api(api_name='v1') 13 | v1_api.register(UserResource()) 14 | v1_api.register(GroupResource()) 15 | v1_api.register(PostResource()) 16 | v1_api.register(TagResource()) 17 | 18 | urlpatterns = patterns('', 19 | # api routing 20 | (r'^api/', include(v1_api.urls)), 21 | #admin routing 22 | url(r'^admin/', include(admin.site.urls)), 23 | ) 24 | -------------------------------------------------------------------------------- /oval/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for oval project. 3 | 4 | This module contains the WSGI application used by Django's development server 5 | and any production WSGI deployments. It should expose a module-level variable 6 | named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover 7 | this application via the ``WSGI_APPLICATION`` setting. 8 | 9 | Usually you will have the standard Django WSGI application here, but it also 10 | might make sense to replace the whole Django WSGI application with a custom one 11 | that later delegates to the Django one. For example, you could introduce WSGI 12 | middleware here, or combine a Django application with an application of another 13 | framework. 14 | 15 | """ 16 | import os 17 | 18 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oval.settings") 19 | 20 | # This application object is used by any WSGI server configured to use this 21 | # file. This includes Django's development server, if the WSGI_APPLICATION 22 | # setting points here. 23 | from django.core.wsgi import get_wsgi_application 24 | application = get_wsgi_application() 25 | 26 | # Apply WSGI middleware here. 27 | # from helloworld.wsgi import HelloWorldApplication 28 | # application = HelloWorldApplication(application) 29 | -------------------------------------------------------------------------------- /oval/api.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import User 2 | from tastypie.resources import ModelResource 3 | from tastypie.authentication import ApiKeyAuthentication 4 | from tastypie.authorization import DjangoAuthorization 5 | from tastypie import fields 6 | from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS 7 | 8 | class ApiKeyOrSessionAuthentication(ApiKeyAuthentication): 9 | def is_authenticated(self, request, **kwargs): 10 | if request.user.is_authenticated(): 11 | return True 12 | return super(ApiKeyOrSessionAuthentication, self).is_authenticated(request, **kwargs) 13 | 14 | def get_identifier(self, request): 15 | if request.user.is_authenticated(): 16 | return request.user.username 17 | return super(ApiKeyOrSessionAuthentication, self).get_identifier(request) 18 | 19 | class UserResource(ModelResource): 20 | 21 | def determine_format(self, request): 22 | return 'application/json' 23 | 24 | class Meta: 25 | authentication = ApiKeyOrSessionAuthentication() 26 | authorization = DjangoAuthorization() 27 | 28 | queryset = User.objects.all() 29 | resource_name = 'user' 30 | allowed_methods = ['get','put'] 31 | excludes = ['password', 'is_staff', 'is_superuser'] 32 | 33 | filtering = { 'username': ALL, } 34 | -------------------------------------------------------------------------------- /oval/views.py: -------------------------------------------------------------------------------- 1 | from django.http import HttpResponseRedirect, HttpResponse 2 | from django.contrib.auth import authenticate, login as auth_login 3 | from django.contrib.auth.models import User 4 | 5 | from oval.api import UserResource 6 | from oval.forms import LoginForm 7 | 8 | # def auth(request): 9 | # if request.method=='GET' and request.user.is_authenticated(): 10 | # ur = UserResource() 11 | # ur_bundle = ur.build_bundle(obj=request.user, request=request) 12 | # return HttpResponse(ur.serialize(None, ur.full_dehydrate(ur_bundle), 'application/json')) 13 | # elif request.method=='POST' and request.POST['email'] and request.POST['password']: 14 | # user = authenticate(username=username, password=password) 15 | # return HttpResponse("AUTHED") 16 | # return HttpResponse("NOAUTH") 17 | 18 | def index(request): 19 | if request.method == 'POST': 20 | form = LoginForm(request.POST) 21 | if form.is_valid(): 22 | username = form.cleaned_data['username'] 23 | password = form.cleaned_data['password'] 24 | 25 | user = authenticate(username=username, password=password) 26 | if user is not None: 27 | if user.is_active: 28 | login(request, user) 29 | return render_to_response('index.html') 30 | elif request.GET and request.user.is_authenticated(): 31 | return render_to_response('index.html') 32 | return render_to_response('login.html') 33 | -------------------------------------------------------------------------------- /group/api.py: -------------------------------------------------------------------------------- 1 | from tastypie.resources import ModelResource 2 | from tastypie.authentication import ApiKeyAuthentication, SessionAuthentication 3 | from tastypie.authorization import DjangoAuthorization 4 | from tastypie import fields 5 | from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS 6 | 7 | from oval.api import UserResource 8 | from group.models import Group, Post, Tag 9 | 10 | class ApiKeyOrSessionAuthentication(ApiKeyAuthentication): 11 | def is_authenticated(self, request, **kwargs): 12 | if request.user.is_authenticated(): 13 | return True 14 | return super(ApiKeyOrSessionAuthentication, self).is_authenticated(request, **kwargs) 15 | 16 | def get_identifier(self, request): 17 | if request.user.is_authenticated(): 18 | return request.user.username 19 | return super(ApiKeyOrSessionAuthentication, self).get_identifier(request) 20 | # 21 | # def apply_limits(self, request, object_list=None): 22 | # if request and request.method in ('GET', 'DELETE'): # 1. 23 | # return object_list.filter(users__contains=request.user) 24 | # if isinstance(object_list, Bundle): # 2. 25 | # bundle = object_list # for clarity, lets call it a bundle 26 | # bundle.data['users'].contains(request.user) # 3. 27 | # return bundle 28 | # return [] 29 | 30 | class GroupResource(ModelResource): 31 | 32 | def determine_format(self, request): 33 | return 'application/json' 34 | 35 | users = fields.ToManyField(UserResource, attribute=lambda bundle: Group.objects.filter(users__pk=bundle.request.user.pk)) 36 | class Meta: 37 | 38 | queryset = Group.objects.all() 39 | resource_name = 'group' 40 | allowed_methods = ['get','put'] 41 | filtering = { 42 | 'users': ALL_WITH_RELATIONS, 43 | } 44 | 45 | authentication = SessionAuthentication() 46 | authorization = DjangoAuthorization() 47 | 48 | # def obj_create(self, bundle, request=None, **kwargs): 49 | # return super(EnvironmentResource, self).obj_create(bundle, request, user=request.user) 50 | 51 | def apply_authorization_limits(self, request, object_list): 52 | # if request.user.is_superuser: 53 | return object_list.filter(users__pk=request.user.pk) 54 | 55 | # def obj_get(self, bundle, request, **kwargs): 56 | # bundle = self._meta.authorization.apply_limits(request, bundle) 57 | # return super(GroupResource, self).obj_create(bundle, request, **kwargs) 58 | 59 | # def obj_create(self, bundle, request, **kwargs): # 5. 60 | # bundle = self._meta.authorization.apply_limits(request, bundle) 61 | # return super(GroupResource, self).obj_create(bundle, request, **kwargs) 62 | 63 | # def obj_update(self, bundle, request, **kwargs): # 6. 64 | # bundle = self._meta.authorization.apply_limits(request, bundle) 65 | # return super(GroupResource, self).obj_update(bundle, request, **kwargs) 66 | 67 | class PostResource(ModelResource): 68 | class Meta: 69 | authentication = ApiKeyOrSessionAuthentication() 70 | authorization = DjangoAuthorization() 71 | 72 | queryset = Post.objects.all() 73 | resource_name = 'post' 74 | allowed_methods = ['get','put'] 75 | 76 | class TagResource(ModelResource): 77 | class Meta: 78 | authentication = ApiKeyOrSessionAuthentication() 79 | authorization = DjangoAuthorization() 80 | 81 | queryset = Post.objects.all() 82 | resource_name = 'tag' 83 | allowed_methods = ['get','put'] 84 | 85 | -------------------------------------------------------------------------------- /oval/settings.py: -------------------------------------------------------------------------------- 1 | # Django settings for oval project. 2 | import os 3 | PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 4 | 5 | DEBUG = True 6 | TEMPLATE_DEBUG = DEBUG 7 | 8 | ADMINS = ( 9 | # ('Your Name', 'your_email@example.com'), 10 | ) 11 | 12 | MANAGERS = ADMINS 13 | 14 | DATABASES = { 15 | 'default': { 16 | 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 17 | 'NAME': 'oval', # Or path to database file if using sqlite3. 18 | 'USER': 'oval', # Not used with sqlite3. 19 | 'PASSWORD': 'oval', # Not used with sqlite3. 20 | 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 21 | 'PORT': '', # Set to empty string for default. Not used with sqlite3. 22 | } 23 | } 24 | 25 | # Hosts/domain names that are valid for this site; required if DEBUG is False 26 | # See https://docs.djangoproject.com/en/1.4/ref/settings/#allowed-hosts 27 | ALLOWED_HOSTS = [] 28 | 29 | # Local time zone for this installation. Choices can be found here: 30 | # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 31 | # although not all choices may be available on all operating systems. 32 | # In a Windows environment this must be set to your system time zone. 33 | TIME_ZONE = 'America/Chicago' 34 | 35 | # Language code for this installation. All choices can be found here: 36 | # http://www.i18nguy.com/unicode/language-identifiers.html 37 | LANGUAGE_CODE = 'en-us' 38 | 39 | SITE_ID = 1 40 | 41 | # If you set this to False, Django will make some optimizations so as not 42 | # to load the internationalization machinery. 43 | USE_I18N = True 44 | 45 | # If you set this to False, Django will not format dates, numbers and 46 | # calendars according to the current locale. 47 | USE_L10N = True 48 | 49 | # If you set this to False, Django will not use timezone-aware datetimes. 50 | USE_TZ = True 51 | 52 | # Absolute filesystem path to the directory that will hold user-uploaded files. 53 | # Example: "/home/media/media.lawrence.com/media/" 54 | MEDIA_ROOT = '' 55 | 56 | # URL that handles the media served from MEDIA_ROOT. Make sure to use a 57 | # trailing slash. 58 | # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 59 | MEDIA_URL = '' 60 | 61 | # Absolute path to the directory static files should be collected to. 62 | # Don't put anything in this directory yourself; store your static files 63 | # in apps' "static/" subdirectories and in STATICFILES_DIRS. 64 | # Example: "/home/media/media.lawrence.com/static/" 65 | STATIC_ROOT = '' 66 | 67 | # URL prefix for static files. 68 | # Example: "http://media.lawrence.com/static/" 69 | STATIC_URL = '/static/' 70 | 71 | # Additional locations of static files 72 | STATICFILES_DIRS = ( 73 | # Put strings here, like "/home/html/static" or "C:/www/django/static". 74 | # Always use forward slashes, even on Windows. 75 | # Don't forget to use absolute paths, not relative paths. 76 | ) 77 | 78 | # List of finder classes that know how to find static files in 79 | # various locations. 80 | STATICFILES_FINDERS = ( 81 | 'django.contrib.staticfiles.finders.FileSystemFinder', 82 | 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 83 | # 'django.contrib.staticfiles.finders.DefaultStorageFinder', 84 | ) 85 | 86 | # Make this unique, and don't share it with anybody. 87 | SECRET_KEY = '#jtrycd#zuxh5fqo81h$2i_kwdwmy=*3x2*nvw*_arm&jbhn#i' 88 | 89 | # List of callables that know how to import templates from various sources. 90 | TEMPLATE_LOADERS = ( 91 | 'django.template.loaders.filesystem.Loader', 92 | 'django.template.loaders.app_directories.Loader', 93 | # 'django.template.loaders.eggs.Loader', 94 | ) 95 | 96 | MIDDLEWARE_CLASSES = ( 97 | 'django.middleware.common.CommonMiddleware', 98 | 'django.contrib.sessions.middleware.SessionMiddleware', 99 | 'django.middleware.csrf.CsrfViewMiddleware', 100 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 101 | 'django.contrib.messages.middleware.MessageMiddleware', 102 | # Uncomment the next line for simple clickjacking protection: 103 | # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 104 | ) 105 | 106 | ROOT_URLCONF = 'oval.urls' 107 | 108 | # Python dotted path to the WSGI application used by Django's runserver. 109 | WSGI_APPLICATION = 'oval.wsgi.application' 110 | 111 | TEMPLATE_DIRS = ( 112 | # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 113 | # Always use forward slashes, even on Windows. 114 | # Don't forget to use absolute paths, not relative paths. 115 | os.path.join(PROJECT_ROOT, "templates"), 116 | ) 117 | 118 | INSTALLED_APPS = ( 119 | 'django.contrib.auth', 120 | 'django.contrib.contenttypes', 121 | 'django.contrib.sessions', 122 | 'django.contrib.sites', 123 | 'django.contrib.messages', 124 | 'django.contrib.staticfiles', 125 | 'django.contrib.admin', 126 | 'tastypie', 127 | 'group', 128 | ) 129 | 130 | # A sample logging configuration. The only tangible logging 131 | # performed by this configuration is to send an email to 132 | # the site admins on every HTTP 500 error when DEBUG=False. 133 | # See http://docs.djangoproject.com/en/dev/topics/logging for 134 | # more details on how to customize your logging configuration. 135 | LOGGING = { 136 | 'version': 1, 137 | 'disable_existing_loggers': False, 138 | 'filters': { 139 | 'require_debug_false': { 140 | '()': 'django.utils.log.RequireDebugFalse' 141 | } 142 | }, 143 | 'handlers': { 144 | 'mail_admins': { 145 | 'level': 'ERROR', 146 | 'filters': ['require_debug_false'], 147 | 'class': 'django.utils.log.AdminEmailHandler' 148 | } 149 | }, 150 | 'loggers': { 151 | 'django.request': { 152 | 'handlers': ['mail_admins'], 153 | 'level': 'ERROR', 154 | 'propagate': True, 155 | }, 156 | } 157 | } 158 | --------------------------------------------------------------------------------