Beispiel #1
0
    def form_valid(self, form):
        # Create a user, but remember to set inactive!
        user = User()
        user.username = form.cleaned_data['email']
        user.email = form.cleaned_data['email']
        user.is_active = False
        try:
            user.save()
        except IntegrityError:
            form.add_error('email',
                           gettext('Shop with this email already exists.'))
            return super(ShopRegisterView, self).form_invalid(form)

        self.object = form.save(commit=False)
        self.object.postcode = Postcode.objects.get(
            postcode=form.cleaned_data['postcode_special'])
        self.object.user = user
        self.object.save()

        current_site = get_current_site(self.request)
        context = {
            'shopname': self.object.name,
            'user': user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
        }

        html_message = render_to_string('emails/account_activation.html',
                                        context)
        txt_message = render_to_string('emails/account_activation.txt',
                                       context)

        email = EmailMultiAlternatives(gettext('FOODBEE - Confirm email'),
                                       txt_message)
        email.from_email = settings.DEFAULT_FROM_EMAIL
        email.to = [self.object.email]
        email.attach_alternative(html_message, "text/html")
        email.content_subtype = 'html'
        email.mixed_subtype = 'related'

        with open('base/static/base/img/fb_logo.png', mode='rb') as f:
            image = MIMEImage(f.read())
            image.add_header('Content-ID', "<Foodbee_logo_long.png>")
            email.attach(image)

        email.send()

        return super().form_valid(form)
Beispiel #2
0
def add_user(request):
    '''用户添加'''
    context = {}
    context['add_user'] = True
    if request.method == 'GET':
        #获得职位信息
        positions = Position.objects.all()
        position_list = []
        for position in positions:
            position_list.append({
                'id': position.id,
                'name': position.name,
            })
        #获得部门信息
        departments = Department.objects.all()
        department_list = []
        for department in departments:
            department_list.append({
                'id': department.id,
                'name': department.name,
            })
        if department_list:
            context['department_list'] = department_list
        if position_list:
            context['position_list'] = position_list

    else:
        errorList = []
        username = request.POST['username']
        username_zh = request.POST.get('username_zh', '')
        mobile = request.POST['mobile']
        password = request.POST['password']
        confirm_password = request.POST['confirm_password']
        department = request.POST.get('department', '')
        position = request.POST.get('position', '')
        chief = request.POST.get('chief', '')
        short_phone = request.POST.get('short_phone', '')
        if password != confirm_password:
            errorList.append({'error': u'密码不一致'})
        else:
            user = User()
            if username:
                user.username = username
            if username_zh:
                user.username_zh = username_zh
            if mobile:
                user.mobile = mobile
            if department:
                user.department = Department.objects.get(id=int(department))
            if position:
                user.position = Position.objects.get(id=int(position))
            if short_phone:
                user.short_phone = short_phone
            user.set_password(password)
            try:
                user.full_clean()
                user.save()

            except ValidationError as e:
                # Do something based on the errors contained in e.message_dict.
                # Display them to a user, or handle them programmatically.
                errKeys = e.message_dict.keys()
                for errKey in errKeys:
                    errorList.append({'error': e.message_dict[errKey][0]})
        if errorList:
            context['errorList'] = errorList

    return render(request, 'base/settings.html', context)