예제 #1
0
 def insert(self, collection, data, by_user=None):
     if self.PYRENEES_GUID_KEY not in data:
         data.update({self.PYRENEES_GUID_KEY: guid()})
     self.touch(data, by_user)
     json_data = JSONEncoder().encode(data)
     self.db[collection].insert(json.loads(json_data))
     return data
예제 #2
0
    def _save(cls,
              file_data,
              to_path,
              to_name=None,
              org_name=None,
              use_date_directory=False):
        file_extension = org_name.split('.')[-1] if org_name else ''
        file_extension = '.' + file_extension if file_extension[
            0] != '.' else file_extension
        to_path = cls.get_abspath_path(to_path)
        if not os.path.isdir(to_path):
            logger.debug('Invalid path %s for save file' % to_path)
            return None
        destination = (cls._make_dirs_by_date(to_path) if use_date_directory else to_path) + '/' + \
                      (to_name if to_name else guid()) + file_extension
        destination = destination.replace('//', '/')
        if isinstance(file_data, InMemoryUploadedFile) or isinstance(
                file_data, TemporaryUploadedFile):
            with open(destination, 'wb+') as file_obj:
                for chunk in file_data.chunks():
                    file_obj.write(chunk)
                file_obj.close()

            logger.info('File %s has been saved!' % destination)
            return cls.get_relative_path(destination)
        else:
            return None
예제 #3
0
def save_file(file,
              name=None,
              extension=None,
              save_to_path=settings.MEDIA_ROOT,
              save_to_name=None,
              use_date_directory=False):
    file_name = save_to_name if save_to_name else guid()
    file_extension = extension if extension else (
        name.split('.')[-1] if name else '')
    file_extension = '.' + file_extension if file_extension[
        0] != '.' else file_extension

    destination = (make_dirs_by_date(save_to_path) if use_date_directory else save_to_path) \
                                                                             + '/' + file_name + file_extension
    destination = destination.replace('//', '/')
    if isinstance(file, InMemoryUploadedFile) or isinstance(
            file, TemporaryUploadedFile):
        with open(destination, 'wb+') as file_obj:
            for chunk in file.chunks():
                file_obj.write(chunk)

        file_obj.close()
    else:
        return None
    logger.info('File %s has been saved!' % destination)
    return get_relative_path(destination)
예제 #4
0
def send_active_mail(user):
    if not user.email:
        return

    message = Message().parse_template(template=REGISTER_VERIFY_CODE,
                                       keywords={
                                           'active_code': guid(),
                                           "email": user.email
                                       })
    message.receiver = user
    messager = Messager().set_backend(DjangoEmailBackend)
    messager.message = message
    messager.send()
    logger.info('Active email has sent to %s' % user.username)
예제 #5
0
    def register(self, request):
        try:
            email = request.data["email"]
            password = request.data["password"]
            password_check = request.data["confirm"]
        except KeyError as err:
            logger.error(err)
            return AlpsRestResponse.fail(status=status.HTTP_400_BAD_REQUEST)

        if email is None or email == "":
            return AlpsRestResponse.success(**ERROR_MISSING_EMAIL)

        if password is None or password != password_check:
            return AlpsRestResponse.success(**ERROR_PASSWORD_MISMATCH)

        try:
            get_user_model().objects.get(email=email)
        except get_user_model().DoesNotExist as err:
            pass
        else:
            return AlpsRestResponse.success(**ERROR_EMAIL_EXIST)

        username = email.split('@')[0]
        try:
            get_user_model().objects.get(username=username)
        except get_user_model().DoesNotExist as err:
            pass
        else:
            username = guid()  # user name already exist, instead of GUID

        user = get_user_model()(username=username, email=email)
        user.set_password(password)
        user.is_active = False  # default is True
        user.save()
        log_action(user, user, Actions.REGISTERING, "Registered successfully")
        logger.info("Registered successfully with %s" % username)
        serializer = UserSerializer(user, context={'request': request})

        # send active email
        send_active_mail(user)
        return AlpsRestResponse.success(serializer.data)
예제 #6
0
    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None,
             force_update_deleted=False):

        if self.deleted and not force_update_deleted:
            raise UpdateDeletedError(_("Deleted record can not be update!"))

        self._soft_touch()
        self.modified_time = now()
        # set created_time and created_by in creating
        if self.pk is None:
            self.created_time = now()
            if not self.pyr_guid:
                self.pyr_guid = guid()
            if self.created_by is None:
                self.created_by = get_unknown_user()

        return super().save(force_insert, force_update, using, update_fields)