Commit 374611bc authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

remove site framework, introduce invite function

parent 5588197b
......@@ -36,12 +36,16 @@ import logging
from django.utils.translation import ugettext as _
from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from urlparse import urljoin
from random import randint
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITEURL, SITENAME, BASEURL
from astakos.im.models import Invitation
logger = logging.getLogger(__name__)
def activate(user, sitename, sitedomain, baseurl, email_template_name='welcome_email.txt'):
def activate(user, email_template_name='welcome_email.txt'):
"""
Activates the specific user and sends email.
......@@ -49,13 +53,48 @@ def activate(user, sitename, sitedomain, baseurl, email_template_name='welcome_e
"""
user.is_active = True
user.save()
subject = _('Welcome to %s' % sitename)
subject = _('Welcome to %s' % SITENAME)
message = render_to_string(email_template_name, {
'user': user,
'url': sitedomain,
'baseurl': baseurl,
'site_name': sitename,
'support': DEFAULT_CONTACT_EMAIL % sitename.lower()})
sender = DEFAULT_FROM_EMAIL % sitename
'url': SITEURL,
'baseurl': BASEURL,
'site_name': SITENAME,
'support': DEFAULT_CONTACT_EMAIL % SITENAME.lower()})
sender = DEFAULT_FROM_EMAIL % SITENAME
send_mail(subject, message, sender, [user.email])
logger.info('Sent greeting %s', user)
def _generate_invitation_code():
while True:
code = randint(1, 2L**63 - 1)
try:
Invitation.objects.get(code=code)
# An invitation with this code already exists, try again
except Invitation.DoesNotExist:
return code
def invite(inviter, username, realname):
"""
Send an invitation email and upon success reduces inviter's invitation by one.
Raises SMTPException, socket.error
"""
code = _generate_invitation_code()
invitation = Invitation(inviter=inviter,
username=username,
code=code,
realname=realname)
invitation.save()
subject = _('Invitation to %s' % SITENAME)
url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.signup')), code)
message = render_to_string('im/invitation.txt', {
'invitation': invitation,
'url': url,
'baseurl': BASEURL,
'service': SITENAME,
'support': DEFAULT_CONTACT_EMAIL % SITENAME.lower()})
sender = DEFAULT_FROM_EMAIL % SITENAME
send_mail(subject, message, sender, [invitation.username])
logger.info('Sent invitation %s', invitation)
inviter.invitations = max(0, inviter.invitations - 1)
inviter.save()
\ No newline at end of file
......@@ -48,7 +48,7 @@ from django.contrib import messages
from django.db import transaction
from astakos.im.models import AstakosUser, Invitation
from astakos.im.util import get_context, get_current_site
from astakos.im.util import get_context
from astakos.im.forms import *
from astakos.im.views import render_response, index
from astakos.im.admin.forms import AdminProfileForm
......@@ -335,9 +335,7 @@ def users_activate(request, user_id, template_name='pending_users.html', extra_c
user = AstakosUser.objects.get(id=user_id)
status = messages.SUCCESS
try:
sitename, sitedomain = get_current_site(request, use_https=request.is_secure())
baseurl = request.build_absolute_uri('/').rstrip('/')
activate(user, sitename, sitedomain, baseurl, email_template_name)
activate(user, email_template_name)
message = _('Greeting sent to %s' % user.email)
transaction.commit()
except (SMTPException, socket.error) as e:
......
......@@ -43,11 +43,12 @@ from django.core.urlresolvers import reverse
from smtplib import SMTPException
from urllib import quote
from urlparse import urljoin
from astakos.im.models import AstakosUser, Invitation
from astakos.im.forms import *
from astakos.im.util import get_invitation
from astakos.im.settings import INVITATIONS_ENABLED, DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, MODERATION_ENABLED
from astakos.im.settings import INVITATIONS_ENABLED, DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, MODERATION_ENABLED, SITEURL, BASEURL
import socket
import logging
......@@ -244,15 +245,13 @@ class SimpleBackend(object):
def _send_verification(request, user, template_name):
site = Site.objects.get_current()
baseurl = request.build_absolute_uri('/').rstrip('/')
url = '%s%s?auth=%s&next=%s' % (baseurl,
reverse('astakos.im.views.activate'),
url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('astakos.im.views.activate')),
quote(user.auth_token),
quote(baseurl))
quote(BASEURL))
message = render_to_string(template_name, {
'user': user,
'url': url,
'baseurl': baseurl,
'baseurl': BASEURL,
'site_name': site.name,
'support': DEFAULT_CONTACT_EMAIL % site.name.lower()})
sender = DEFAULT_FROM_EMAIL % site.name
......
......@@ -38,10 +38,10 @@ 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 astakos.im.models import AstakosUser
from astakos.im.util import get_current_site
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, SITENAME, SITEURL
import logging
......@@ -218,17 +218,15 @@ class ExtendedPasswordResetForm(PasswordResetForm):
Generates a one-use only link for resetting password and sends to the user.
"""
for user in self.users_cache:
site_name, sitedomain = get_current_site(request, use_https=use_https)
t = loader.get_template(email_template_name)
c = {
'email': user.email,
'domain': sitedomain.split('://')[-1],
'site_name': site_name,
'domain': SITEURL,
'site_name': SITENAME,
'uid': int_to_base36(user.id),
'user': user,
'token': token_generator.make_token(user),
'protocol': use_https and 'https' or 'http',
'token': token_generator.make_token(user)
}
from_email = DEFAULT_FROM_EMAIL % site_name
send_mail(_("Password reset on %s") % site_name,
from_email = DEFAULT_FROM_EMAIL % SITENAME
send_mail(_("Password reset on %s") % SITENAME,
t.render(Context(c)), from_email, [user.email])
from django.conf import settings
from os.path import abspath, dirname, join
from urlparse import urlparse
PROJECT_PATH = getattr(settings, 'PROJECT_PATH', dirname(dirname(abspath(__file__))))
......@@ -48,6 +49,13 @@ IM_STATIC_URL = getattr(settings, 'ASTAKOS_IM_STATIC_URL', '/im/static/im/')
# If set to False and invitations not enabled newly created user will be automatically accepted
MODERATION_ENABLED = getattr(settings, 'ASTAKOS_MODERATION_ENABLED', True)
# Set baseurl
SITEURL = getattr(settings, 'ASTAKOS_SITEURL', 'http://pithos.dev.grnet.gr/im')
BASEURL = getattr(settings, 'ASTAKOS_BASEURL', 'http://pithos.dev.grnet.gr')
# Set service name
SITENAME = getattr(settings, 'ASTAKOS_SITENAME', 'Pithos+')
# SQLAlchemy (choose SQLite/MySQL/PostgreSQL).
BACKEND_DB_MODULE = getattr(settings, 'PITHOS_BACKEND_DB_MODULE', 'pithos.backends.lib.sqlalchemy')
BACKEND_DB_CONNECTION = getattr(settings, 'PITHOS_BACKEND_DB_CONNECTION', 'sqlite:///' + join(PROJECT_PATH, 'backend.db'))
......@@ -59,4 +67,3 @@ BACKEND_BLOCK_PATH = getattr(settings, 'PITHOS_BACKEND_BLOCK_PATH', join(PROJECT
# Default setting for new accounts.
BACKEND_QUOTA = getattr(settings, 'PITHOS_BACKEND_QUOTA', 50 * 1024 * 1024 * 1024)
BACKEND_VERSIONING = getattr(settings, 'PITHOS_BACKEND_VERSIONING', 'auto')
......@@ -3,7 +3,7 @@
για την υπηρεσία {{ site_name }} της ΕΔΕΤ κατά την Alpha (πιλοτική) φάση λειτουργίας της,
χρησιμοποιήστε τον παρακάτω σύνδεσμο:
{{ protocol }}://{{ domain }}/local/reset/confirm/{{ uid }}-{{ token }}/
{{ domain }}/local/reset/confirm/{{ uid }}-{{ token }}/
Σημείωση:
......@@ -17,7 +17,7 @@
έκδοση Alpha στην έκδοση Beta. Θα υπάρξει έγκαιρη ειδοποίησή σας πριν
από τη μετάβαση αυτή.
Περισσότερα για την υπηρεσία θα βρείτε στο {{ protocol }}://{{ domain }}/, αφού
Περισσότερα για την υπηρεσία θα βρείτε στο {{ domain }}/, αφού
ενεργοποιήσετε την πρόσκλησή σας.
Για όποιες παρατηρήσεις ή προβλήματα στη λειτουργεία της υπηρεσίας μπορείτε να
......@@ -36,7 +36,7 @@
You can use the following link:
{{ protocol }}://{{ domain }}/local/reset/confirm/{{ uid }}-{{ token }}/
{{ domain }}/local/reset/confirm/{{ uid }}-{{ token }}/
to reset your password for GRNET's {{ site_name }} service has been created
for its Alpha test phase.
......@@ -53,7 +53,7 @@ request you not to transfer important files to {{ site_name}} yet.
Also, please bear in mind that all data will be deleted when the service moves to Beta.
We will notify you before the transition.
For more information, please visit {{ protocol }}://{{ domain }}/, after
For more information, please visit {{ domain }}/, after
activating your invitation.
We look forward to your feedback, to improve the functionality and
......
......@@ -94,14 +94,6 @@ def get_context(request, extra_context={}, **kwargs):
extra_context.update(kwargs)
return RequestContext(request, extra_context)
def get_current_site(request, use_https=False):
"""
returns the current site name and full domain (including prorocol)
"""
protocol = use_https and 'https' or 'http'
site = Site.objects.get_current()
return site.name, '%s://%s' % (protocol, site.domain)
def get_invitation(request):
"""
Returns the invitation identified by the ``code``.
......
......@@ -34,7 +34,6 @@
import logging
import socket
from random import randint
from smtplib import SMTPException
from urllib import quote
from functools import wraps
......@@ -54,9 +53,10 @@ from django.http import HttpResponseRedirect
from astakos.im.models import AstakosUser, Invitation
from astakos.im.backends import get_backend
from astakos.im.util import get_context, get_current_site, prepare_response
from astakos.im.util import get_context, prepare_response
from astakos.im.forms import *
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_NAME, IM_MODULES
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_NAME, IM_MODULES, SITENAME, SITEURL, BASEURL
from astakos.im.admin.functions import invite as invite_func
logger = logging.getLogger(__name__)
......@@ -120,30 +120,6 @@ def index(request, login_template_name='im/login.html', profile_template_name='i
form = globals()[formclass](**kwargs),
context_instance = get_context(request, extra_context))
def _generate_invitation_code():
while True:
code = randint(1, 2L**63 - 1)
try:
Invitation.objects.get(code=code)
# An invitation with this code already exists, try again
except Invitation.DoesNotExist:
return code
def _send_invitation(request, baseurl, inv):
sitename, sitedomain = get_current_site(request, use_https=request.is_secure())
subject = _('Invitation to %s' % sitename)
baseurl = request.build_absolute_uri('/').rstrip('/')
url = '%s%s?code=%d' % (baseurl, reverse('astakos.im.views.signup'), inv.code)
message = render_to_string('im/invitation.txt', {
'invitation': inv,
'url': url,
'baseurl': baseurl,
'service': sitename,
'support': DEFAULT_CONTACT_EMAIL % sitename.lower()})
sender = DEFAULT_FROM_EMAIL % sitename
send_mail(subject, message, sender, [inv.username])
logger.info('Sent invitation %s', inv)
@login_required
@transaction.commit_manually
def invite(request, template_name='im/invitations.html', extra_context={}):
......@@ -189,18 +165,8 @@ def invite(request, template_name='im/invitations.html', extra_context={}):
realname = request.POST.get('realname')
if inviter.invitations > 0:
code = _generate_invitation_code()
invitation = Invitation(inviter=inviter,
username=username,
code=code,
realname=realname)
invitation.save()
try:
baseurl = request.build_absolute_uri('/').rstrip('/')
_send_invitation(request, baseurl, invitation)
inviter.invitations = max(0, inviter.invitations - 1)
inviter.save()
invite_func(inviter, username, realname)
status = messages.SUCCESS
message = _('Invitation sent to %s' % username)
transaction.commit()
......@@ -378,10 +344,9 @@ def send_feedback(request, template_name='im/feedback.html', email_template_name
form = FeedbackForm(request.POST)
if form.is_valid():
sitename, sitedomain = get_current_site(request, use_https=request.is_secure())
subject = _("Feedback from %s" % sitename)
subject = _("Feedback from %s" % SITENAME)
from_email = request.user.email
recipient_list = [DEFAULT_CONTACT_EMAIL % sitename.lower()]
recipient_list = [DEFAULT_CONTACT_EMAIL % SITENAME.lower()]
content = render_to_string(email_template_name, {
'message': form.cleaned_data['feedback_msg'],
'data': form.cleaned_data['feedback_data'],
......
......@@ -155,6 +155,5 @@ INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.sessions'
)
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