Beispiel #1
0
    def generate_and_add_tickets(self, amount=60):
        name_prefix = "Example ticket number "
        try:
            last_example_ticket = Ticket.objects.filter(name__startswith=name_prefix).order_by('pk').reverse()[0]
        except IndexError:
            start_num = 1
        else:
            start_num = int(search(r'(\d+)$', last_example_ticket.name).group(0)) + 1

        def generate_random_date():
            year_today = datetime.today().year
            # probably shouldn't matter, but preventing future dates.
            year = randint(year_today - 3, year_today - 1)
            month = randint(1, 12)
            # february only has 28 days, making this harder by
            # including 29-31 is unnecessary
            day = randint(1, 28)
            hour = randint(1, 23)
            minute = randint(1, 59)
            return datetime(year, month, day, hour, minute, tzinfo=UTC)

        user_objects = User.objects.filter(username__startswith=USERNAME_PREFIX)
        if not user_objects.exists():
            raise DataError('To create example tickets, at least one example user has to be created.')
        topic_objects = Topic.objects.all()
        subtopic_objects = Subtopic.objects.all()
        ticket_query_list = []
        for n in range(start_num, start_num + amount):
            created_and_updated = generate_random_date()
            topic = choice(topic_objects)
            try:
                subtopic = choice(subtopic_objects.filter(topic=topic))
            except IndexError:
                # topic has no subtopics
                subtopic = None

            ticket_query_list.append(Ticket(
                created=created_and_updated,
                updated=created_and_updated,
                event_date=generate_random_date(),
                requested_user=choice(user_objects),
                name="{}{}".format(name_prefix, n),
                topic=topic,
                subtopic=subtopic,
                rating_percentage=randint(0, 100),
                mandatory_report=choice([True, False]),
                description=("Space for further notes. If you're entering a trip "
                             "tell us where did you go and what you did there."),
                supervisor_notes=("This space is for notes of project "
                                  "supervisors and accounting staff."),
            ))
        Ticket.objects.bulk_create(ticket_query_list)
Beispiel #2
0
    def create(name: str, artists: str, album: str, length: int) -> bool:

        try:
            new_song = Song.objects.create(name=name,
                                           artist=artists,
                                           album=album,
                                           length=length)
        except ValueError:
            raise DataError("Song hasn't been saved")
        else:
            new_song.save()
            return True

        return False
Beispiel #3
0
    def from_db_value(value, *_args):
        """Convert a database value to a list value."""
        if not value:
            return None

        try:
            duration, unit = value.split("|")
        except ValueError as error:
            raise DataError(
                "Value in database should be of the form '{duration:d}|{unit:s}'"
            ) from error

        if not duration:
            return None

        return int(duration), unit
Beispiel #4
0
    def save_mutations(self, strain, var):
        """Save all the mutations in the var file"""
        for snp in var.values():
            #gene = snp['regionid1']
            if 'varname' not in snp or len(snp['varname']) > 80:
                continue
            try:
                (_, locus, mutation) = unpack_mutation_format(snp['varname'])
                # All genes in the gene summary should be already loaded.
                #locus = GeneLocus.objects.get(name=locus, genome=self.genome)
                # Ignore that and ask for a new one
                locus, _ = GeneLocus.objects.update_or_create(name=locus, genome=self.genome)
            except ValueError:
                sys.stderr.write("Failed to unpack {varname}\n".format(**snp))
                continue
            except GeneLocus.DoesNotExist:
                raise DataError("Failed to get gene {varname}, "\
                    "are all genes loaded from reference?".format(**snp))

            if len(mutation) > 150:
                sys.stderr.write("Mutation name is too large, can not add to database.\n")
                continue

            try:
                (mutation, _) = locus.mutations.get_or_create(name=mutation, defaults=dict(
                    nucleotide_position=None,
                    nucleotide_reference=None,
                    nucleotide_varient=None,
                    aminoacid_position=None,
                    aminoacid_reference=None,
                    aminoacid_varient=None,
                    codon_position=snp.get('codpos', None),
                    codon_varient=snp.get('altcodon', None),
                    codon_reference=snp.get('codon', None),
                ))

                strain.mutations.get_or_create(mutation=mutation, defaults=dict(
                    quality=to_num(snp.get('qual', 0)),
                    mutation_reads=to_num(snp.get('hqr', 0)),
                    reference_reads=to_num(snp.get('hqr_ref', 0)),
                    mapping_quality=to_num(snp.get('fq', 0)))
                )
            except (ValueError, IndexError, KeyError) as err:
                sys.stderr.write("Failed to add mutation: {} ({}) {}\n".format(mutation, err, snp))
                continue
Beispiel #5
0
    def to_python(self, value):
        """Convert a string value to a list value. Used for deserialization and in clean forms."""
        if isinstance(value, (list, tuple)):
            return value

        if not value:
            return None

        if isinstance(value, str):
            try:
                duration, unit = value.split("|")
            except ValueError as error:
                raise DataError(
                    "Value in database should be of the form '{duration:d}|{unit:s}'"
                ) from error

            return int(duration.strip()), unit.strip()

        return value
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        """
        Função utilizada para realizar o login do usuário

        Parameters:
        request (HttpRequest): Post Request contendo email e password do usuário

        Returns:
        HttpResponse: Retorna o token de autenticação como dado da resposta.

        """
        email = request.data['email']
        password = request.data['password']
        try:
            user = self.queryset.get(email=email)
            if not check_password(password, user.password):
                raise DataError("Senha errada.")
        except:
            return Response(data={'error': 'Wrong email or password.'},
                            status=status.HTTP_403_FORBIDDEN)
        token, created = Token.objects.get_or_create(user=user)
        return Response({'token': f'{token}'}, status=status.HTTP_202_ACCEPTED)
Beispiel #7
0
    def import_vcf(self, importer, vcf, var):
        """Import the VCF file and list of mutations into database"""
        if 'ENRICHED' not in vcf.metadata:
            raise NotEnrichedError()

        if 'LOCATION' not in vcf.metadata:
            raise DataError('Location is missing from metadata')

        loc = vcf.metadata['LOCATION'][0]
        con = loc.get('COUNTRY', None)
        country = long_match(COUNTRY_MAP, self.countries, con, Country, None,\
            'name', 'detail__name_short', 'detail__name_abbr', 'iso2', 'iso3')

        if country is None:
            raise DataError("Country not found: {}".format(con))

        city = long_match(CITY_MAP, self.places, loc.get('CITY', None),\
            Place, None, 'name', country=country)

        pat = vcf.metadata.get('PATIENT', [{}])[0]
        datum = dict(
            importer=importer,
            country=country, city=city,
            patient_id=pat.get('ID', 'None'),
            patient_sex=pat.get('SEX', None),
            patient_age=(pat.get('AGE', None) or None),
            patient_hiv=pat.get('HIV', None),
            # TODO: Patient notes are discarded here, maybe keep them.
        )

        # Drug data must exist!
        drugs = vcf.metadata.get('DRUG', [])
        if not drugs:
            raise DataError("No phenotype drug resistance data.")

        # BIO SAMPLE first.
        name = vcf.metadata.get('SAMPLE_ID', (None,))[0]
        name = EMPTY.get(name, name)

        other_name = vcf.metadata.get('STRAIN_NAME', (None,))[0]
        other_name = EMPTY.get(other_name, other_name)

        if name is None:
            if other_name is None:
                raise DataError("No valid STRAIN_NAME or bio SAMPLE_ID")
            name, other_name = other_name, None
        datum['old_id'] = other_name

        if 'LINEAGE' in vcf.metadata:
            datum['lineage'] = Lineage.objects.get_or_create(
                name=vcf.metadata['LINEAGE'][0],
                slug=name,
            )[0]

        study = None
        if 'STUDY' in vcf.metadata:
            name = vcf.metadata['STUDY'][0].get('NAME', None)
            url = vcf.metadata['STUDY'][0].get('URL', None)
            doi = vcf.metadata['STUDY'][0].get('DOI', None)
            if doi:
                study = Paper.objects.get_or_create(doi=doi, defaults={'name': name, 'url': url})[0]
            elif name:
                study = Paper.objects.get_or_create(name=name, defaults={'url': url})[0]
            if study is not None:
                datum['source_paper'] = study

        if 'PROJECT_ID'in vcf.metadata:
            datum['bioproject'] = BioProject.objects.get_or_create(
                name=vcf.metadata['PROJECT_ID'][0])[0]

        strain, _ = StrainSource.objects.update_or_create(name=name, defaults=datum)

        for _drug in vcf.metadata.get('DRUG', []):
            try:
                drug = long_match({
                    'OFLOXACIN': 'OFLX',
                }, self.drugs, _drug['NAME'], Drug, 'NOP', 'name', 'code', _match='icontains')
            except Drug.DoesNotExist:
                raise DataError("Can't import drug: {}".format(_drug['NAME']))
            drug.strains.update_or_create(strain=strain, defaults=dict(
                resistance=_drug['STATUS'].lower(),
                # TODO: More drug testing information here.
            ))

        strain.generate_resistance_group()
        self.save_mutations(strain, var)
        return name
Beispiel #8
0
	def save(self, *args, **kwargs):
		if self.balance < 0:
			print("positive forced")
			self.balance = 0
			raise DataError("my exeption~~~~~~")
		super().save(*args, **kwargs)
    def grab_organization_posts(self, org_dict, parent=None):
        url = base_url + '/organizations/'
        r = requests.get(url, params=org_dict)
        page_json = json.loads(r.text)
        organization_ocd_id = page_json['results'][0]['id']

        url = base_url + '/' + organization_ocd_id + '/'
        r = requests.get(url)
        page_json = json.loads(r.text)

        if page_json.get('error'):
            raise DataError(page_json['error'])

        source_url = ''
        if page_json['sources']:
            source_url = page_json['sources'][0]['url']

        if DEBUG: print(page_json['id'])

        if parent:
            if DEBUG: print('parent')
            try:
                org_obj, created = Organization.objects.get_or_create(
                    ocd_id=organization_ocd_id,
                    name=page_json['name'],
                    classification=page_json['classification'],
                    source_url=source_url,
                    slug=slugify(page_json['name']),
                    _parent=parent,
                )
            except IntegrityError:
                ocd_id_part = organization_ocd_id.rsplit('-', 1)[1]
                org_obj, created = Organization.objects.get_or_create(
                    ocd_id=organization_ocd_id,
                    name=page_json['name'],
                    classification=page_json['classification'],
                    source_url=source_url,
                    slug=slugify(page_json['name']) + ocd_id_part,
                    _parent=parent,
                )
        else:
            if DEBUG: print('not parent')
            try:
                org_obj, created = Organization.objects.get_or_create(
                    ocd_id=organization_ocd_id,
                    name=page_json['name'],
                    classification=page_json['classification'],
                    source_url=source_url,
                    slug=slugify(page_json['name']),
                )
            except IntegrityError:
                ocd_id_part = organization_ocd_id.rsplit('-', 1)[1]
                org_obj, created = Organization.objects.get_or_create(
                    ocd_id=organization_ocd_id,
                    name=page_json['name'],
                    classification=page_json['classification'],
                    source_url=source_url,
                    slug=slugify(page_json['name']) + ocd_id_part,
                )

        # if created and DEBUG:
        #     print('   adding organization: %s' % org_obj.name )
        if created and DEBUG:
            print('\u263A', end=' ', flush=True)

        for post_json in page_json['posts']:

            try:
                obj = Post.objects.get(ocd_id=post_json['id'])

                obj.label = post_json['label']
                obj.role = post_json['role']
                obj._organization = org_obj
                obj.division_ocd_id = post_json['division_id']

                obj.save()

            except Post.DoesNotExist:
                obj, created = Post.objects.get_or_create(
                    ocd_id=post_json['id'],
                    label=post_json['label'],
                    role=post_json['role'],
                    _organization=org_obj,
                    division_ocd_id=post_json['division_id'],
                )

        for child in page_json['children']:
            self.grab_organization_posts({'id': child['id']}, org_obj)
Beispiel #10
0
    def import_vcf(self, importer, vcf, var):
        """Import the VCF file and list of mutations into database"""
        if 'ENRICHED' not in vcf.metadata:
            raise NotEnrichedError()

        if 'LOCATION' not in vcf.metadata:
            raise DataError('Location is missing from metadata')

        loc = vcf.metadata['LOCATION'][0]
        con = loc.get('COUNTRY', None)
        country = long_match(COUNTRY_MAP, self.countries, con, Country, None,\
            'name', 'detail__name_short', 'detail__name_abbr', 'iso2', 'iso3')

        if country is None:
            raise DataError("Country not found: {}".format(con))

        city = long_match(CITY_MAP, self.places, loc.get('CITY', None),\
            Place, None, 'name', country=country)

        pat = vcf.metadata.get('PATIENT', [{}])[0]
        datum = dict(
            importer=importer,
            country=country,
            city=city,
            patient_id=pat.get('ID', 'None'),
            patient_sex=pat.get('SEX', None),
            patient_age=(pat.get('AGE', None) or None),
            patient_hiv=pat.get('HIV', None),
            # TODO: Patient notes are discarded here, maybe keep them.
        )

        # Drug data must exist!
        drugs = vcf.metadata.get('DRUG', [])
        if not drugs:
            raise DataError("No phenotype drug resistance data.")

        # BIO SAMPLE first.
        name = vcf.metadata.get('SAMPLE_ID', (None, ))[0]
        name = EMPTY.get(name, name)

        other_name = vcf.metadata.get('STRAIN_NAME', (None, ))[0]
        other_name = EMPTY.get(other_name, other_name)

        if name is None:
            if other_name is None:
                raise DataError("No valid STRAIN_NAME or bio SAMPLE_ID")
            name, other_name = other_name, None
        datum['old_id'] = other_name

        if 'LINEAGE' in vcf.metadata:
            datum['lineage'] = Lineage.objects.get_or_create(
                name=vcf.metadata['LINEAGE'][0], slug=name)[0]

        study = None
        if 'STUDY' in vcf.metadata:
            name = vcf.metadata['STUDY'][0].get('NAME', None)
            url = vcf.metadata['STUDY'][0].get('URL', None)
            doi = vcf.metadata['STUDY'][0].get('DOI', None)
            if doi:
                study = Paper.objects.get_or_create(doi=doi,
                                                    defaults={
                                                        'name': name,
                                                        'url': url
                                                    })[0]
            elif name:
                study = Paper.objects.get_or_create(name=name,
                                                    defaults={'url': url})[0]
            if study is not None:
                datum['source_paper'] = study

        if 'PROJECT_ID' in vcf.metadata:
            datum['bioproject'] = BioProject.objects.get_or_create(
                name=vcf.metadata['PROJECT_ID'][0])[0]

        strain, _ = StrainSource.objects.update_or_create(name=name,
                                                          defaults=datum)

        for _drug in vcf.metadata.get('DRUG', []):
            try:
                drug = long_match({
                    'OFLOXACIN': 'OFLX',
                },
                                  self.drugs,
                                  _drug['NAME'],
                                  Drug,
                                  'NOP',
                                  'name',
                                  'code',
                                  _match='icontains')
            except Drug.DoesNotExist:
                raise DataError("Can't import drug: {}".format(_drug['NAME']))
            drug.strains.update_or_create(
                strain=strain,
                defaults=dict(resistance=_drug['STATUS'].lower(),
                              # TODO: More drug testing information here.
                              ))

        strain.generate_resistance_group()

        for snp in var.values():
            #gene = snp['regionid1']
            if 'varname' not in snp or len(snp['varname']) > 80:
                continue
            try:
                (_, locus, mutation) = unpack_mutation_format(snp['varname'])
                # All genes in the gene summary should be already loaded.
                #locus = GeneLocus.objects.get(name=locus, genome=self.genome)
                # Ignore that and ask for a new one
                locus, _ = GeneLocus.objects.update_or_create(
                    name=locus, genome=self.genome)
            except ValueError:
                #raise DataError("Failed to unpack {varname}".format(**snp))
                sys.stderr.write("Failed to unpack {varname}\n".format(**snp))
                continue
            except GeneLocus.DoesNotExist:
                raise DataError("Failed to get gene {varname}, "\
                    "are all genes loaded from reference?".format(**snp))

            if len(mutation) > 150:
                sys.stderr.write(
                    "Mutation name is too large, can not add to database.\n")
                continue

            try:
                locus.mutations.get_or_create(
                    name=mutation,
                    defaults=dict(
                        nucleotide_position=None,
                        nucleotide_reference=None,
                        nucleotide_varient=None,
                        aminoacid_position=None,
                        aminoacid_reference=None,
                        aminoacid_varient=None,
                        codon_position=snp.get('codpos', None),
                        codon_varient=snp.get('altcodon', None),
                        codon_reference=snp.get('codon', None),
                    ))
            except Exception as err:
                sys.stderr.write("Failed to add mutation: {} ({}) {}\n".format(
                    mutation, err, snp))
                continue

        return name