Esempio n. 1
0
 def save(self, *args, **kwargs):
     self.uid = self.uid or util.get_uuid(8)
     self.html = self.html or mistune.markdown(self.text)
     self.max_upload_size = self.max_upload_size or self.set_upload_size()
     """ self.name = self.name or self.user.first_name or self.user.email.split("@")[0] """
     self.date_joined = self.date_joined or now()
     self.last_login = self.last_login or now()  # - timedelta(days=1)
     super(Profile, self).save(*args, **kwargs)
Esempio n. 2
0
def create_profile(sender, instance, created, raw, using, **kwargs):
    if created:
        # Set the username to a simpler form.
        username = f"{instance.first_name}-{instance.pk}" if instance.first_name else f'user-{instance.pk}'
        # check if username exists and if it does, generate auto
        if User.objects.filter(username=username).exclude(
                id=instance.pk).exists():
            username = util.get_uuid(6)

        User.objects.filter(pk=instance.pk).update(username=username)

        # Make sure staff users are also moderators.
        role = Profile.MANAGER if instance.is_staff else Profile.READER
        full_name = f'{instance.first_name} {instance.last_name}'
        Profile.objects.using(using).create(user=instance,
                                            uid=username,
                                            name=full_name,
                                            role=role)
        profile = instance.profile
        profile.avatar = generate_avatar(profile)
        tasks.create_messages.spool(rec_list=[instance],
                                    template="messages/welcome.md")
Esempio n. 3
0
    def handle(self, *args, **options):

        # Get the filename.
        fname = options['fname']

        if not os.path.isfile(fname):
            logger.error(f'Not a valid filename: {fname}')
            return
        stream = open(fname, 'rU')
        for row in csv.DictReader(stream):
            name = row.get("Name", '')
            email = row.get("Email", '')
            handler = row.get("Handler", '')

            # All fields must be filled in.
            if not (name and email):
                logger.error(f"Invalid row: {row}")
                continue

            # Fix capitalization.
            name = name.strip()
            email = email.strip().lower()
            handler = handler.strip()

            if User.objects.filter(email=email).exists():
                logger.info(
                    f"Skipped creation. User with email={email} already exists."
                )
            else:
                username = handler or util.get_uuid(16)
                user = User.objects.create_user(email=email,
                                                username=username,
                                                first_name=name)
                user.set_password(settings.DEFAULT_PASSWORD)
                user.save()
                logger.info(f"Created user name={name} email={email}")
Esempio n. 4
0
def create_uuid(sender, instance, *args, **kwargs):
    instance.username = instance.username or util.get_uuid(8)
Esempio n. 5
0
 def save(self, *args, **kwargs):
     self.uid = self.uid or util.get_uuid(10)
     self.sent_date = self.sent_date or util.now()
     super(Message, self).save(**kwargs)