Example #1
0
def create_super_user(sender, instance, create, raw, using, update_fields, **kwargs):
    """
    Creat a Profile for User when a newly user is created,
    """
    if not create:
        return

    if instance.is_staff:
        user_profile = UserProfile.objects.create(user=instance)
        user_profile.save()

        profile = Profile(_id=instance.pk)
        profile.avatar = ''
        profile.full_name = ''
        profile.other_name = ''
        profile.save()

        activity = Activities(_id=instance.pk)
        activity.save()
        avatar_dir = os.path.join(settings.MEDIA_ROOT, str(instance.pk), settings.AVATAR_DIR)
        cover_dir = os.path.join(settings.MEDIA_ROOT, str(instance.pk), settings.COVER_DIR)

        try:
            os.makedirs(avatar_dir, mode=0o700)
            os.makedirs(cover_dir, mode=0o700)
        except OSError as err:
            user_profile.delete()
            profile.delete()
            activity.delete()
            print('Created user\'s error: {}'.format(err.strerror))
 def form_valid(self, form):
     try:
         form.instance.is_staff = False
         user = form.save()
         profile = Profile(_id=user.pk)
         profile.other_name = ''
         # profile.avatar = ''
         profile.full_name = ''
         try:
             profile.save()
         except:
             user.delete()
     except:
         pass
     return super().form_valid(form)