예제 #1
0
 def process(self):
     email = self.managerInstance.unprocessed_db_obj.user.email  # to create instance for target user
     for filename in self.data:
         if isinstance(
                 self.data[filename], dict
         ):  # if data type is dict that it contains, error so updating it
             self.managerInstance.details[filename] = self.data[
                 filename]  # in details
             continue
         for data in self.data[filename]:
             try:
                 self.managerInstance.details[filename] = {
                     "processing_class": "",
                     "status_ok": True
                 }
                 class_ = self.get_data_processing_class(
                     data, filename, self.managerInstance.original_filename)
                 self.managerInstance.details[filename][
                     "processing_class"] = class_.__name__
                 processing_class = class_(email=email, **data)
                 self.managerInstance.details[filename][
                     "processing_class_messages"] = processing_class.details
             except Exception as ex:
                 logger.critical(f"Exception while processing: {str(ex)}")
                 self.managerInstance.details[filename]["error"] = str(ex)
                 self.managerInstance.details[filename]["status_ok"] = False
예제 #2
0
 def set_seen(self):
     self.is_seen = True
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #3
0
def user_permissions():
    # get request data
    try:
        data = request.get_json()

        # get company id
        company_id = data.get('id')
        if company_id is None:
            current_app.logger.warning(
                f'Payload does not contain company id. Payload: {data}')
            raise Exception('Payload does not contain company id')

        # update company
        user = auth.current_user()
        company_details = user.set_company(company_id=company_id)

        return jsonify({
            'permissions': permissions(user),
            'company': company_details,
        }), 200

    except Exception as e:
        print(e)
        from polzyFunctions.GlobalConstants import logger
        logger.critical(f"Error: {e}", stack_info=True)
        return {'error': f'Failed to set company: {e}'}, 400
예제 #4
0
 def set_language(self, language):
     self.language = language
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #5
0
 def set_seen(self):
     self.seen_at = datetime.now()
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #6
0
    def set_processed(cls, file, processed_obj=None, details=None, key=None):
        if not details:
            details = {}

        filename, extension = os.path.splitext(file)

        if not processed_obj:  # getting File row object if not supplied
            processed_obj = db.session.query(models.File).filter_by(id=filename).first()

        if not key:  # zip file has different name on storage and details dict. Key is name for details dict
            key = os.path.basename(file)

        if extension.lower() == ".zip":
            if not os.path.exists(filename):
                filename = os.path.join(GlobalConstants.filesPath, filename)
                if not os.path.exists(filename):
                    processed_obj.set_processed(details=details.get(key, {}))
                    return
            files = os.listdir(filename)
            for extracted_file in files:  # these files are already processed, now creating entry in db & updating
                instance = cls.create_new(file=os.path.join(filename, extracted_file
                                                            ).replace(GlobalConstants.filesPath, ""),
                                          extension=os.path.splitext(extracted_file)[1],
                                          parent_id=filename)  # parent_id means this entry is child of a zip file
                cls.set_processed(
                    instance.id + os.path.splitext(extracted_file)[1], instance, details=details, key=extracted_file
                )  # marking the zip extracted file processed with necessary details. Details key is original filename
            try:
                os.rmdir(filename)  # after processing and moving all files delete zip directory
            except Exception as ex:  # if zip directory is not empty it will create error
                logger.critical(f"Exception while removing directory {filename}: {str(ex)}")
            details[key] = details.copy()

        processed_obj.set_processed(details=details.get(key, {}))
        cls.move_processed_file(os.path.basename(file))  # moving files to processed files directory
예제 #7
0
    def set_company(self, company_id=None, company=None):
        # check if company data is provided
        if company_id is None and company is None:
            raise Exception('Neither Company ID nor Company provided')

        # check if company should be fetched
        if company is None:
            company = db.session.query(Company).filter_by(id=company_id).first()

        # check if company exists
        if company is None:
            raise Exception('Company not found')

        # get UserToCompany instance
        user_company = db.session.query(UserToCompany).filter(and_(
            UserToCompany.user_id == self.id,
            UserToCompany.company_id == company.id,
        )).first()

        # check if association exists
        if user_company is None:
            raise Exception(f'User is not assigned to company ID {company_id}')

        # set company
        self.company_id = company.id
        try:
            db.session.commit()
        except Exception as ex:
            logger.critical(f"Exception while committing changes to db: {ex}")
            db.session.rollback()

        # return company json
        return user_company.to_json()
예제 #8
0
    def _get_default_data(self):
        stage = 'pqa'
        output_dir = ''
        offline = False
        try:
            config = configparser.ConfigParser()
            # pathlib.Path resolves issue of lowercase path stored in __file__ which was creating issue in configparser
            config.read(self.setting_file)
            default = config["Default"]
        except Exception as ex:
            logger.critical(f"Exception while reading config file: {ex}")
            return offline, output_dir, stage

        stage = default.get("stage", stage)

        unchecked_path = default.get("PDFOutput")
        try:
            if unchecked_path:
                Path(unchecked_path).mkdir(parents=True, exist_ok=True)
                output_dir = unchecked_path
        except Exception as ex:
            output_dir = ''
            logger.info(f"Error during Path creation: {ex}")

        if default.get("offline", "").lower().strip() == "true":
            offline = True
        return offline, output_dir, stage
예제 #9
0
def activityWriter(inInstance):
    """
    The usage of "Antrag" here is wrong, usually it's an AntragActivitiy that will be provided in the call.
    It is anyway important to have this reference because otherwise the "where used" doesn't work for "setSearchString"

    """
    from polzyFunctions.Dataclasses.Antrag import Antrag

    lInstance = None
    try:
        lInstance = inInstance.antrag
    except AttributeError:
        pass

    if not lInstance:
        # this will work if the instance itself is an Antrag (happens so far only when the Tag is changed by
        # frontend
        lInstance: Antrag = inInstance

    logger.debug(f"classname = {lInstance.__class__.__name__}")
    # Update search string in this antrag instance. It is the main search criteria.
    try:
        lInstance.setSearchString()
    except AttributeError:
        logger.critical(
            f"We have instance of Type {type(inInstance)}. We can't currently work with that."
        )
        return

    record = AntragActivityRecords.new(lInstance)
    logger.debug(f"Activity recorded with id: {record.id}")
    return record.timestamp
예제 #10
0
    def getConfigurationParameter(self, configurationEnvironmentKey: str, configKeyToSearchFor: str):
        """
        Main method to retrieve a configuration setting. It will first try to read from the given
        configurationEnvironment. If that is not successful it will fallback to "default.json".

        If unsuccessful, we'll create a log message.

        :param configurationEnvironmentKey: Environment key (e.g. sapClient)
        :param configKeyToSearchFor: The key in either default Configuration or manually set configuration
        :return: the value of the configuration entry.
        """
        lEnvironmentExists = self.__checkAndReadConfigurations(configurationEnvironmentKey)
        if not lEnvironmentExists:
            lReturn = self.configsRead["default"].get(configKeyToSearchFor)
        else:
            lReturn = self.configsRead.get(configurationEnvironmentKey, {}).get(configKeyToSearchFor)
            if not lReturn:
                lReturn = self.configsRead["default"].get(configKeyToSearchFor)

        if not lReturn:
            logger.critical(f"Configuration key {configKeyToSearchFor} not found in "
                            f"current configuration {configurationEnvironmentKey} nor in default configuration.")
            return False

        return lReturn
예제 #11
0
    def new(cls, antrag):
        json_data = {}
        for activities in antrag.Activities:
            json_data[activities.__class__.__name__] = activities.toJsonForPersistence()
            if activities.encrypt:  # if activity needs to be encrypt then encrypt it and replace
                encrypted_dic = dict()
                for key, value in json_data[activities.__class__.__name__].items():
                    encrypted_dic[cls.encrypt(key)] = cls.encrypt(value)
                encrypted_dic["encrypt"] = True  # used for identifying that this activity needs to be decrypt
                json_data[activities.__class__.__name__] = encrypted_dic

        instance = cls(
            antrag_id=str(antrag.id),
            user_id=str(antrag.user.id),
            company_id=str(antrag.user.company_id),
            antragsnummer=antrag.antragsnummer,
            status=antrag.status,
            searchString=antrag.searchstring,
            json_data=json.dumps(antrag.Fields.toJSON()),
            json_data_activities=json.dumps(json_data),
            class_name=antrag.__class__.__name__,
            sapClient=antrag.sapClient,
            tag=antrag.tag,
        )
        db.session.add(instance)
        try:
            db.session.commit()
        except Exception as ex:
            logger.critical(f"Exception while committing changes to db: {ex}")
            db.session.rollback()
        return instance
예제 #12
0
 def __init__(self, fileNameAndPath, original_filename, unprocessed_db_obj,
              classes: list):
     self.unprocessed_db_obj = unprocessed_db_obj  # used to set processed tag on failed files
     self.details = {
         os.path.basename(fileNameAndPath): {}
     }  # used to save necessary details in db
     self.processor = None
     self.classes = classes
     try:
         self.fileName, self.extension = self.sanitize_filename(
             fileNameAndPath)
         self.original_filename = original_filename
         self.fileHandler = Handler(self.fileName, self.original_filename,
                                    self.extension)
         self.data = self.fileHandler.data
         self.process_data()
     except Exception as ex:
         logger.critical(
             f"Exception while processing {os.path.basename(fileNameAndPath)}: {str(ex)}"
         )
         self.details[os.path.basename(
             fileNameAndPath)]["status_ok"] = False
         self.details[os.path.basename(fileNameAndPath)]["error"] = str(ex)
         DatabaseManager.set_processed(os.path.basename(fileNameAndPath),
                                       self.unprocessed_db_obj,
                                       self.details)
예제 #13
0
파일: Antrag.py 프로젝트: Athos1972/PoLZy
    def setFieldValue(self, fieldName, newValue, optional=False):
        """
        Write a new value into a field of this instance.

        :param fieldName:
        :param newValue:
        :param optional: don't raise an error/write to log, if field does not exist
        :return:
        """
        try:
            lFeld = self.Fields.getField(name=fieldName)
            lFeld.value = newValue
        except Exception as e:
            if not optional:
                logger.critical(
                    f"Should have written {newValue} into field {fieldName} "
                    f"and failed with error {e}. Most probably typo in Program."
                )
                raise AttributeError(
                    f"Should have written {newValue} into field {fieldName} "
                    f"and failed with error {e}. Most probably typo in Program."
                )
            else:
                logger.debug(
                    f"Should have written {newValue} into field {fieldName} "
                    f"and failed with error {e}. Most probably typo in Program. Optional was set, so Ok!"
                )
예제 #14
0
파일: utils.py 프로젝트: Athos1972/PoLZy
def returnDate(string):
    # some fields need date object, so we are using dateutils here to get date object
    try:
        dt = parse(string).strftime(GlobalConstants.dateFormat)
    except Exception as ex:
        logger.critical(f"Unable to parse date: {string}. Error was {ex}")
        dt = datetime.now().strftime(GlobalConstants.dateFormat)
    return dt
예제 #15
0
 def new(cls, activity_name, line_of_business, points=1):
     instance = cls(activity_name=activity_name, line_of_business=line_of_business, points=points)
     db.session.add(instance)
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #16
0
 def update_count():
     instance = db.session.query(AntragNummer).first()
     instance.count += 100
     db.session.add(instance)
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #17
0
 def finish(self, status):
     #
     # sets is_finished to True
     #
     self.status = status
     self.finished = datetime.utcnow()
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #18
0
 def set_processed(self, details=None):
     if not details:
         details = {}
     self.processed = True
     self.status_ok = details.get("status_ok", True)
     if isinstance(details, dict):
         details = json.dumps(details)
     self.details = details
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #19
0
파일: Antrag.py 프로젝트: Athos1972/PoLZy
    def get_activity(self, activity_name, optional=False):
        """
        Retrieve an activity from the list of Activities in this instance by class name or activity name
        :param activity_name: class name or activity name of the desired activity
        :param optional: Don't raise an error if the activity can't be found
        :return: the activity or None
        """
        for activity in self.Activities:
            if activity_name == activity.__class__.__name__ or activity_name == activity.name:
                return activity

        if not optional:
            logger.critical(f"Cannot find activity: {activity_name}",
                            stack_info=True)
예제 #20
0
    def new(cls, user_id, company_id, type_id):
        instance = cls(
            user_id=user_id,
            company_id=company_id,
            type_id=type_id,
        )
        db.session.add(instance)
        try:
            db.session.commit()
        except Exception as ex:
            logger.critical(f"Exception while committing changes to db: {ex}")
            db.session.rollback()

        return instance
예제 #21
0
파일: Antrag.py 프로젝트: Athos1972/PoLZy
 def _toggleFieldGroupOnOff(self, listOfFieldgroupNamesToToggle: list,
                            OnOffAsBoolean: bool):
     """
     Toggle a list of fieldgroups (=Cards on the UI) on or off by setting the value of the field-group to True/False
     :param listOfFieldgroupNamesToToggle: List of the names of the fieldgroups to be effected
     :param OnOffAsBoolean: True = On, False = Off
     :return:
     """
     for fielgroup in listOfFieldgroupNamesToToggle:
         try:
             self.Fields.getField(name=fielgroup).value = OnOffAsBoolean
         except AttributeError:
             logger.critical(
                 f"Field {fielgroup} not found. Typo? Forgot to add .value?"
             )
예제 #22
0
    def set_processed(self, commit=True):
        #
        # set activity as processed
        #

        self.processed = datetime.utcnow()
        self.is_processed = True

        # save to db
        if commit:
            try:
                db.session.commit()
            except Exception as ex:
                logger.critical(f"Exception while committing changes to db: {ex}")
                db.session.rollback()
예제 #23
0
 def update_badge(self):
     level_id = self.get_level_id()
     badge = db.session.query(GamificationBadge).filter_by(user_id=self.user_id).filter_by(
         company_id=self.company_id).filter_by(type_id=self.type_id).filter_by(level_id=level_id).first()
     if not badge:
         badge = GamificationBadge(
             user_id=self.user_id, company_id=self.company_id, type_id=self.type_id, level_id=level_id)
         db.session.add(badge)
         try:
             db.session.commit()
         except Exception as ex:
             logger.critical(f"Exception while committing changes to db: {ex}")
             db.session.rollback()
         ToastNotifications.new(
             message=badge.id, type="badge", company_ids=self.company_id, user_ids=self.user_id
         )
예제 #24
0
 def new(cls, message, type="default", duration=3000, horizontal="left", vertical="top", company_ids=None, user_ids=None):
     companies = []
     if company_ids:
         if isinstance(company_ids, list):
             for company_id in company_ids:
                 if db.session.query(Company).filter_by(id=company_id).first():
                     companies.append(company_id)
                 else:
                     print(f"Company id {company_id} is not correct. Please recheck.")
         else:
             if db.session.query(Company).filter_by(id=company_ids).first():
                 companies.append(company_ids)
             else:
                 print(f"Company id {company_ids} is not correct. Please recheck.")
     else:
         companies = [company.id for company in db.session.query(Company).all()]
     if not companies:
         print("Supplied company ids are not valid. Hence no notification will be updated")
     for company_id in companies:
         users = []
         if not user_ids:
             users = [user.id for user in db.session.query(User).filter_by(company_id=company_id).all()]
         else:
             if isinstance(user_ids, list):
                 for user_id in user_ids:
                     if db.session.query(User).filter_by(id=user_id).first():
                         users.append(user_id)
                     else:
                         print(f"User id {user_id} is not correct. Please recheck.")
             else:
                 if db.session.query(User).filter_by(id=user_ids).first():
                     users.append(user_ids)
                 else:
                     print(f"User id {user_ids} is not correct. Please recheck.")
         for user_id in users:
             if type == "badge":
                 instance = cls(company_id=company_id, user_id=user_id, message=message, type=type)
             else:
                 instance = cls(company_id=company_id, user_id=user_id, message=message, type=type,
                                duration=duration, horizontal=horizontal, vertical=vertical)
             db.session.add(instance)
     try:
         db.session.commit()
     except Exception as ex:
         logger.critical(f"Exception while committing changes to db: {ex}")
         db.session.rollback()
예제 #25
0
def newbadge():

    # get user
    user = User.query.first()
    if user is None:
        return {'error': 'There are no user in DB'}, 400

    # create random badge
    # get possible types
    badge_level = GamificationBadgeLevel.query.first()
    all_types = GamificationBadgeType.query.all()
    search_type = True
    count = 0
    while search_type:
        badge_type = choice(all_types)
        search_type = any(b.type == badge_type for b in user.badges)
        if search_type:
            current_badge = next(
                filter(lambda b: b.type == badge_type, user.badges))
            if current_badge.level.next_level:
                badge_level = current_badge.level.next_level
                new_badge = current_badge
                new_badge.level = badge_level
                new_badge.is_seen = False
                search_type = False
        else:
            new_badge = GamificationBadge(
                user=user,
                company=user.company.company,
                type=badge_type,
                level=badge_level,
            )
            db.session.add(new_badge)

        # break if all user badges of the highest level
        if count > 100:
            raise Exception('No new badge is available:(')

    try:
        db.session.commit()
    except Exception as ex:
        logger.critical(f"Exception while committing changes to db: {ex}")
        db.session.rollback()

    messenger.announce_badge(new_badge)
    return {'success': 'OK'}, 200
예제 #26
0
 def __init__(self, setting_file=Path(os.path.abspath(__file__)).parent.parent.joinpath("run_setting.ini")):
     logger.info("Executing __init__ in ConfigurationProvider")
     self.configsRead = {}
     self.badConfigs = {}
     # pathToConfig can and should be overwritten if needed otherwise:
     self.pathToConfig = Path(os.getcwd()).joinpath("Configurations")
     self.basePath = Path(os.getcwd())
     self.setting_file = setting_file
     if not self.pathToConfig.exists():
         self.pathToConfig = Path(os.path.abspath(__file__)).parent.parent.joinpath("Configurations")
         self.basePath = Path(os.path.abspath(__file__)).parent.parent
     result = self.__checkAndReadConfigurations("default")
     if not result:
         logger.critical("Default configuration was not found. Aborting.")
         raise NotImplementedError("Default configuration not found. Aborting.")
     self.language = "en"  # default fallback language is english
     self.offline, self.PDFOutput, self.defaultStage = self._get_default_data()
예제 #27
0
    def get_rank_by_category(self, rank_category, stats):
        # getting weighted points for every activity and adding them up per user
        unadded_user_points = {}
        for stat in stats:  # first getting weighted points of each activity
            try:
                if not stat.user.email in unadded_user_points:
                    unadded_user_points[stat.user.email] = []
            except AttributeError:
                logger.critical(
                    f"User id {stat.user_id} doesn't exist. So ranking is skipping it."
                )
                continue
            unadded_user_points[stat.user.email].append([
                stat.get_user_statistics().get(rank_category, 0),
                stat.user.email
            ])

        added_points = []
        for user_email in unadded_user_points:  # adding activity points to one per user
            score = 0
            for points in unadded_user_points[user_email]:
                score += points[0]
            added_points.append([score, user_email])

        last_score = -1
        last_rank = 0
        ranked_data = []
        for dt in sorted(added_points,
                         reverse=True):  # ranking user as per their points
            dic = {}
            if dt[0] != last_score:  # give same rank number if last iteration has same score
                last_rank += 1
                last_score = dt[0]
                dt.append(last_rank)
            else:
                dt.append(last_rank)
            dic['name'] = dt[1]
            dic['operations'] = dt[0]
            dic['rank'] = dt[2]
            ranked_data.append(dic)
        return ranked_data
예제 #28
0
 def create_or_update_row(cls, activity: GamificationActivity, commit=True):
     type_id = cls.get_type_id(activity.event, activity.event_details)
     weightage = cls.get_weight(activity.event, activity.event_details)
     if not type_id:
         return None
     user_id = activity.user_id
     company_id = activity.company_id
     user_stats = db.session.query(GamificationUserStats).filter_by(user_id=user_id
         ).filter_by(company_id=company_id).filter_by(type_id=type_id).first()
     if user_stats:
         user_stats.add_points(weight=weightage)
     if not user_stats:
         user_stats = GamificationUserStats.new(user_id=user_id, company_id=company_id, type_id=type_id)
         user_stats.add_points(weight=weightage)
     if commit:
         try:
             db.session.commit()
         except Exception as ex:
             logger.critical(f"Exception while committing changes to db: {ex}")
             db.session.rollback()
     return user_id
예제 #29
0
파일: utils.py 프로젝트: Athos1972/PoLZy
def updateFieldDataName(data):
    sanitized_data = dict()
    for key, value in data.items():
        if key not in valid_headers:  # then most probably this key contains value of some other keys in it -
            try:
                sanitize = getattr(sys.modules[__name__], key.lower())(
                    value)  # e.g. name is first_name and last_name
                sanitized_data.update(sanitize)
            except Exception as ex:
                logger.critical(
                    f"Found new header: {key}. This shouldn't be the case. Error was {ex}"
                )
        elif key in dateObj:  # if key is in date object category that means we need to convert value in datetime object
            processed = returnDate(value)
            sanitized_data[key] = processed
        else:
            if key not in skipped_headers:  # if key in skipped headers, that means current we want to avoid those keys.
                sanitized_data[key] = value
            else:
                logger.debug(f"{key} skipped.")
    return sanitized_data
예제 #30
0
    def new(cls, user, filename, id, parent_id=None, file_type=None):
        #
        # create new instance of File
        #

        instance = cls(
            id=id,
            filename=filename,
            user_id=user.id,
            company_id=user.company_id,
            parent_id=parent_id,
            type=file_type,
        )

        db.session.add(instance)
        try:
            db.session.commit()
        except Exception as ex:
            logger.critical(f"Exception while committing changes to db: {ex}")
            db.session.rollback()

        return instance