How do I explicitly specify language of email I want to send? - email

I have a custom user model that have a preferred_language field. I want all the emails (activation and password reset) to be sent translated to the language that user specified in profile.
class CustomUser(AbstractBaseUser, PermissionsMixin):
...
LANGUAGE_CHOICES = (
(1, "English"),
(2, "Русский")
)
preferred_language = models.PositiveSmallIntegerField(choices=LANGUAGE_CHOICES, default=2,
verbose_name=_("Preferred language"))
I thought about setting custom email class but didn't saw in navive djoser's classes any points where I could explicitly set the language of outcome emails despite of ready-to-be-translated style of email templates:
class ActivationEmail(BaseEmailMessage):
template_name = 'email/activation.html'
def get_context_data(self):
context = super(ActivationEmail, self).get_context_data()
user = context.get('user')
context['uid'] = utils.encode_uid(user.pk)
context['token'] = default_token_generator.make_token(user)
context['url'] = settings.ACTIVATION_URL.format(**context)
return context
Reset password view (which's refference is settings.EMAIL.activation used in ActivationEmail class above):
class ResetPassword(ActionViewMixin, generics.GenericAPIView):
...
def send_password_reset_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
settings.EMAIL.password_reset(self.request, context).send(to)
def send_activation_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
settings.EMAIL.activation(self.request, context).send(to)

In your case I would use the override context manager that stores the current language on enter (in order to sent the email) and restores it on exit.
from django.utils import translation
def send_password_reset_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
lang_code = user.lang_code # retrieve user's language code here
with translation.override(lang_code):
settings.EMAIL.password_reset(self.request, context).send(to)
def send_activation_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
lang_code = user.lang_code # retrieve user's language code here
with translation.override(lang_code):
settings.EMAIL.activation(self.request, context).send(to)

Related

How to give users a user role based on their email in Python?

I'm creating a registration page which contains 3 input fields: name, email, password. In my User model class, I've created a user_role method:
from models.base_model import BaseModel
from flask_login import UserMixin
import peewee as pw
class Users(BaseModel, UserMixin):
name = pw.CharField(max_length=100, unique=True)
email = pw.CharField(max_length=100, unique=True)
password = pw.CharField()
role = pw.CharField(max_length=50)
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
def __repr__(self):
return f"User: {self.name}, {self.email}, {self.role}"
def user_roles(self):
user_email = self.email
self.roles = ["user","admin"]
check_email = ["test#testing.com"]
if user_email == check_email:
return self.roles == "admin"
else:
return self.roles == "user"
The check_email is use to check if the email that a user fill in the field is equal to the check_email value. If it does, the role should be assign as 'admin'. In my route file, this is what it look like when saving to the database:
if form.validate_on_submit():
name = form.name.data
email = form.email.data
raw_password = form.password.data
password = generate_password_hash(raw_password, method="sha256", salt_length=20)
role = Users.user_roles(email)
new_user = Users(name=name, email=email, password=password, role=role)
Now when I tried to test out the registration form, this is the error it gave me:
I want to know is there a way to fix this error?
PS: I'm doing this for testing purposes to see if I could assign user role. I would probably add more user roles in the future, so I don't think Flask-Admin could help me
You have your user_roles method defined as a class method when it can and should probably be a static method. Also you are calling user_roles with the email string but the try to access self.email in the method. Just change that and you are good to go.
def get_role(email):
check_email = "test#testing.com"
if email == check_email:
return "admin"
else:
return "user"
Edit: Forgot to remove brackets as we don't want an array but just the string.

How to set defaut value for a many2one field in Odoo 9.0c?

I have customized a module that include a many2one field. Now, I would like to set the default value for that field with the codition is: [[u'bom_ids', u'!=', False]]. i have tried below code but it did not work probebly
width_id = field.Many2one('sale.order.line.width', default ='_get_width_default')
def _get_width_default(self, cr, uid, context=None):
res = self.pool.get('product.template').search(cr, uid, [(u'bom_ids', u'!=', False)], context=context)
return res and res[0] or False
default = {
'width_id' : _get_width_default,
}
Could you guy please help me to point what is the problem and how to sovle the problem and finnally get my purpose. Thank for watching
import logging
_logger = logging.getLogger(__name__)
def _get_width_default(self, cr, uid, context=None):
#REPLACE DOMAIN WITH SOMETHING RELEVANT
domain = []
res = self.pool.get('sale.order.line.width').search(cr, uid, domain, context=context)
_logger.info("PREPARING DEFAULT VALUE")
_logger.info(res)
return res[0] or False
width_id = fields.many2one('sale.order.line.width')
_defaults = {
'width_id' : _get_width_default,
}
What do you see as far as logging is concerned? Try the above code (v7 style).
Odoo9 Style (NEW API)
import logging
_logger = logging.getLogger(__name__)
def _get_width_default(self):
#REPLACE DOMAIN WITH SOMETHING RELEVANT
domain = []
res = self.env['sale.order.line.width'].search(domain)
_logger.info("PREPARING DEFAULT VALUE")
_logger.info(res)
return res[0].id or False
width_id = fields.many2one('sale.order.line.width',default=_get_width_default)

'QuerySet' object has no attribute 'subs'

Can someone pls explain how can I filter users posts if user is subscriber or not?
'QuerySet' object has no attribute 'subs'
I use custom user
class Every(AbstractBaseUser):
user = models.OneToOneField(settings.AUTH_USER_MODEL, unique=True)
name = models.CharField(max_length=30, blank=True)
Here is post model:
class Post(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
text = models.TextField(max_length=1200)
A subscriber model:
class Sub(models.Model):
user = models.OneToOneField(User, related_name='user')
subs = models.ManyToManyField(User, blank=True, related_name='subs')
A view that I'm trying to use:
def tape(request, every_id):
context = {}
context.update(csrf(request))
post_form = PostForm
pform = post_form
sub = Sub.objects.filter(subs=every_id)# here I get users that intersting for my user
tape = Post.objects.filter(user=sub.subs).order_by("-timestamp")
username = request.user
context = {"username": username, "pform": pform, "tape": tape, "sub": sub,}
return render(request, 'tape.html', context)
actually it would be better to use a class view. But here is an answer with an usual view. We need to get subscription's and subscribers's ids before we start filter posts
here is even a bit more :-)
def get_user_id_list(user):
"""Returns a list of subscribers's ids"""
try:
sub = user.user
Sub.DoesNotExist:
return []
return sub.subs.all().values_list('user_id', flat=True)
def get_user_id_list_2(user):
"""Returns a list of subscription's ids"""
return user.subs.values_list('user_id', flat=True)
def tape(request):
pform = PostForm
user_id_list = get_user_id_list_2(request.user)
logger.info('user_id_list = {}'.format(user_id_list))
tape = Post.objects.filter(user_id__in=user_id_list).order_by("-timestamp")
username = request.user
context = {
"username": username,
"pform": pform,
"tape": tape,
"sub": sub,
}
return render(request, 'tape.html', context)

How to get e-mail address of current Jenkins user to use in groovy script

I've created a groovy script for the new Jenkins Workflow Plugin, https://github.com/jenkinsci/workflow-plugin. I want it to send a mail to the user who started the job when it needs input for the next step. I've tried to search the API but I can't find anything about getting the users email address.
I would think of something like this.
import hudson.model.User
def user = User.current()
def mailAddress = user.getMailAddress()
Is there a way to get the current Jenkins user' address in groovy?
I found a way:
import hudson.model.AbstractProject
import hudson.tasks.Mailer
import hudson.model.User
def item = hudson.model.Hudson.instance.getItem(env.JOB_NAME)
def build = item.getLastBuild()
def cause = build.getCause(hudson.model.Cause.UserIdCause.class)
def id = cause.getUserId()
User u = User.get(id)
def umail = u.getProperty(Mailer.UserProperty.class)
print umail.getAddress()
You can access the object of the current user with the method current()
def user = hudson.model.User.current();
The email address can be retrieved in the same way as to what you have done in your answer.
print user.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress();
import hudson.tasks.Mailer;
import hudson.model.User;
import hudson.model.Cause;
import hudson.model.Cause.UserIdCause;
def cause = build.getCause(hudson.model.Cause$UserIdCause)
def id = cause.getUserId()
User u = User.get(id)
def umail = u.getProperty(Mailer.UserProperty.class)
print umail.getAddress()
If you have access to the build variable in the Java code of your plugin (for instance in the setUp() method of the class that extends BuildWrapper), you can get the currently logged user this way :
#Override
public MyJenkinsPlugin setUp(AbstractBuild build, Launcher launcher, BuildListener listener)
String connectedUser = build.getCause(Cause.UserIdCause.class).getUserId();
String mail = User.get(connectedUser.getProperty(hudson.tasks.Mailer.UserProperty.class).getEmailAddress()
...
}
I have not been able to get the logged user using User.current().getId(), it always returned me 'SYSTEM'.
Hope it helps!
You can get the author name and then use it for an example on a mailing registry or something like that:
def author = ""
def changeSet = currentBuild.rawBuild.changeSets
for (int i = 0; i < changeSet.size(); i++)
{
def entries = changeSet[i].items;
for (int i = 0; i < changeSet.size(); i++)
{
def entries = changeSet[i].items;
def entry = entries[0]
author += "${entry.author}"
}
}
print author;
You can use the following routine:
import hudson.tasks.Mailer;
import hudson.model.User;
/**#
* Get user's email
*
* #param id null for a user who triggered the current build or name otherwise
* #return user's email
*/
def getUserEmail(String id=null) {
User user = User.getById(id ?: currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId(), false)
user?.getProperty(Mailer.UserProperty.class).getAddress()
}

Not able to fetch mongodb object with a valid field

I'm using Mongoengine.
When I do a:
>>> Grant.objects().first().client_id
The result is as expected.
ObjectId('526fd0da82353536892f22ae')
But, when I search based on the client_id,
>>> Grant.objects(client_id="526fd0da82353536892f22ae").first()
I get a InvalidQueryError:
InvalidQueryError: Cannot resolve field "client_id"
Here's what my Grant model looks like:
class Grant(db.Document):
#user_id = db.StringField()
user = db.ReferenceField(User)
client_id = db.StringField()
client = db.ReferenceField(Client)
code = db.StringField()
redirect_uri = db.StringField()
expires = db.DateTimeField()
scopes = db.ListField()
#for soft-deleting the grant
is_deleted = db.BooleanField(default=False)
#property
def user_id(self):
return self.user.id
#property
def client_id(self):
return self.client.id
def delete(self):
self.is_deleted = True
Can someone go through this code and point out the problem?
Do you try to use it like :
Grant.objects(client_id=ObjectId("526fd0da82353536892f22ae")).first()