Exemple #1
0
    def get(self):

        user = UserModel.find_by_username(email=get_jwt_identity())
        results = []
        company = CompanyModel.find_by_id(id=user.id_company)
        if (company == None):
            return {'data': []}
        else:
            #return jsonify(json_list = questions)
            return {
                "id": company.id,
                "sector": company.sector,
                "subsector": company.subsector,
                "commercial_name": company.commercial_name,
                "fiscal_name": company.fiscal_name,
                "nif": company.nif,
                "number_survey": company.number_survey,
                "duplication_survey": company.duplication_survey,
                "name_surname": company.name_surname,
                "telephone_number": company.telephone_number,
                "description": company.description,
                "comarca": company.comarca,
                "territori_leader": company.territori_leader,
                "number_workers": company.number_workers
            }
Exemple #2
0
 def get(self):
     user = UserModel.find_by_username(email=get_jwt_identity())
     results = []
     company = CompanyModel.find_by_id(id=user.id_company)
     items = SurveyCompanyModel.find_by_company_id(
         id_company=user.id_company)
     if (len(items) == 0):
         return {'data': []}
     else:
         #return jsonify(json_list = questions)
         for item in items:
             results.append({
                 "id":
                 item.id,
                 "id_survey":
                 item.id_survey,
                 "name_survey":
                 item.name_survey,
                 "id_company":
                 item.id_company,
                 "status":
                 item.status,
                 "score":
                 item.score,
                 "last_modified":
                 item.last_date.strftime('%m/%d/%Y %H:%M'),
                 "version":
                 item.version
             })
         return results
Exemple #3
0
    def get(cls, id):

        company = CompanyModel.find_by_id(id)

        if company:
            return {"company": company_schema.dump(company)}

        return {"message": "not found"}, 404
	def save_employee (self,first_name,last_name,marital_status,gender,date_of_birth,id):
		#id will be greater than zero when EDIT action is triggered.
		if id>0:
			emp_k = db.Key.from_path('CompanyModel','Bellucci','EmployeeModel',long(id))
			emp = db.get(emp_k)
		else:
			comp = CompanyModel(key_name='Bellucci',name='Bellucci Enterprise')
			comp.put()
			emp = EmployeeModel(parent = comp)

		emp.first_name = first_name
		emp.last_name = last_name
		emp.marital_status = marital_status
		emp.gender = gender
		emp.date_of_birth = datetime.date(year=int(date_of_birth[6:10]), month=int(date_of_birth[3:5]), day=int(date_of_birth[0:2]))
		emp.user_name = users.get_current_user().email()
		emp.put()
Exemple #5
0
    def delete(cls, id):

        company = CompanyModel.find_by_id(id)

        if company:
            company.delete_from_db()

            return {"message": "Compnay deleted!"}

        return {"message": "not found"}, 404
Exemple #6
0
    def save_employee(self, first_name, last_name, marital_status, gender,
                      date_of_birth, id):
        #id will be greater than zero when EDIT action is triggered.
        if id > 0:
            emp_k = db.Key.from_path('CompanyModel', 'Bellucci',
                                     'EmployeeModel', long(id))
            emp = db.get(emp_k)
        else:
            comp = CompanyModel(key_name='Bellucci',
                                name='Bellucci Enterprise')
            comp.put()
            emp = EmployeeModel(parent=comp)

        emp.first_name = first_name
        emp.last_name = last_name
        emp.marital_status = marital_status
        emp.gender = gender
        emp.date_of_birth = datetime.date(year=int(date_of_birth[6:10]),
                                          month=int(date_of_birth[3:5]),
                                          day=int(date_of_birth[0:2]))
        emp.user_name = users.get_current_user().email()
        emp.put()
Exemple #7
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id_survey',
                            help='This field cannot be blank',
                            required=True)
        parser.add_argument('id_company',
                            help='This field cannot be blank',
                            required=True)
        parser.add_argument('file_dari',
                            help='This field cannot be blank',
                            required=False)

        data = parser.parse_args()

        items = SurveyCompanyModel.find_by_company_id(
            id_company=data['id_company'])
        company = CompanyModel.find_by_id(id=data['id_company'])
        version = len(items) + 1
        new_surveycompany = SurveyCompanyModel(
            id_survey=data['id_survey'],
            name_survey=company.commercial_name + ".v" + str(version),
            id_company=data['id_company'],
            version=version,
            status='created',
            start_date=datetime.utcnow(),
            last_date=datetime.utcnow(),
            score=0,
            file_dari=data['file_dari'])
        try:
            survey = SurveyModel.find_by_id(id=data['id_survey'])
            questions = [int(s) for s in survey.questions.split(',')]
            answers_id = []
            for question in questions:
                answer = AnswerModel(id_question=question,
                                     score=-1,
                                     future=False,
                                     id_option=-1,
                                     justification_text='',
                                     justification_file='')
                answer.save_to_db()
                answers_id.append(answer.id)
            new_surveycompany.answers = ','.join([str(i) for i in answers_id])

            new_surveycompany.save_to_db()
            return {
                'message':
                'Survey company ' + str(new_surveycompany.id) + ' was created'
            }
        except Exception as e:
            print(e)
            return {'message': 'Something went wrong ' + str(e)}, 500
Exemple #8
0
    def get(self):
        users = CompanyModel.find_all()
        results = []
        if (len(users) == 0):
            return {'data': []}
        else:
            #return jsonify(json_list = questions)
            for item in users:
                results.append({
                    "email": item.email,
                    "type": item.type_user,
                    "company": item.id_company
                })

            return {'data': results}
Exemple #9
0
    def get(self):
        user = UserModel.find_by_username(email=get_jwt_identity())
        company = CompanyModel.find_by_id(id=user.id_company)

        results = []
        survey = SurveyModel.find_by_sector(company.sector, company.subsector)
        if (survey == None):
            return {'data': []}
        else:
            #return jsonify(json_list = questions)
            return {
                "id": survey.id,
                "sector": survey.sector,
                "subsector": survey.subsector,
                "questions": survey.questions
            }
Exemple #10
0
    def put(cls, id):

        company_json = request.get_json()["company"]
        users_json = company_json["users"]
        company = CompanyModel.find_by_id(id)

        if company:

            # validate company
            if company_json.get("name") != company.name or company_json.get(
                    "email") != company.email:
                if CompanyModel.find_by_name_or_email(
                        company_json.get("name"), company_json.get("email")):
                    return {
                        "message": "Company name or email already exists"
                    }, 400

            # delete users
            new_users = []
            for user in company.users:
                exists_user = next(
                    filter(lambda usr: usr.get("id") == user.id, users_json),
                    None)

                if exists_user:
                    new_users.append(user)

            company.users = new_users

            # validates usernames and emails
            for new_user in users_json:

                current_user = next(
                    filter(lambda usr: usr.id and usr.id == new_user.get("id"),
                           company.users), None)

                # if the user already exists
                if current_user:

                    if current_user.username != new_user.get("username"):
                        if UserModel.find_by_username(
                                new_user.get("username")):
                            return {
                                "message": "Username or email already exists11"
                            }, 400

                    if current_user.email != new_user.get("email"):
                        if UserModel.find_by_email(new_user.get("email")):
                            return {
                                "message": "Username or email already exists11"
                            }, 400

                    current_user.username = new_user["username"]
                    current_user.email = new_user["email"]
                    current_user.role_id = new_user["role_id"]

                    if new_user.get("password"):
                        hashed_password = custom_pbkdf2.hash(
                            new_user.get("password"))
                        current_user.password = hashed_password

                # if the user is new
                else:

                    if UserModel.find_by_username_or_email(
                            new_user.get("username"), new_user.get("email")):
                        return {
                            "message": "Username or email already exists22"
                        }, 400

                    hashed_password = custom_pbkdf2.hash(new_user["password"])
                    new_user["password"] = hashed_password

                    user_to_add = user_schema.load(new_user)
                    company.users.append(user_to_add)

            company.name = company_json.get("name")
            company.email = company_json.get("email")
            company.mobile_number = company_json.get("mobile_number")
            company.address = company_json.get("address")

            company.save_to_db()

            for user in company.users:
                if not user.profile_config:
                    user_setting = user_setting_schema.load(
                        {"user_id": user.id})
                    user_setting.save_to_db()

            return {
                "message": "Company updated",
                "company": company_schema.dump(company)
            }

        return {"message": "not found"}, 404
Exemple #11
0
    def get(cls):

        companies = CompanyModel.get_companies()

        return {"companies": company_schema.dump(companies, many=True)}
Exemple #12
0
 def post(self):
     parser = reqparse.RequestParser()
     parser.add_argument('sector',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('subsector',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('commercial_name',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('fiscal_name',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('nif',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('duplication_survey',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('name_surname',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('telephone_number',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('description',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('comarca',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('territori_leader',
                         help='This field cannot be blank',
                         required=True)
     parser.add_argument('number_workers',
                         help='This field cannot be blank',
                         required=True)
     data = parser.parse_args()
     if CompanyModel.find_by_nif(data['nif']):
         return {'message': 'Company {} already exists'.format(data['nif'])}
     new_company = CompanyModel(sector=data['sector'],
                                subsector=data['subsector'],
                                commercial_name=data['commercial_name'],
                                fiscal_name=data['fiscal_name'],
                                nif=data['nif'],
                                number_survey=0,
                                duplication_survey=False,
                                name_surname=data['name_surname'],
                                telephone_number=data['telephone_number'],
                                description=data['description'],
                                comarca=data['comarca'],
                                territori_leader=data['territori_leader'],
                                number_workers=data['number_workers'])
     try:
         new_company.save_to_db()
         return {
             'message': 'Company {} was created'.format(data['username'])
         }
     except Exception as e:
         print(e)
         return {'message': 'Something went wrong ' + str(e)}, 500