Пример #1
0
def _get_questionnaire(q_name,
                       q_version,
                       q_description,
                       session=None,
                       **kwargs):
    if not session:
        session = Session()
    quest = session.query(Questionnaire).filter(
        and_(Questionnaire.name == q_name,
             Questionnaire.version == q_version)).first()
    if not quest:
        quest = Questionnaire()
        quest.name = q_name
        quest.description = q_description
        quest.version = q_version
        creator = kwargs.get('created_by')
        if creator:
            quest.created_by = creator
            quest.project_users.append(creator)
            quest.project_admins.append(creator)

        quest.results_table_name = get_result_table_name(quest)
        session.add(quest)
        session.commit()
    return quest
Пример #2
0
def handle_save(question, message): #todo handle all saves to db
	print('handle_save', question.code)
	if not question.save_in_survey:
		return
	check_data(question, message)
	session = Session()
	try:
		response = question.get_response_object(message.from_user.id)
	except (DetachedInstanceError, ProgrammingError):
		question = session.query(Question).filter(Question.id == question.id).first()
		response = question.get_response_object(message.from_user.id)

	if question.type.code == QuestionTypes.categorical:
		code = None
		for cat in question.categories:
			if cat.text == message.text:
				code = cat.id
				break
		response.__setattr__(question.code, code)
		session.add(response)
		session.commit()
	elif question.type.code == QuestionTypes.location:
		response.__setattr__(f'{question.code}_latitude', message.location.latitude)
		response.__setattr__(f'{question.code}_longitude', message.location.longitude)
		session.add(response)
		session.commit()
	elif question.type.code == QuestionTypes.photo:
		file_info = bot.get_file(message.photo[-1].file_id)
		print('handle_save', 'photo', file_info)
		downloaded_file = bot.download_file(file_info.file_path)
		file_name = '{}_{}'.format(message.from_user.id, file_info.file_path.split('/')[-1])
		file_folder = Path(MEDIA_FOLDER, question.questionnaire.results_table_name)
		file_path = Path(file_folder, file_name)
		# file_path.mkdir(parents=True)
		try:
			with open(file_path, 'wb') as out:
				out.write(downloaded_file)
		except FileNotFoundError:
			file_folder.mkdir(parents=True)
			with open(file_path, 'wb') as out:
				out.write(downloaded_file)
		print('handle_save', 'file saved', file_path)
		print('handle_save', 'file saved relative', str(file_path.relative_to(ROOT_DIR)))
		response.__setattr__(question.code, str(file_path.relative_to(ROOT_DIR)))
		session.add(response)
		session.commit()
	elif question.type.code == QuestionTypes.timestamp:
		response.__setattr__(question.code, datetime.datetime.now())
		session.add(response)
		session.commit()
	elif question.type.code == QuestionTypes.text or question.type.code == QuestionTypes.integer:
		print('TEXT Q TYPE', message.content_type, message.content_type == 'text')
		response.__setattr__(question.code, message.text)
		session.add(response)
		session.commit()
	else:
		print('handle_save', 'QUESTION TYPE NOT HANDLED FOR SAVE', question.type)
Пример #3
0
def handle_toggle_is_active(call, project_id):
	session = Session()
	selected_project = session.query(Questionnaire).filter(Questionnaire.id == project_id).first()
	selected_project.is_active = not selected_project.is_active
	session.add(selected_project)
	session.commit()
	msg = f'Selected project {selected_project} is {"active" if selected_project.is_active else "inactive"} now'
	bot.answer_callback_query(call.id, msg)
	bot.send_message(call.message.chat.id, msg)
Пример #4
0
def handle_toggle_is_interviewer(call, tg_id):
	session = Session()
	selected_user = session.query(User).filter(User.tg_id == tg_id).first()
	selected_user.is_interviewer = not selected_user.is_interviewer
	session.add(selected_user)
	session.commit()
	msg = f'Selected user {selected_user} is {"" if selected_user.is_interviewer else " NOT"} an interviewer now'
	bot.answer_callback_query(call.id, msg)
	bot.send_message(call.message.chat.id, msg)
Пример #5
0
def handle_assign_admin_to_project(call, tg_id, project_id):
	session = Session()
	selected_user = session.query(User).filter(User.tg_id == tg_id).first()
	selected_project = session.query(Questionnaire).filter(Questionnaire.id == project_id).first()
	if selected_user in selected_project.project_admins:
		selected_project.project_admins.remove(selected_user)
		bot.answer_callback_query(call.id, f'{selected_user} is NOT an admin in {selected_project}')
		bot.send_message(call.message.chat.id, f'{selected_user} is NOT an admin in {selected_project}')
	else:
		selected_project.project_admins.append(selected_user)
		bot.answer_callback_query(call.id, f'{selected_user} IS an admin in {selected_project}')
		bot.send_message(call.message.chat.id, f'{selected_user} IS an admin in {selected_project}')
	session.add(selected_project)
	session.commit()
Пример #6
0
def create_user(message, session=None):
	if not session:
		session = Session()
	user = User(
		tg_id=message.from_user.id,
		first_name=message.from_user.first_name,
		last_name=message.from_user.last_name,
		# chat_id=message.chat.id,
		username=message.from_user.username,
	)
	user.is_admin = user.tg_id in User.ADMIN_IDS
	session.add(user)
	session.commit()
	print('NEW USER ', user, user.id)
	warn_admins('New user joined: {}'.format(repr(user)), session)
	return user
Пример #7
0
def _save(question,
          questionnaire,
          step=None,
          force_save_in_survey=None,
          category_code_start=1,
          allow_overwrite=True,
          session=None):
    if not session:
        session = Session()
    question.questionnaire = questionnaire
    question.step = step
    question.save_in_survey = force_save_in_survey if force_save_in_survey is not None else question.should_be_saved_in_survey
    category_code = category_code_start
    for i in question.categories:
        i.code = i.code if i.code else category_code
        session.add(i)
        category_code += 1
    session.add(question)
    try:
        session.commit()
        print(f'{question.code} created')
    except IntegrityError:
        session.rollback()
        print(f'{question.code} already in database')
        if allow_overwrite:
            session.delete(
                session.query(Question).filter(
                    and_(Question.code == question.code,
                         Question.questionnaire_id ==
                         questionnaire.id)).first())
            session.commit()
            print(f'{question.code} deleted')
            session.add(question)
            session.commit()
            print(f'{question.code} created')
    if step:
        step += 1
Пример #8
0
from settings import MY_TG
from survey.models import recreate_all, Session, User
from survey.result_table import create_result_table

recreate_all()
me = User(tg_id=MY_TG, first_name='A', last_name='D', is_admin=True)
s = Session()
s.add(me)
s.commit()

import gluten_shops.survey_script as script

create_result_table(script.questionnaire)
# print('READ THIS', 'https://docs.sqlalchemy.org/en/13/orm/extensions/automap.html')