def random_id(): code = generate_id(size=8, chars=digits) while DataRequestProjectMember.objects.filter( project_member_id=code).count() > 0: code = generate_id(size=8, chars=digits) return code
def random_project_member_id(): """ Return a zero-padded string 16 digits long that's not already used in the database. """ code = generate_id(size=8, chars=digits) while (DataRequestProjectMember.objects.filter( project_member_id=code).count() > 0): code = generate_id(size=8, chars=digits) return code
def random_project_member_id(): """ Return a zero-padded string 16 digits long that's not already used in the database. """ code = generate_id(size=8, chars=digits) while ( DataRequestProjectMember.objects.filter(project_member_id=code).count() > 0 ): code = generate_id(size=8, chars=digits) return code
def save(self, conn, update=True, fields=None, columns=None): if fields: data = {k: getattr(self, k) for k in fields if hasattr(self, k)} else: data = self._get_editable_attrs() if set(self.JSON_FIELDS) & set(data.keys()): for f in self.JSON_FIELDS: v = data.get(f) if not v: continue [k.update(id=generate_id()) for k in v if not k.get(u'id')] if update: sql_query = get_update_query(self.TABLE, data, where_params=dict(id=self.id), editable_columns=columns or self.EDITABLE_FIELDS) else: sql_query = get_insert_query(self.TABLE, data, self.CREATE_FIELDS) try: cursor = yield momoko.Op(conn.execute, sql_query) self.id = cursor.fetchone()[0] except Exception, ex: raise PSQLException(ex)
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 refresh_token(self): """ Generate a new master access token that expires in 24 hours. """ self.master_access_token = generate_id() self.token_expiration_date = now_plus_24_hours() self.save()
def add_vacancy(cls, conn, vacancy_data): vacancy_id = generate_id(21) vacancy_data.update(id=vacancy_id) sqp_query = get_insert_query(globals.TABLE_VACANCIES, vacancy_data) try: yield momoko.Op(conn.execute, sqp_query) except PSQLException, ex: logging.exception(ex)
def add_participant(cls, conn, participant_data): """ Добавить участника в таблицу участников :param conn: :param participant_data: {project_id, role_name, scientist_id, first_name, last_name, middle_name} :type participant_data: dict """ participant_id = generate_id(21) participant_data.update(id=participant_id) sql_query = get_insert_query(globals.TABLE_PARTICIPANTS, participant_data) try: yield momoko.Op(conn.execute, sql_query) except PSQLException, ex: logging.exception(ex)
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))