def create(cls, scientist_dict=None, scientist_photo=None, test_mode=False): # check if user can create account yield cls.validate_credentials(scientist_dict) # create ID if test_mode: scientist_id = scientist_dict.get(u'id') else: scientist_id = generate_id(21) # create account yield cls.update_roles(scientist_id, scientist_dict) editable_data = Scientist.get_editable_data(scientist_dict, update=False) editable_data.update(id=scientist_id) image_url = yield cls.upload_avatar(scientist_id, scientist_photo) if image_url: editable_data.update(image_url=image_url) scientist = Scientist(**editable_data) yield scientist.save(update=False, fields=editable_data.keys()) image_url = globals.GET_IMG(image_url, globals.IMG_S) if image_url else u'' raise gen.Return(dict(scientist_id=scientist_id, image_url=image_url))
def delete(cls, scientist_id): scientist = yield Scientist.get_by_id(scientist_id) try: print 'deleting from postgres' yield Scientist.delete(scientist_id, tbl=globals.TABLE_ROLES) except PSQLException, ex: raise ex
def get_my_projects(cls, manager_id): scientist_columns = [u'managing_project_ids'] scientist_json = yield Scientist.get_json_by_id(manager_id, columns=scientist_columns) # [{scientist_id, vacancy_id, message}] project_ids = scientist_json.get(u'managing_project_ids', []) if not project_ids: raise gen.Return([]) projects = [] for project_id in project_ids: project_columns = [u'title', u'responses'] project_json = yield Project.get_json_by_id(project_id, columns=project_columns) project_json.update(project_id=project_id) raw_responses = project_json.pop(u'responses', []) if not raw_responses: project_json.update(responses=[]) projects.append(project_json) continue responses = [] for response in raw_responses: response_data = yield cls.get_response_data(response) responses.append(response_data) project_json.update(responses=responses) projects.append(project_json) raise gen.Return(projects)
def delete_like(cls, project_id, scientist_id): project = yield Project.get_by_id(project_id) project.likes -= 1 scientist = yield Scientist.get_by_id(scientist_id) scientist.liked_projects.remove(project_id) yield project.save(fields=['likes']) yield scientist.save(fields=['liked_projects'])
def add_like(cls, project_id, scientist_id): project = yield Project.get_by_id(project_id) project.likes += 1 scientist = yield Scientist.get_by_id(scientist_id) scientist.liked_projects = list(set(scientist.liked_projects.append(project_id))) yield project.save(fields=['likes']) yield scientist.save(fields=['liked_projects'])
def add_like(cls, project_id, scientist_id): project = yield Project.get_by_id(project_id) project.likes += 1 scientist = yield Scientist.get_by_id(scientist_id) scientist.liked_projects = list( set(scientist.liked_projects.append(project_id))) yield project.save(fields=['likes']) yield scientist.save(fields=['liked_projects'])
def get(self, *args, **kwargs): print u'scientist search' search_data = json.loads(self.get_argument(u'data', u'{}')) try: response = yield Scientist.search(search_data) except Exception, ex: logging.info('Exc on search scientists:') logging.exception(ex) response = dict( message=ex.message )
def get(cls, scientist_id): """ :param scientist_id: :type scientist_id: int :rtype: dict """ data = yield Scientist.get_json_by_id(scientist_id) image_url = data.get(u'image_url', u'') and globals.GET_IMG(data.get(u'image_url', u''), globals.IMG_L) data.update(image_url=image_url) logging.info(data) raise gen.Return(data)
def get_all(cls): data = yield Scientist.get_all_json(columns=Scientist.OVERVIEW_FIELDS) scientists = [] for d in data: image_url = d.get(u'image_url', u'') and globals.GET_IMG(d.get(u'image_url', u''), globals.IMG_L) scientists.append(dict( id=d[u'id'], image_url=image_url, full_name='{} {} {}'.format(d.get(u'first_name', u''), d.get(u'middle_name', u''), d.get(u'last_name', u'')), location='{} {}'.format(d.get(u'city', u''), d.get(u'country', u'')), projects=len(d.get(u'participating_projects', [])) )) raise gen.Return(scientists)
def delete_participation(cls, scientist_id, data): """ :param scientist_id: :param data: {role_id, project_id} """ scientist = yield Scientist.get_by_id(scientist_id) project = yield Project.get_by_id(data[u'project_id']) scientist.participating_projects.remove(dict(project_id=project.id, role_id=data[u'role_id'])) yield scientist.save(fields=[u'participating_projects']) for i in xrange(len(project.participants)): if project.participants[i][u'id'] == data[u'role_id']: project.participants[i][u'status'] = globals.STATUS_DELETED yield project.save(fields=[u'participants'])
def delete_desired_project(cls, scientist_id, data): """ :param scientist_id: :param data: {project_id, vacancy_id} """ scientist = yield Scientist.get_by_id(scientist_id) project = yield Project.get_by_id(data[u'project_id']) scientist.desired_vacancies = [scientist.desired_vacancies[i] for i in xrange(len(scientist.desired_vacancies)) if scientist.desired_vacancies[i][u'vacancy_id'] != data[u'vacancy_id']] yield scientist.save(fields=[u'desired_vacancies']) project.responses = [project.responses[i] for i in xrange(len(project.responses)) if (project.responses[i][u'scientist_id'] != scientist_id) and (project.responses[u'vacancy_id'] != data[u'vacancy_id'])] yield project.save(fields=[u'responses'])
def add_response(cls, conn, data): """ Участник нажимает кнопку "Участвовать" у проекта. Добавляем втаблицу откликов новое значение. Прописываем отклик у ученого и у проекта. :param data: {scientist_id, project_id, vacancy_id, message} :type data: dict """ project = yield Project.get_by_id(data[u'project_id']) scientist = yield Scientist.get_by_id(data[u'scientist_id']) sql_query = get_insert_query(globals.TABLE_RESPONSES, data, returning=u'') try: yield momoko.Op(conn.execute, sql_query) except PSQLException, ex: logging.exception(ex) raise
def delete(cls, project_id): """ Удалить участников, удалить вакансии, поставить статус откликам - удаленные. У ученого убрать из managing_project_ids. """ project = yield Project.get_by_id(project_id) for participant_id in project.participants: yield cls.delete_participant(participant_id) for vacancy_id in project.vacancies: yield cls.delete_vacancy(vacancy_id) for response_id in project.responses: yield cls.set_del_status_response(response_id) scientist = yield Scientist.get_by_id(project.manager_id) scientist.managing_project_ids.remove(project_id) yield scientist.save(fields=[u'managing_project_ids'], columns=[u'managing_project_ids']) yield Project.delete(project_id, tbl=Project.TABLE)
def get_response_data(cls, conn, response_id): scientist_id = response_id[:response_id.find(u':')] project_id = response_id[response_id.find(u':')+1:response_id.rfind(u':')] vacancy_id = response_id[response_id.rfind(u':')+1:] response_data = dict( scientist_id=scientist_id, vacancy_id=vacancy_id ) # get message and status columns = [u'message', u'status'] where_list = [ dict( column=u'scientist_id', value=scientist_id ), dict( column=u'project_id', value=project_id ), dict( column=u'vacancy_id', value=vacancy_id ) ] sql_query = get_select_query(globals.TABLE_RESPONSES, columns=columns, where=where_list) cursor = yield momoko.Op(conn.execute, sql_query) data = cursor.fetchone() response_data.update(dict(zip(columns, data))) # get vacancy name v_col = [u'vacancy_name'] sql_query = get_select_query(globals.TABLE_VACANCIES, columns=v_col, where=dict(column=u'id', value=vacancy_id)) cursor = yield momoko.Op(conn.execute, sql_query) data = cursor.fetchone() response_data.update(dict(zip(v_col, data))) # get scientist name scientist = yield Scientist.get_by_id(scientist_id) scientist_name = u' '.join(map(lambda x: x.decode('utf8'), [scientist.last_name, scientist.first_name, scientist.middle_name])) response_data.update(dict( scientist_name=scientist_name, )) raise gen.Return(response_data)
def get_participation_projects(cls, scientist_id): cols = [u'participating_projects'] sc_json = yield Scientist.get_json_by_id(scientist_id, columns=cols) # [{project_id, role_id}] projects = [] for participation in sc_json.get(u'participating_projects', []): project_columns = [u'title', u'participants'] project_data = yield Project.get_json_by_id(participation[u'project_id'], columns=project_columns) participants = project_data.pop(u'participants') project_data.update(project_id=participation[u'project_id'], role_name=[k[u'role_name'] for k in participants if k[u'id'] == participation[u'role_id']][0] ) projects.append(project_data) print projects raise gen.Return(projects)
def get_desired_projects(cls, scientist_id): cols = [u'desired_vacancies'] sc_json = yield Scientist.get_json_by_id(scientist_id, columns=cols) # [{project_id, vacancy_id}] projects = [] for application in sc_json(u'desired_vacancies', []): project_columns = [u'title', u'vacancies'] project_data = yield Project.get_json_by_id(application[u'project_id'], columns=project_columns) vacancies = project_data.pop(u'vacancies', []) project_data.update(project_id=application[u'project_id'], vacancy_id=application[u'vacancy_id'], vacancy_name=[k[u'vacancy_name'] for k in vacancies if k[u'id'] == application[u'vacancy_id']][0] ) projects.append(project_data) print projects raise gen.Return(projects)
def create(cls, project_dict, test_mode=False): participants = project_dict.pop(u'participants', []) vacancies = project_dict.pop(u'vacancies', []) if not project_dict.get(u'manager_id'): raise Exception(u'No manager ID in creating project') # create ID if test_mode: project_id = project_dict.pop(u'id') else: project_id = generate_id(21) editable_data = Project.get_editable_data(project_dict, update=False) editable_data.update(id=project_id) project = Project(**editable_data) yield project.save(update=False, fields=editable_data.keys()) participant_ids = [] for participant in participants: participant.update(project_id=project_id) participant_id = yield cls.add_participant(participant) participant_ids.append(participant_id) vacancy_ids = [] for vacancy in vacancies: vacancy.update(project_id=project_id) v_id = yield cls.add_vacancy(vacancy) vacancy_ids.append(v_id) project.participants = participant_ids project.vacancies = vacancy_ids yield project.save(fields=[u'participants', u'vacancies']) scientist = yield Scientist.get_by_id(editable_data[u'manager_id']) scientist.managing_project_ids.append(project_id) yield scientist.save(fields=[u'managing_project_ids'], columns=[u'managing_project_ids']) raise gen.Return(dict(id=project_id))
def update(cls, scientist_dict, scientist_photo): scientist_id = scientist_dict.pop(u'scientist_id') if not scientist_id: raise Exception(u'No scientist id on update') scientist = yield Scientist.get_by_id(scientist_id) # if image arrived - change image url, else save the same if scientist_photo: new_image_url = yield cls.upload_avatar(scientist_id, scientist_photo) scientist_dict.update(image_url=new_image_url) else: scientist_dict.update(image_url=scientist.image_url) updated_data = scientist.get_updated_data(scientist_dict) scientist.populate_fields(updated_data) yield scientist.save(fields=updated_data.keys()) image_url = globals.GET_IMG(scientist.image_url, globals.IMG_S) if scientist.image_url else u'' raise gen.Return(dict(scientist_id=scientist_id, image_url=image_url))
returning=u'') try: yield momoko.Op(conn.execute, sql_query) except PSQLException, ex: logging.exception(ex) if data[u'result'] == globals.STATUS_ACCEPTED: sql_query = get_select_query(globals.TABLE_VACANCIES, columns=[u'vacancy_name'], where=dict(column=u'id', value=data[u'vacancy_id'])) cursor = yield momoko.Op(conn.execute, sql_query) vacancy_name = cursor.fetchone()[0] scientist = yield Scientist.get_by_id(data[u'scientist_id']) participant_data = dict( project_id=data[u'project_id'], scientist_id=data[u'scientist_id'], role_name=vacancy_name, first_name=scientist.first_name.decode('utf-8') if isinstance( scientist.first_name, str) else scientist.first_name, last_name=scientist.last_name.decode('utf-8') if isinstance( scientist.last_name, str) else scientist.last_name, middle_name=scientist.middle_name.decode('utf-8') if isinstance(scientist.middle_name, str) else scientist.middle_name, ) project = yield Project.get_by_id(data[u'project_id']) participant_id = yield cls.add_participant(participant_data) project.participants.append(participant_id)
def get_current_user(self): user = None if self.current_user_id: user = yield Scientist.get_by_id(self.current_user_id) raise gen.Return(user)
where_params=dict(scientist_id=data[u'scientist_id'], project_id=data[u'project_id'], vacancy_id=data[u'vacancy_id']), returning=u'') try: yield momoko.Op(conn.execute, sql_query) except PSQLException, ex: logging.exception(ex) if data[u'result'] == globals.STATUS_ACCEPTED: sql_query = get_select_query(globals.TABLE_VACANCIES, columns=[u'vacancy_name'], where=dict(column=u'id', value=data[u'vacancy_id'])) cursor = yield momoko.Op(conn.execute, sql_query) vacancy_name = cursor.fetchone()[0] scientist = yield Scientist.get_by_id(data[u'scientist_id']) participant_data = dict( project_id=data[u'project_id'], scientist_id=data[u'scientist_id'], role_name=vacancy_name, first_name=scientist.first_name.decode('utf-8') if isinstance(scientist.first_name, str) else scientist.first_name, last_name=scientist.last_name.decode('utf-8') if isinstance(scientist.last_name, str) else scientist.last_name, middle_name=scientist.middle_name.decode('utf-8') if isinstance(scientist.middle_name, str) else scientist.middle_name, ) project = yield Project.get_by_id(data[u'project_id']) participant_id = yield cls.add_participant(participant_data) project.participants.append(participant_id) yield project.save(fields=[u'participants'], columns=[u'participants']) @classmethod @gen.coroutine