Permalink
Browse files

refactoring - tabs to spaces + settings to conf

  • Loading branch information...
1 parent b3ee05a commit 9b98cd01cd7e2ad2403e6d4760235b5f0880c0e1 @zalun zalun committed Sep 30, 2010
View
@@ -1,21 +0,0 @@
-This is a document which will carry notes while coding offline
-This content will be send to goplanapp into discussions.
-Please delete only your staff.
-Start coding on the top.
--------------------------------------------------------------
-
-zalun - 2010-02-17 - Saving dependencies
-----------------------------------------
-There is a problem with editing dependencies and saving them together
-with the main edited file. It just doesn't seem to work well to force
-user to use the same save action for all edited files.
-
-What I suggest is to display a popUp onAnySave if any capability was changed.
-Whithin that popUp there will be a list of changed elements. User will have
-an if item should be saved and in which way (new version/update).
-
-The system is designed to connect saving of the model only if its content
-has been changed - however it will to wait until proper onChange event will
-be written. Currently the system will send the onChange event after onBlur,
-which may bring some problems. I guess using the BespinEmbedded will solve
-this issue.
@@ -1,20 +0,0 @@
-# Taken from feincms
-# http://github.com/matthiask/feincms/blob/master/feincms/__init__.py
-
-# Do not use Django settings at module level as recommended
-from django.utils.functional import LazyObject
-
-class LazySettings(LazyObject):
- def _setup(self):
- from amo import default_settings
- self._wrapped = Settings(default_settings)
-
-class Settings(object):
- def __init__(self, settings_module):
- for setting in dir(settings_module):
- if setting == setting.upper():
- setattr(self, setting, getattr(settings_module, setting))
-
-settings = LazySettings()
-
-
@@ -5,140 +5,140 @@
from django.utils.encoding import smart_str
from person.models import Profile, Limit
-from amo import settings
+from amo import conf
DEFAULT_AMO_PASSWORD = 'saved in AMO'
class AMOAuthentication:
- def authenticate(self, username, password):
- """
- Authenticate user by contacting with AMO
- """
-
-
- # TODO: Validate alphanum + .-_@
-
- # check if username exists in database
- try:
- user = User.objects.get(username=username)
- # was user signed up via AMO?
- if user.password != DEFAULT_AMO_PASSWORD:
- # standard authorisation
- if user.check_password(password):
- try:
- profile = user.get_profile()
- except:
- # create empty profile for users stored in FD database
- profile = Profile(user=user)
- profile.save()
- return user
- return None
- except User.DoesNotExist:
- # username does not exist in FD database
- user = None
-
-
- # if access to the FD is limited
- if settings.AMO_LIMITED_ACCESS:
- if username not in [x.email for x in list(Limit.objects.all())]:
- return None
-
- if not settings.AUTH_DATABASE:
- return None
-
- # here contact AMO and receive authentication status
- email = username
- username = self.auth_db_authenticate(username, password)
-
- if not username:
- return None
-
- # check if user was already signed to FD
- try:
- user = User.objects.get(username=username)
- # update user's email if needed
- if user.email != email:
- user.email = email
- user.save()
- except:
- # save user into the database
- user = User(
- username=username,
- email=email,
- password=DEFAULT_AMO_PASSWORD,
- )
- user.save()
-
- # Manage profile
- try:
- profile = user.get_profile()
- except Profile.DoesNotExist:
- profile = Profile(user=user)
-
- # update profile and return User instance
- return update_profile(user, profile, self.user_data)
-
-
- def get_user(self, user_id):
- try:
- return User.objects.get(pk=user_id)
- except:
- return None
-
-
- def auth_db_authenticate(self, username, password):
- " authenticate email/password pair in MAO database "
- columns = ('id', 'email','username','display_name','password','homepage')
-
- auth_conn = MySQLdb.connect(
- host=settings.AUTH_DATABASE['HOST'],
- user=settings.AUTH_DATABASE['USER'],
- passwd=settings.AUTH_DATABASE['PASSWORD'],
- db=settings.AUTH_DATABASE['NAME']
- )
- auth_cursor = auth_conn.cursor()
- SQL = 'SELECT %s FROM %s WHERE email="%s"' % (','.join(columns), settings.AUTH_DATABASE['TABLE'], username)
- auth_cursor.execute(SQL)
- data = auth_cursor.fetchone()
- user_data = {}
- for i in range(len(data)):
- user_data[columns[i]] = data[i]
- if not user_data:
- return None
-
- if '$' not in user_data['password']:
- valid = (get_hexdigest('md5', '', password) == user_data['password'])
- else:
- algo, salt, hsh = user_data['password'].split('$')
- valid = (hsh == get_hexdigest(algo, salt, password))
-
- if not valid:
- return None
-
- username = user_data['id']
- self.user_data = user_data
- return username
-
-
+ def authenticate(self, username, password):
+ """
+ Authenticate user by contacting with AMO
+ """
+
+
+ # TODO: Validate alphanum + .-_@
+
+ # check if username exists in database
+ try:
+ user = User.objects.get(username=username)
+ # was user signed up via AMO?
+ if user.password != DEFAULT_AMO_PASSWORD:
+ # standard authorisation
+ if user.check_password(password):
+ try:
+ profile = user.get_profile()
+ except:
+ # create empty profile for users stored in FD database
+ profile = Profile(user=user)
+ profile.save()
+ return user
+ return None
+ except User.DoesNotExist:
+ # username does not exist in FD database
+ user = None
+
+
+ # if access to the FD is limited
+ if conf.AMO_LIMITED_ACCESS:
+ if username not in [x.email for x in list(Limit.objects.all())]:
+ return None
+
+ if not conf.AUTH_DATABASE:
+ return None
+
+ # here contact AMO and receive authentication status
+ email = username
+ username = self.auth_db_authenticate(username, password)
+
+ if not username:
+ return None
+
+ # check if user was already signed to FD
+ try:
+ user = User.objects.get(username=username)
+ # update user's email if needed
+ if user.email != email:
+ user.email = email
+ user.save()
+ except:
+ # save user into the database
+ user = User(
+ username=username,
+ email=email,
+ password=DEFAULT_AMO_PASSWORD,
+ )
+ user.save()
+
+ # Manage profile
+ try:
+ profile = user.get_profile()
+ except Profile.DoesNotExist:
+ profile = Profile(user=user)
+
+ # update profile and return User instance
+ return update_profile(user, profile, self.user_data)
+
+
+ def get_user(self, user_id):
+ try:
+ return User.objects.get(pk=user_id)
+ except:
+ return None
+
+
+ def auth_db_authenticate(self, username, password):
+ " authenticate email/password pair in MAO database "
+ columns = ('id', 'email','username','display_name','password','homepage')
+
+ auth_conn = MySQLdb.connect(
+ host=conf.AUTH_DATABASE['HOST'],
+ user=conf.AUTH_DATABASE['USER'],
+ passwd=conf.AUTH_DATABASE['PASSWORD'],
+ db=conf.AUTH_DATABASE['NAME']
+ )
+ auth_cursor = auth_conn.cursor()
+ SQL = 'SELECT %s FROM %s WHERE email="%s"' % (','.join(columns), conf.AUTH_DATABASE['TABLE'], username)
+ auth_cursor.execute(SQL)
+ data = auth_cursor.fetchone()
+ user_data = {}
+ for i in range(len(data)):
+ user_data[columns[i]] = data[i]
+ if not user_data:
+ return None
+
+ if '$' not in user_data['password']:
+ valid = (get_hexdigest('md5', '', password) == user_data['password'])
+ else:
+ algo, salt, hsh = user_data['password'].split('$')
+ valid = (hsh == get_hexdigest(algo, salt, password))
+
+ if not valid:
+ return None
+
+ username = user_data['id']
+ self.user_data = user_data
+ return username
+
+
def update_profile(user, profile, data):
- if 'display_name' in data:
- if data['display_name']:
- names = data['display_name'].split(' ')
- user.firstname = names[0]
- if len(names) > 1:
- user.lastname = names[-1]
- user.save()
-
- if 'username' in data:
- profile.nickname = data['username']
- if 'homepage' in data:
- profile.homepage = data['homepage']
-
- profile.save()
-
- return user
-
+ if 'display_name' in data:
+ if data['display_name']:
+ names = data['display_name'].split(' ')
+ user.firstname = names[0]
+ if len(names) > 1:
+ user.lastname = names[-1]
+ user.save()
+
+ if 'username' in data:
+ profile.nickname = data['username']
+ if 'homepage' in data:
+ profile.homepage = data['homepage']
+
+ profile.save()
+
+ return user
+
def get_hexdigest(algorithm, salt, raw_password):
- return hashlib.new(algorithm, smart_str(salt + raw_password)).hexdigest()
+ return hashlib.new(algorithm, smart_str(salt + raw_password)).hexdigest()
File renamed without changes.
@@ -1,3 +0,0 @@
-from django.db import models
-
-# Create your models here.
@@ -1,17 +1,15 @@
from django.test import TestCase
-from django.test.client import Client
-from django.contrib.auth.models import User
from django.contrib.auth import authenticate
-from person.models import Profile
class AuthTest(TestCase):
-
- def test_failing_login(self):
- # testing failed authentication on AMO
- self.assertEqual(
- None,
- authenticate(
- username='non existing',
- password='user')
- )
+
+ def test_failing_login(self):
+ # testing failed authentication on AMO
+ # this test assumes FlightDeck has access to AMO database
+ self.assertEqual(
+ None,
+ authenticate(
+ username='non existing',
+ password='user')
+ )
@@ -1 +0,0 @@
-# Create your views here.
@@ -1,18 +0,0 @@
-# Taken from feincms
-# http://github.com/matthiask/feincms/blob/master/feincms/__init__.py
-
-# Do not use Django settings at module level as recommended
-from django.utils.functional import LazyObject
-
-class LazySettings(LazyObject):
- def _setup(self):
- from api import default_settings
- self._wrapped = Settings(default_settings)
-
-class Settings(object):
- def __init__(self, settings_module):
- for setting in dir(settings_module):
- if setting == setting.upper():
- setattr(self, setting, getattr(settings_module, setting))
-
-settings = LazySettings()
File renamed without changes.
@@ -1,3 +0,0 @@
-from django.db import models
-
-# Create your models here.
@@ -1,19 +1,20 @@
"""
-Testing the cuddlefish engine to export API
+Testing the cuddlefish engine to export API
"""
import os
from django.test import TestCase
from cuddlefish import apiparser
-from api import settings
+from api import conf
class CuddleTest(TestCase):
- def test_basic(self):
- """
- exporting hunks
- """
- docs_dir = os.path.join(settings.VIRTUAL_ENV,'src/jetpack-sdk/packages/jetpack-core/docs')
- text = open(os.path.join(docs_dir,'url.md')).read()
- self.failUnless(len(list(apiparser.parse_hunks(text))) > 0)
+ def test_basic(self):
+ """
+ exporting hunks
+ """
+ #XXX: the path is different now
+ docs_dir = os.path.join(conf.FRAMEWORK_PATH,'sdk_versions/jetpack-sdk/packages/jetpack-core/docs')
+ text = open(os.path.join(docs_dir,'url.md')).read()
+ self.failUnless(len(list(apiparser.parse_hunks(text))) > 0)
Oops, something went wrong.

0 comments on commit 9b98cd0

Please sign in to comment.