Ejemplo n.º 1
0
	def __check_rate_limit__(self, env):
		self.log.debug("Checking rate limit.")

		if not config.LIMIT_REQUESTS_BY_IP and not config.LIMIT_REQUESTS_BY_USER:
			return

		address = env["REMOTE_ADDR"]

		with factory.create_db_connection() as conn:
			with conn.enter_scope() as scope:
				request_db = factory.create_request_db()
				user_db = factory.create_user_db()

				if config.LIMIT_REQUESTS_BY_IP:
					count = request_db.count_requests_by_ip(scope, address, 3600)

					self.log.debug("'%s' has made %d of %d allowed requests.", address, count, config.IP_REQUESTS_PER_HOUR)

					if count > config.IP_REQUESTS_PER_HOUR:
						raise exception.HTTPException(402, "IP request limit reached.")

				user_id = user_db.map_username(scope, self.username)

				if config.LIMIT_REQUESTS_BY_USER:
					count = request_db.count_requests_by_user_id(scope, user_id, 3600)

					self.log.debug("'%s' (%d) has made %d of %d allowed requests.", self.username, user_id, count, config.USER_REQUESTS_PER_HOUR)

					if count > config.USER_REQUESTS_PER_HOUR:
						raise exception.HTTPException(402, "User request limit reached.")

				request_db.add_request(scope, address, user_id)

				scope.complete()
Ejemplo n.º 2
0
	def __get__(self, env, id, code):
		with factory.create_db_connection() as connection:
			db = factory.create_user_db()

			with connection.enter_scope() as scope:
				if not db.password_request_id_exists(scope, id):
					raise exception.NotFoundException("Request id not found.")

		v = view.HTMLTemplateView(200, template.ChangePasswordPage, config.DEFAULT_LANGUAGE)
		v.bind({"id": id, "code": code, "error_field": None})

		return v
Ejemplo n.º 3
0
    def __get__(self, env, id, code):
        with factory.create_db_connection() as connection:
            db = factory.create_user_db()

            with connection.enter_scope() as scope:
                if not db.password_request_id_exists(scope, id):
                    raise exception.NotFoundException("Request id not found.")

        v = view.HTMLTemplateView(200, template.ChangePasswordPage,
                                  config.DEFAULT_LANGUAGE)
        v.bind({"id": id, "code": code, "error_field": None})

        return v
Ejemplo n.º 4
0
    def __check_rate_limit__(self, env):
        self.log.debug("Checking rate limit.")

        if not config.LIMIT_REQUESTS_BY_IP and not config.LIMIT_REQUESTS_BY_USER:
            return

        address = env["REMOTE_ADDR"]

        with factory.create_db_connection() as conn:
            with conn.enter_scope() as scope:
                request_db = factory.create_request_db()
                user_db = factory.create_user_db()

                if config.LIMIT_REQUESTS_BY_IP:
                    count = request_db.count_requests_by_ip(
                        scope, address, 3600)

                    self.log.debug("'%s' has made %d of %d allowed requests.",
                                   address, count, config.IP_REQUESTS_PER_HOUR)

                    if count > config.IP_REQUESTS_PER_HOUR:
                        raise exception.HTTPException(
                            402, "IP request limit reached.")

                user_id = user_db.map_username(scope, self.username)

                if config.LIMIT_REQUESTS_BY_USER:
                    count = request_db.count_requests_by_user_id(
                        scope, user_id, 3600)

                    self.log.debug(
                        "'%s' (%d) has made %d of %d allowed requests.",
                        self.username, user_id, count,
                        config.USER_REQUESTS_PER_HOUR)

                    if count > config.USER_REQUESTS_PER_HOUR:
                        raise exception.HTTPException(
                            402, "User request limit reached.")

                request_db.add_request(scope, address, user_id)

                scope.complete()
Ejemplo n.º 5
0
    def __consumer__(self):
        db = factory.create_mail_db()

        while True:
            self.__logger.debug("mailer waits for events.")

            self.__consumer_cond.acquire()
            self.__consumer_cond.wait(config.MAIL_CHECK_INTERVAL)
            self.__consumer_cond.release()

            if not self.is_running():
                break

            # get & send mails:
            self.__logger.debug("Searching for mails to send.")

            with factory.create_db_connection() as conn:
                with conn.enter_scope() as scope:
                    mails = db.get_unsent_messages(scope, 100)

                if len(mails) > 0:
                    self.__logger.info("Found %d mail(s) to send.", len(mails))

                    try:
                        self.__mta.start_session()

                        for m in mails:
                            with conn.enter_scope() as scope:
                                if self.__mta.send(m["subject"], m["body"],
                                                   m["email"]):
                                    db.mark_sent(scope, m["id"])
                                    scope.complete()

                            if not self.is_running():
                                break

                        self.__mta.end_session()

                    except Exception as e:
                        self.__logger.error(e)
                        self.__logger.error(traceback.print_exc())
Ejemplo n.º 6
0
	def __consumer__(self):
		db = factory.create_mail_db()

		while True:
			self.__logger.debug("mailer waits for events.")

			self.__consumer_cond.acquire()
			self.__consumer_cond.wait(config.MAIL_CHECK_INTERVAL)
			self.__consumer_cond.release()

			if not self.is_running():
				break

			# get & send mails:
			self.__logger.debug("Searching for mails to send.")

			with factory.create_db_connection() as conn:
				with conn.enter_scope() as scope:
					mails = db.get_unsent_messages(scope, 100)

				if len(mails) > 0:
					self.__logger.info("Found %d mail(s) to send.", len(mails))

					try:
						self.__mta.start_session()

						for m in mails:
							with conn.enter_scope() as scope:
								if self.__mta.send(m["subject"], m["body"], m["email"]):
									db.mark_sent(scope, m["id"])
									scope.complete()

							if not self.is_running():
								break

						self.__mta.end_session()

					except Exception as e:
						self.__logger.error(e)
						self.__logger.error(traceback.print_exc())
Ejemplo n.º 7
0
	def __check_rate_limit__(self, env):
		self.log.debug("Checking rate limit.")

		if not config.LIMIT_REQUESTS_BY_IP:
			return

		address = env["REMOTE_ADDR"]

		with factory.create_db_connection() as conn:
			with conn.enter_scope() as scope:
				db = factory.create_request_db()

				count = db.count_requests_by_ip(scope, address, 3600)

				self.log.debug("'%s' has made %d of %d allowed requests.", address, count, config.IP_REQUESTS_PER_HOUR)

				if count > config.IP_REQUESTS_PER_HOUR:
					raise exception.HTTPException(402, "IP request limit reached.")

				db.add_request(scope, address)

				scope.complete()
Ejemplo n.º 8
0
    def __check_rate_limit__(self, env):
        self.log.debug("Checking rate limit.")

        if not config.LIMIT_REQUESTS_BY_IP:
            return

        address = env["REMOTE_ADDR"]

        with factory.create_db_connection() as conn:
            with conn.enter_scope() as scope:
                db = factory.create_request_db()

                count = db.count_requests_by_ip(scope, address, 3600)

                self.log.debug("'%s' has made %d of %d allowed requests.",
                               address, count, config.IP_REQUESTS_PER_HOUR)

                if count > config.IP_REQUESTS_PER_HOUR:
                    raise exception.HTTPException(402,
                                                  "IP request limit reached.")

                db.add_request(scope, address)

                scope.complete()
Ejemplo n.º 9
0
def import_images():
    log = logger.get_logger()

    log.info("Searching for image files in '%s'", config.IMAGE_LIBRARY_PATH)

    with factory.create_db_connection() as conn:
        db = factory.create_object_db()

        for filename in os.listdir(config.IMAGE_LIBRARY_PATH):
            log.info("Found file: '%s'", filename)

            path = os.path.join(config.IMAGE_LIBRARY_PATH, filename)

            mime = mimetypes.guess_type(path)[0]
            index = filename.rfind(".")
            name = filename[:index]

            b64_origin = os.path.join(config.IMAGE_LIBRARY_BASE64_PATH,
                                      "%s.base64" % name)
            b64_thumbnail = os.path.join(config.IMAGE_LIBRARY_BASE64_PATH,
                                         "%s.thumbnail.base64" % name)

            try:
                if os.path.isfile(path) and (
                        not os.path.exists(b64_origin)
                        or not os.path.exists(b64_thumbnail)):
                    log.info("Importing file...")

                    with conn.enter_scope() as scope:
                        db.create_object(scope, util.new_guid(), filename)

                        log.info('Creating file: "%s"' % b64_origin)

                        with open(path, "rb") as f:
                            b64 = "data:%s;base64,%s" % (
                                mime, f.read().encode("base64"))

                        with open(b64_origin, "w") as f:
                            f.write(b64)

                        log.info('Creating file: "%s"' % b64_thumbnail)

                        image = Image.open(path)
                        image.thumbnail(config.IMAGE_LIBRARY_THUMBNAIL_SIZE,
                                        Image.ANTIALIAS)

                        buffer = cStringIO.StringIO()
                        image.save(buffer, "PNG")

                        b64 = "data:png;base64,%s" % buffer.getvalue().encode(
                            "base64")

                        with open(b64_thumbnail, "w") as f:
                            f.write(b64)

                        scope.complete()
                else:
                    log.debug("Ignoring file.")

            except:
                dst = os.path.join(config.IMAGE_LIBRARY_FAILURE_PATH, filename)

                log.error('Import failed, moving file "%s" to "%s"' %
                          (path, dst))

                os.rename(path, dst)

                for f in [b64_origin, b64_thumbnail]:
                    try:
                        log.info('Deleting file: "%s"' % f)
                        os.remove(f)

                    except:
                        pass
Ejemplo n.º 10
0
	def __create_db_connection__(self):
		return factory.create_db_connection()