def notify_users(request, folder, files, upload=True): if folder.inherit_permissions: parents = folder.get_ancestors() for f in parents: if f.inherit_permissions is False: folder = f break users_list = get_acl_by_object(folder, 'reader') email = ZornaEmail() url = request.build_absolute_uri(reverse( 'documents', args=[])) + '?dir=F%s' % folder.pk ec = {"folder": folder, "files": files, 'url': url, 'upload': upload, 'user': request.user} body_text = render_to_string('fileman/email_notification_text.html', ec) body_html = render_to_string('fileman/email_notification_html.html', ec) if upload: subject = _(u'A new file has been uploaded') else: subject = _(u'A file has been updated') step = getattr(settings, "ZORNA_MAIL_MAXPERPACKET", 25) users_email = [user.email for user in users_list] for n in range(0, len(users_email) / step + 1): email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, bcc=users_email[ n * step:(n + 1) * step]) email.send()
def forgot_passord(request): if request.method == "POST": form = RequestNewPasswordForm(data=request.POST) if form.is_valid(): user = User.objects.get(Q(username=form.cleaned_data[ 'username']) | Q(email=form.cleaned_data['username'])) up = user.get_profile() email = ZornaEmail() subject = _(u'Password reset request') h = hashlib.sha1() h.update(str(random.random())) salt = h.hexdigest()[:5] h.update(salt + user.username) hashcode = h.hexdigest() up.reset_password_key = hashcode up.save() ec = { 'user': user, 'site': Site.objects.get_current(), 'url': request.build_absolute_uri(reverse('request_password')) + '?user=%s&auth=%s' % (user.username, hashcode), } body_text = render_to_string( 'account/email_reset_password_text.html', ec) body_html = render_to_string( 'account/email_reset_password_html.html', ec) email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [user.email]) email.send() return reset_password_link_sent(request) else: form = RequestNewPasswordForm() context = RequestContext(request) return render_to_response("account/forgot_password.html", {'form': form}, context_instance=context)
def notify_users(request, story, categories, new_article=True): bnotify = request.POST.get('notify_users', 0) users_email = [] for cat in categories: if cat.email_notification and (cat.email_notification == 1 or bnotify): acl_users = get_acl_by_object(cat, 'reader') if acl_users: users_email.extend([u.email for u in acl_users]) users_email = list(set(users_email)) if users_email: email = ZornaEmail() url = request.build_absolute_uri(reverse( 'view_story', args=[categories[0].pk, story.pk, story.slug])) ec = {"story": story, 'url': url, 'new_article': new_article, 'user': request.user} body_text = render_to_string( 'articles/email_notification_text.html', ec) body_html = render_to_string( 'articles/email_notification_html.html', ec) if new_article: subject = _(u'A new article has been published') else: subject = _(u'An article has been updated') step = getattr(settings, "ZORNA_MAIL_MAXPERPACKET", 25) for n in range(0, len(users_email) / step + 1): email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, bcc=users_email[ n * step:(n + 1) * step]) email.send()
def forgot_passord(request): if request.method == "POST": form = RequestNewPasswordForm(data=request.POST) if form.is_valid(): user = User.objects.get( Q(username=form.cleaned_data['username']) | Q(email=form.cleaned_data['username'])) up = user.get_profile() email = ZornaEmail() subject = _(u'Password reset request') h = hashlib.sha1() h.update(str(random.random())) salt = h.hexdigest()[:5] h.update(salt + user.username) hashcode = h.hexdigest() up.reset_password_key = hashcode up.save() ec = { 'user': user, 'site': Site.objects.get_current(), 'url': request.build_absolute_uri(reverse('request_password')) + '?user=%s&auth=%s' % (user.username, hashcode), } body_text = render_to_string( 'account/email_reset_password_text.html', ec) body_html = render_to_string( 'account/email_reset_password_html.html', ec) email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [user.email]) email.send() return reset_password_link_sent(request) else: form = RequestNewPasswordForm() context = RequestContext(request) return render_to_response("account/forgot_password.html", {'form': form}, context_instance=context)
def save(self, request, admin_origin, is_active, *args, **kwargs): """ Create the new ``User`` and his profile """ current_site = Site.objects.get_current() try: reg = SiteRegistration.objects.get(site=current_site) except SiteRegistration.DoesNotExist: reg = None if reg and reg.validation_type == REGISTRATION_NO_VALIDATION: is_active = True new_user = User.objects.create_user(self.cleaned_data['username'], self.cleaned_data['email'], self.cleaned_data['password1']) new_user.is_active = is_active new_user.first_name = self.cleaned_data['firstname'] new_user.last_name = self.cleaned_data['lastname'] new_user.save() up = new_user.get_profile() if admin_origin or (reg and reg.validation_type == REGISTRATION_NO_VALIDATION): up.activation_key = UserProfile.ACTIVATED up.save() if reg: groups = reg.groups.all() if groups: args = [obj for obj in groups] up.groups.clear() up.groups.add(*args) if admin_origin is False and reg: ec = { 'activation_key': up.activation_key, 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, 'site': current_site, 'user': new_user, 'mime_type': 'text', 'url': request.build_absolute_uri( reverse('account_activate', args=[up.activation_key])) } if reg.validation_type == REGISTRATION_EMAIL_VALIDATION: subject = _("Account activation on") + ' ' + current_site.name body_text = render_to_string('account/activation_email.html', ec) ec['mime_type'] = 'html' body_html = render_to_string('account/activation_email.html', ec) email = ZornaEmail() email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [new_user.email]) email.send() elif reg.validation_type == REGISTRATION_ADMIN_VALIDATION: from zorna.site.models import SiteOptions email_lists = SiteOptions.objects.get_authorized_user( 'zorna_validate_registration') if email_lists: subject = _("Your site has recorded a new registration") body_text = render_to_string( 'account/registration_email.html', ec) ec['mime_type'] = 'html' body_html = render_to_string( 'account/registration_email.html', ec) email = ZornaEmail() email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [u.email for u in email_lists]) email.send() return new_user
def save(self, request): message = self.cleaned_data['message'] send_to = self.cleaned_data['send_to'] upload_to = [] calendar_owners = [] dest = [] ao = get_allowed_objects(request.user, Community, ['manage', 'member']) if send_to: send_to = send_to.split(',') for item in send_to: item = item.split('-') if item[0] == 'u': # user user = User.objects.get(pk=item[1]) # if user recipient member of any current user communities ao_member_user = get_allowed_objects( user, Community, ['member', 'manage']) inter = [k for k in ao if k in ao_member_user] if len(inter): dest.append(user) calendar_owners.append(user) upload_to.append(u"U%s" % user.pk) else: community = Community.objects.get(pk=item[1]) if community.pk in ao: dest.append(community) calendar_owners.append(community) upload_to.append(u"C%s" % community.pk) users_emails = [] if len(dest): m = MessageCommunity(message=message) m.owner = m.modifier = request.user m.save() for k in dest: if isinstance(k, User): m.users.add(k) users_emails.append(k.email) else: m.communities.add(k) if k.email_notification: users = list(chain(get_acl_by_object( k, 'member'), get_acl_by_object(k, 'manage'))) users_emails.extend([u.email for u in users]) else: return None files = request.FILES.getlist("attachments") if len(upload_to) and len(files): try: path_library = get_upload_library() path = os.path.join(get_upload_communities(), "%s" % m.pk) if not os.path.isdir(path): os.makedirs(path) for f in request.FILES.getlist("attachments"): s = os.path.splitext(f.name) fname = slugify(s[0]) fext = s[1] destination = open(u"%s/%s" % ( path, u"%s%s" % (fname, fext)), 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() for d in upload_to: destpath = os.path.join(path_library, "%s" % d) if not os.path.isdir(destpath): os.makedirs(destpath) try: libfile = ZornaFile( owner=request.user, modifier=request.user) libfile.save() fsrc = u"%s/%s/%s,%s%s" % ( path_library, d, str(libfile.pk), fname, fext) shutil.copy2(u"%s/%s" % ( path, u"%s%s" % (fname, fext)), fsrc) except Exception as e: print(e) except Exception as e: pass # send email notification if len(users_emails): users_emails = list(set(users_emails)) if users_emails: email = ZornaEmail() url = request.build_absolute_uri(reverse( 'communities_home_page', args=[])) + '?all_msg=message&message_id=%s' % m.pk ec = {"message": m, 'url': url, 'user': request.user} body_text = render_to_string( 'communities/email_notification_text.html', ec) body_html = render_to_string( 'communities/email_notification_html.html', ec) subject = _( u'A new message has been posted in communities') step = getattr(settings, "ZORNA_MAIL_MAXPERPACKET", 25) for n in range(0, len(users_emails) / step + 1): email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, bcc=users_emails[ n * step:(n + 1) * step]) email.send() return m return None
def save(self, request, admin_origin, is_active, *args, **kwargs): """ Create the new ``User`` and his profile """ current_site = Site.objects.get_current() try: reg = SiteRegistration.objects.get(site=current_site) except SiteRegistration.DoesNotExist: reg = None if reg and reg.validation_type == REGISTRATION_NO_VALIDATION: is_active = True new_user = User.objects.create_user(self.cleaned_data[ 'username'], self.cleaned_data['email'], self.cleaned_data['password1']) new_user.is_active = is_active new_user.first_name = self.cleaned_data['firstname'] new_user.last_name = self.cleaned_data['lastname'] new_user.save() up = new_user.get_profile() if admin_origin or (reg and reg.validation_type == REGISTRATION_NO_VALIDATION): up.activation_key = UserProfile.ACTIVATED up.save() if reg: groups = reg.groups.all() if groups: args = [obj for obj in groups] up.groups.clear() up.groups.add(*args) if admin_origin is False and reg: ec = {'activation_key': up.activation_key, 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, 'site': current_site, 'user': new_user, 'mime_type': 'text', 'url': request.build_absolute_uri(reverse('account_activate', args=[up.activation_key])) } if reg.validation_type == REGISTRATION_EMAIL_VALIDATION: subject = _("Account activation on") + ' ' + current_site.name body_text = render_to_string( 'account/activation_email.html', ec) ec['mime_type'] = 'html' body_html = render_to_string( 'account/activation_email.html', ec) email = ZornaEmail() email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [ new_user.email]) email.send() elif reg.validation_type == REGISTRATION_ADMIN_VALIDATION: from zorna.site.models import SiteOptions email_lists = SiteOptions.objects.get_authorized_user( 'zorna_validate_registration') if email_lists: subject = _("Your site has recorded a new registration") body_text = render_to_string( 'account/registration_email.html', ec) ec['mime_type'] = 'html' body_html = render_to_string( 'account/registration_email.html', ec) email = ZornaEmail() email.append(subject, body_text, body_html, settings.DEFAULT_FROM_EMAIL, [ u.email for u in email_lists]) email.send() return new_user