Commit 96710a37 authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

remove unused imports & code refinement

parent eff121e3
......@@ -33,24 +33,14 @@
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.db import transaction
from urlparse import urljoin
from astakos.im.models import AstakosUser, Invitation
from astakos.im.forms import *
from astakos.im.models import AstakosUser
from astakos.im.util import get_invitation
from astakos.im.functions import send_verification, send_activation, \
send_admin_notification, activate, SendMailError
from astakos.im.settings import INVITATIONS_ENABLED, DEFAULT_CONTACT_EMAIL, \
DEFAULT_FROM_EMAIL, MODERATION_ENABLED, SITENAME, DEFAULT_ADMIN_EMAIL, RE_USER_EMAIL_PATTERNS
send_admin_notification, activate
from astakos.im.settings import INVITATIONS_ENABLED, MODERATION_ENABLED, SITENAME, RE_USER_EMAIL_PATTERNS
import socket
import logging
import re
......@@ -76,10 +66,13 @@ def get_backend(request):
try:
backend_class = getattr(mod, backend_class_name)
except AttributeError:
raise ImproperlyConfigured('Module "%s" does not define a activation backend named "%s"' % (module, attr))
raise ImproperlyConfigured('Module "%s" does not define a activation backend named "%s"' % (module, backend_class_name))
return backend_class(request)
class ActivationBackend(object):
def __init__(self, request):
self.request = request
def _is_preaccepted(self, user):
# return True if user email matches specific patterns
for pattern in RE_USER_EMAIL_PATTERNS:
......@@ -150,9 +143,6 @@ class InvitationsBackend(ActivationBackend):
account is created and the user is going to receive an email as soon as an
administrator activates his/her account.
"""
def __init__(self, request):
self.request = request
super(InvitationsBackend, self).__init__()
def get_signup_form(self, provider='local', instance=None):
"""
......@@ -215,10 +205,6 @@ class SimpleBackend(ActivationBackend):
supplies the necessary registation information, an incative user account is
created and receives an email in order to activate his/her account.
"""
def __init__(self, request):
self.request = request
super(SimpleBackend, self).__init__()
def _is_preaccepted(self, user):
if super(SimpleBackend, self)._is_preaccepted(user):
return True
......
......@@ -41,7 +41,7 @@ from django.conf import settings
from django.core.urlresolvers import reverse
from astakos.im.models import AstakosUser, GroupKind, Service, Resource
from astakos.im.api.faults import Fault, ItemNotFound, InternalServerError
from astakos.im.api.faults import Fault, ItemNotFound, InternalServerError, BadRequest
from astakos.im.settings import INVITATIONS_ENABLED, COOKIE_NAME, EMAILCHANGE_ENABLED
import logging
......@@ -83,7 +83,7 @@ def api_method(http_method=None):
def _get_user_by_username(user_id):
try:
user = AstakosUser.objects.get(username = user_id)
except AstakosUser.DoesNotExist, e:
except AstakosUser.DoesNotExist:
raise ItemNotFound('Invalid userid')
else:
response = HttpResponse()
......@@ -107,7 +107,7 @@ def _get_user_by_email(email):
raise BadRequest('Email missing')
try:
user = AstakosUser.objects.get(email = email)
except AstakosUser.DoesNotExist, e:
except AstakosUser.DoesNotExist:
raise ItemNotFound('Invalid email')
if not user.is_active:
......
......@@ -35,17 +35,15 @@ import logging
from functools import wraps
from time import time, mktime
from urlparse import urlparse
from collections import defaultdict
from django.http import HttpResponse
from django.utils import simplejson as json
from astakos.im.api.faults import *
from astakos.im.api import render_fault
from astakos.im.models import AstakosUser, Service
from astakos.im.api.faults import (Fault, Unauthorized, InternalServerError, BadRequest,
Forbidden)
from astakos.im.api import render_fault, _get_user_by_email, _get_user_by_username
from astakos.im.models import AstakosUser
from astakos.im.util import epoch
from astakos.im.api import _get_user_by_email, _get_user_by_username
logger = logging.getLogger(__name__)
format = ('%a, %d %b %Y %H:%M:%S GMT')
......
......@@ -35,18 +35,15 @@ import logging
from functools import wraps
from time import time, mktime
from urlparse import urlparse
from collections import defaultdict
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from astakos.im.api.faults import *
from astakos.im.api import render_fault
from astakos.im.api.faults import Fault, Unauthorized, InternalServerError, BadRequest
from astakos.im.api import render_fault, _get_user_by_email, _get_user_by_username
from astakos.im.models import AstakosUser, Service
from astakos.im.util import epoch
from astakos.im.forms import FeedbackForm
from astakos.im.functions import send_feedback as send_feedback_func, SendMailError
from astakos.im.functions import send_feedback as send_feedback_func
logger = logging.getLogger(__name__)
......
......@@ -32,14 +32,12 @@
# or implied, of GRNET S.A.
from astakos.im.settings import IM_MODULES, INVITATIONS_ENABLED, IM_STATIC_URL, \
COOKIE_NAME, LOGIN_MESSAGES, SIGNUP_MESSAGES, PROFILE_MESSAGES, \
LOGIN_MESSAGES, SIGNUP_MESSAGES, PROFILE_MESSAGES, \
GLOBAL_MESSAGES, PROFILE_EXTRA_LINKS
from astakos.im.api import get_menu
from astakos.im.util import get_query
from astakos.im.models import GroupKind
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils import simplejson as json
def im_modules(request):
......
......@@ -31,34 +31,32 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from urlparse import urljoin
from datetime import datetime
from django import forms
from django.utils.translation import ugettext as _
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, \
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm,
PasswordResetForm, PasswordChangeForm
)
from django.core.mail import send_mail
from django.contrib.auth.tokens import default_token_generator
from django.template import Context, loader
from django.utils.http import int_to_base36
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from django.utils.safestring import mark_safe
from django.contrib import messages
from django.utils.encoding import smart_str
from django.forms.extras.widgets import SelectDateWidget
from django.db.models import Q
from django.db.models.query import EmptyQuerySet
from astakos.im.models import *
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, \
BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL, \
from astakos.im.models import (AstakosUser, EmailChange, AstakosGroup, Invitation,
Membership, GroupKind, get_latest_terms
)
from astakos.im.settings import (INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL,
BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL,
RECAPTCHA_ENABLED, LOGGING_LEVEL
)
from astakos.im.widgets import DummyWidget, RecaptchaWidget
from astakos.im.functions import send_change_email
# since Django 1.4 use django.core.urlresolvers.reverse_lazy instead
from astakos.im.util import reverse_lazy, reserved_email, get_query
from astakos.im.util import reserved_email, get_query
import logging
import hashlib
......@@ -150,7 +148,7 @@ class LocalUserCreationForm(UserCreationForm):
user.renew_token()
if commit:
user.save()
logger._log(LOGGING_LEVEL, 'Created user %s' % user.email, [])
logger.log(LOGGING_LEVEL, 'Created user %s' % user.email)
return user
class InvitedLocalUserCreationForm(LocalUserCreationForm):
......@@ -231,7 +229,7 @@ class ThirdPartyUserCreationForm(forms.ModelForm):
user.provider = get_query(self.request).get('provider')
if commit:
user.save()
logger._log(LOGGING_LEVEL, 'Created user %s' % user.email, [])
logger.log(LOGGING_LEVEL, 'Created user %s' % user.email)
return user
class InvitedThirdPartyUserCreationForm(ThirdPartyUserCreationForm):
......@@ -392,7 +390,7 @@ class ExtendedPasswordResetForm(PasswordResetForm):
user = AstakosUser.objects.get(email=email, is_active=True)
if not user.has_usable_password():
raise forms.ValidationError(_("This account has not a usable password."))
except AstakosUser.DoesNotExist, e:
except AstakosUser.DoesNotExist:
raise forms.ValidationError(_('That e-mail address doesn\'t have an associated user account. Are you sure you\'ve registered?'))
return email
......
......@@ -51,7 +51,7 @@ from functools import wraps
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, \
SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL, LOGGING_LEVEL
from astakos.im.models import Invitation, AstakosUser
from astakos.im.models import AstakosUser
logger = logging.getLogger(__name__)
......@@ -68,7 +68,7 @@ def logged(func, msg):
email = user.email if user and user.is_authenticated() else ''
r = func(*args, **kwargs)
if LOGGING_LEVEL:
logger._log(LOGGING_LEVEL, msg % email, [])
logger.log(LOGGING_LEVEL, msg % email)
return r
return with_logging
......@@ -98,7 +98,7 @@ def send_verification(user, template_name='im/activation_email.txt'):
raise SendVerificationError()
else:
msg = 'Sent activation %s' % user.email
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_activation(user, template_name='im/activation_email.txt'):
send_verification(user, template_name)
......@@ -106,7 +106,7 @@ def send_activation(user, template_name='im/activation_email.txt'):
user.save()
def send_admin_notification(template_name,
dictionary={},
dictionary=None,
subject='alpha2 testing notification',
):
"""
......@@ -116,6 +116,7 @@ def send_admin_notification(template_name,
"""
if not DEFAULT_ADMIN_EMAIL:
return
dictionary = dictionary or {}
message = render_to_string(template_name, dictionary)
sender = DEFAULT_FROM_EMAIL
try:
......@@ -125,7 +126,7 @@ def send_admin_notification(template_name,
raise SendNotificationError()
else:
msg = 'Sent admin notification for user %s' % dictionary
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_helpdesk_notification(user, template_name='im/account_notification.txt'):
"""
......@@ -152,7 +153,7 @@ def send_helpdesk_notification(user, template_name='im/account_notification.txt'
raise SendNotificationError()
else:
msg = 'Sent helpdesk admin notification for %s' % user.email
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_invitation(invitation, template_name='im/invitation.txt'):
"""
......@@ -162,7 +163,7 @@ def send_invitation(invitation, template_name='im/invitation.txt'):
"""
subject = _('Invitation to %s alpha2 testing' % SITENAME)
url = '%s?code=%d' % (urljoin(BASEURL, reverse('index')), invitation.code)
message = render_to_string('im/invitation.txt', {
message = render_to_string(template_name, {
'invitation': invitation,
'url': url,
'baseurl': BASEURL,
......@@ -176,7 +177,7 @@ def send_invitation(invitation, template_name='im/invitation.txt'):
raise SendInvitationError()
else:
msg = 'Sent invitation %s' % invitation
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_greeting(user, email_template_name='im/welcome_email.txt'):
"""
......@@ -199,7 +200,7 @@ def send_greeting(user, email_template_name='im/welcome_email.txt'):
raise SendGreetingError()
else:
msg = 'Sent greeting %s' % user.email
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'):
subject = _("Feedback from %s alpha2 testing" % SITENAME)
......@@ -216,7 +217,7 @@ def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'):
raise SendFeedbackError()
else:
msg = 'Sent feedback from %s' % user.email
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def send_change_email(ec, request, email_template_name='registration/email_change_email.txt'):
try:
......@@ -233,7 +234,7 @@ def send_change_email(ec, request, email_template_name='registration/email_chang
raise ChangeEmailError()
else:
msg = 'Sent change email for %s' % ec.user.email
logger._log(LOGGING_LEVEL, msg, [])
logger.log(LOGGING_LEVEL, msg)
def activate(user, email_template_name='im/welcome_email.txt',
helpdesk_email_template_name='im/helpdesk_notification.txt', verify_email=False):
......
......@@ -104,7 +104,7 @@ def remove_user_permission(user, pname):
content_type=content_type)
user.user_permissions.remove(p)
return 1
except Permission.DoesNotExist, e:
except Permission.DoesNotExist:
return -1
def remove_group_permission(group, pname):
......@@ -116,5 +116,5 @@ def remove_group_permission(group, pname):
content_type=content_type)
group.permissions.remove(p)
return 1
except Permission.DoesNotExist, e:
except Permission.DoesNotExist:
return -1
\ No newline at end of file
......@@ -31,13 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from random import choice
from string import digits, lowercase, uppercase
from uuid import uuid4
from time import time
from os.path import abspath
from django.core.management.base import BaseCommand, CommandError
from astakos.im.models import AstakosGroup
......
......@@ -35,7 +35,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from astakos.im.models import AstakosUser, AstakosGroup
from astakos.im.models import AstakosGroup
from ._common import format_bool
......
......@@ -34,12 +34,9 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from astakos.im.models import AstakosUser, AstakosGroup
from ._common import add_group_permission
from astakos.im.models import AstakosGroup
from ._common import add_group_permission, remove_group_permission
class Command(BaseCommand):
args = "<groupname>"
......@@ -73,9 +70,6 @@ class Command(BaseCommand):
raise CommandError("Invalid group")
try:
content_type = ContentType.objects.get(app_label='im',
model='astakosuser')
pname = options.get('add-permission')
if pname:
r, created = add_group_permission(group, pname)
......
......@@ -31,14 +31,10 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import socket
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db.utils import IntegrityError
from astakos.im.models import Resource, ResourceMetadata, Service
from astakos.im.models import Resource, Service
class Command(BaseCommand):
args = "<service> <resource> [<key>=<value>...]"
......
......@@ -31,11 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from random import choice
from string import digits, lowercase, uppercase
from uuid import uuid4
from time import time
from os.path import abspath
from django.core.management.base import BaseCommand, CommandError
......@@ -52,7 +47,7 @@ class Command(BaseCommand):
location = abspath(args[0].decode('utf8'))
try:
f = open(location, 'r')
open(location, 'r')
except IOError:
raise CommandError("Invalid location")
......
......@@ -34,8 +34,6 @@
import socket
from optparse import make_option
from random import choice
from string import digits, lowercase, uppercase
from uuid import uuid4
from datetime import datetime
......
......@@ -31,8 +31,6 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import socket
from django.core.management.base import BaseCommand, CommandError
from django.db.utils import IntegrityError
from django.db import transaction
......
......@@ -35,8 +35,6 @@ from optparse import make_option
from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
......
......@@ -35,7 +35,6 @@ from urllib import unquote
from django.contrib.auth import authenticate
from astakos.im.settings import COOKIE_NAME
from astakos.im.models import AstakosUser
from astakos.im.functions import login
class CookieAuthenticationMiddleware(object):
......
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
......
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
......
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment