コード例 #1
0
def send_verification_email(request):
    send_email(
        request.user.email, 'Eco-AlpsWater verification e-mail',
        'This e-mail has been automatically sent from the Eco-AlpsWater website.'
    )
    return HttpResponse(json.dumps({'success': True}),
                        content_type="application/json")
コード例 #2
0
def request_sequence(request):
    samples = json.loads(request.POST['samples'])
    tot_files = 0
    for sample_id in samples:
        sample = Sample.objects.get(id=sample_id)
        tot_files += sample.sequence_set.all().count()
    if tot_files == 0:
        raise Exception('There are no files associated')
    dirname = str(uuid.uuid1().int >> 64)
    os.mkdir(os.path.join(os.environ['EAW_DOWNLOAD_DIRECTORY'], dirname))
    outdir = os.environ['EAW_DOWNLOAD_DIRECTORY']
    indir = os.environ['EAW_VAULT_DIRECTORY']
    login = '******'
    location = 'https://eco-alpswater.fmach.it/download/{dirname}/'.format(
        dirname=dirname)
    for sample_id in samples:
        sample = Sample.objects.get(id=sample_id)
        for seq in sample.sequence_set.all():
            copyfile(os.path.join(indir, seq.filename),
                     os.path.join(outdir, dirname, seq.filename))
    send_email(
        request.user.email, 'Eco-AlpsWater sequence file(s) ready', '''
        Dear {user},
        the sequence file(s) you requested are ready to be downloaded from the Eco-AlpsWater server using your credentials.
        Please note that all downloadable files get removed every day at midnight.
        
        You can user your browser to download files from {location}.
        
        If you prefer to use WGET you'll need to first login and save the cookie with your credentials
        
        wget -qO- --no-check-certificate --keep-session-cookies --save-cookies cookies.txt --post-data 'username={user}&password=<password>' {login}
        
        And then download files passing the cookie file
        
        wget -c -r --no-check-certificate --load-cookies cookies.txt {location}

        This e-mail has been automatically sent from the Eco-AlpsWater website.
               '''.format(user=request.user.username,
                          location=location,
                          login=login))
    return HttpResponse(json.dumps({'success': True}),
                        content_type="application/json")
コード例 #3
0
def get_sequence(request, id, file=None):
    download_dir = os.path.join(os.environ['EAW_DOWNLOAD_DIRECTORY'], id)
    status_code = 401
    for (dirpath, dirnames, filenames) in os.walk(download_dir):
        for file in filenames:
            seq = Sequence.objects.filter(filename=file).first()
            if seq and (seq.sample.user.username == request.user.username
                        or request.user.is_superuser):
                status_code = 200
                try:
                    user = request.user.username
                    title = 'Eco-AlpsWater admin notification'
                    message = '''
                        Dear admin,
                        user {user} has just downloaded sequence files from directory {dir}.
                        '''.format(user=user, dir=download_dir)
                    for admin in User.objects.filter(is_superuser=True):
                        send_email(admin.email, title, message)
                except Exception as e:
                    pass
                break
    return HttpResponse(json.dumps({}), status=status_code)
コード例 #4
0
def update_sample(request):
    sample = Sample.objects.get(id=request.POST.get('id', None))
    sample.mean_river_outflow = request.POST.get('mean_river_outflow',
                                                 None) or None
    sample.water_renewal_time = request.POST.get('water_renewal_time',
                                                 None) or None
    sample.mixing_type_id = request.POST.get('mixing_type', None) or None
    sample.catchment_area = request.POST.get('catchment_area', None) or None
    sample.temperature = request.POST.get('temperature', None) or None
    sample.field_ph = request.POST.get('field_ph', None) or None
    sample.field_conductivity = request.POST.get('field_conductivity',
                                                 None) or None
    sample.light_attenuation_coefficient = request.POST.get(
        'light_attenuation_coefficient', None) or None
    sample.secchi_disk_depth = request.POST.get('secchi_disk_depth',
                                                None) or None
    sample.euphotic_layer = request.POST.get('euphotic_layer', None) or None
    sample.oxygen_concentration = request.POST.get('oxygen_concentration',
                                                   None) or None
    sample.oxygen_percentage = request.POST.get('oxygen_percentage',
                                                None) or None
    sample.laboratory_ph = request.POST.get('laboratory_ph', None) or None
    sample.laboratory_conductivity = request.POST.get(
        'laboratory_conductivity', None) or None
    sample.total_alkalinity = request.POST.get('total_alkalinity',
                                               None) or None
    sample.bicarbonates = request.POST.get('bicarbonates', None) or None
    sample.nitrate_nitrogen = request.POST.get('nitrate_nitrogen',
                                               None) or None
    sample.sulphates = request.POST.get('sulphates', None) or None
    sample.chloride = request.POST.get('chloride', None) or None
    sample.calcium = request.POST.get('calcium', None) or None
    sample.magnesium = request.POST.get('magnesium', None) or None
    sample.sodium = request.POST.get('sodium', None) or None
    sample.potassium = request.POST.get('potassium', None) or None
    sample.ammonium = request.POST.get('ammonium', None) or None
    sample.total_nitrogen = request.POST.get('total_nitrogen', None) or None
    sample.soluble_reactive_phosphorus = request.POST.get(
        'soluble_reactive_phosphorus', None) or None
    sample.total_phosphorus = request.POST.get('total_phosphorus',
                                               None) or None
    sample.reactive_silica = request.POST.get('reactive_silica', None) or None
    sample.dry_weight = request.POST.get('dry_weight', None) or None
    sample.chlorophyll_a = request.POST.get('chlorophyll_a', None) or None
    sample.dna_extraction_kit_id = request.POST.get('dna_extraction_kit',
                                                    None) or None
    sample.dna_extraction_date = request.POST.get('dna_extraction_date',
                                                  None) or None
    sample.dna_quantity = request.POST.get('dna_quantity', None) or None
    sample.dna_quantification_method_id = request.POST.get(
        'dna_quantification_method', None) or None
    sample.dna_quality_a260_280 = request.POST.get('dna_quality_a260_280',
                                                   None) or None
    sample.dna_quality_a260_230 = request.POST.get('dna_quality_a260_230',
                                                   None) or None

    if sample.dna_extraction_date:
        dt = datetime.datetime.strptime(sample.dna_extraction_date, '%m/%d/%Y')
        sample.dna_extraction_date = datetime.datetime(dt.year,
                                                       dt.month,
                                                       dt.day,
                                                       tzinfo=pytz.UTC)

    full_dir = os.path.join('/tmp/', sample.sample_code)
    files = [
        'vertical_temperature_profiles', 'phytoplankton_countings',
        'cyanotoxin_samples'
    ]
    for f in files:
        full_path = os.path.join(full_dir, f + '.xlsx')
        if os.path.exists(full_path):
            with open(full_path, 'rb') as fi:
                setattr(sample, f, fi.read())
            os.remove(full_path)
    sample.save()
    for k, v in request.POST.items():
        if k.endswith('_comment') and v:
            field_name = k.replace('_comment', '')
            value = v
            comment = Comment.objects.filter(
                Q(sample=sample) & Q(field_name=field_name)).first()
            if not comment:
                comment = Comment(sample=sample, field_name=field_name)
            comment.comment = value
            comment.save()
    barcode_buffer = __create_barcode_file([sample.id])
    excel_buffer = __create_env_metadata([sample.id])
    send_email(
        request.user.email,
        'Eco-AlpsWater sample updated: ' + sample.sample_code, '''
       Dear {user},
       sample with code {sample_code} has just been successfully updated to the Eco-AlpsWater database.
       Please find attached to this e-mail a PNG file with the sample barcode and an Excel file with all the sample information.
       Both files are ZIP-compressed.
       
       This e-mail has been automatically sent from the Eco-AlpsWater website.
               '''.format(user=request.user.username,
                          sample_code=sample.sample_code),
        [(sample.sample_code + '_barcode.zip', barcode_buffer.getvalue(),
          'application/zip'),
         (sample.sample_code + '_sample.zip', excel_buffer.getvalue(),
          'application/zip')])
    return HttpResponse(json.dumps({'success': True}),
                        content_type="application/json")
コード例 #5
0
def upload_complete(request):
    sample = Sample.objects.get(id=request.POST.get('sample_id', None))
    be = BiologicalElement.objects.get(
        id=request.POST.get('biological_element', None))
    edna = EDNAMarker.objects.get(id=request.POST.get('edna_marker', None))
    hash_md5 = hashlib.md5()
    with open(request.POST['sequence_file.path'], "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    md5 = hash_md5.hexdigest()
    file_md5 = False
    with open(request.POST['md5_checksum_file.path']) as f:
        for l in f:
            if l.startswith('#'):
                continue
            for s in l.strip().split():
                if s == md5:
                    file_md5 = True
                    break
    if not file_md5:
        raise Exception(
            'MD5SUM mismatch, sequence file corrupted or wrong MD5')
    file_unique_id = uuid.uuid4()
    new_file_name = '{sample_code}_{be}_{edna}_{file_unique_id}_{org_name}'.format(
        sample_code=sample.sample_code,
        be=be.code,
        edna=edna.name,
        file_unique_id=file_unique_id,
        org_name=request.POST['sequence_file.name'])
    # move file
    full_name = os.path.join(os.environ['EAW_VAULT_DIRECTORY'], new_file_name)
    shutil.move(request.POST['sequence_file.path'], full_name)
    os.chmod(full_name, S_IREAD | S_IRGRP | S_IROTH)
    # add entry in db
    Sequence.objects.create(
        sample=sample,
        filename=new_file_name,
        original_filename=request.POST['sequence_file.name'],
        md5sum=md5)
    try:
        user = request.user.username
        title = 'Eco-AlpsWater admin notification'
        message = '''
            Dear admin,
            user {user} has just uploaded the sequence file {file} for sample {sample}.
            '''.format(user=user,
                       file=request.POST['sequence_file.name'],
                       sample=sample.sample_code)
        for admin in User.objects.filter(is_superuser=True):
            send_email(admin.email, title, message)
        send_email(
            request.user.email, 'Eco-AlpsWater sequence uploaded', '''
           Dear {user},
           sequence file named {file} has been successfully uploaded for sample with code {sample_code}.

           This e-mail has been automatically sent from the Eco-AlpsWater website.
                   '''.format(user=request.user.username,
                              file=request.POST['sequence_file.name'],
                              sample_code=sample.sample_code))
    except Exception as e:
        pass
    return HttpResponse(json.dumps({'success': True}),
                        content_type="application/json")
コード例 #6
0
 def wrapped_f(*args):
     request = args[0]
     title = 'Eco-AlpsWater admin notification'
     message = ''
     if self.operation == 'update_sample':
         user = request.user.username
         sample = Sample.objects.get(id=request.POST.get('id', None))
         message = '''
         Dear admin,
         user {user} has just performed an update operation on sample {sample_code}.
         '''.format(user=user, sample_code=sample.sample_code)
     elif self.operation == 'save_sample':
         user = request.user.username
         sample_code = request.POST.get('sample_code', None)
         message = '''
         Dear admin,
         user {user} has just performed a create new sample operation with code {sample_code}.
         '''.format(user=user, sample_code=sample_code)
     elif self.operation == 'request_sequence':
         user = request.user.username
         samples = json.loads(request.POST['samples'])
         message = '''
         Dear admin,
         user {user} has just request to download sequence files for samples {samples}.
         '''.format(user=user,
                    samples=', '.join([
                        Sample.objects.get(id=sample_id).sample_code
                        for sample_id in samples
                    ]))
     elif self.operation == 'get_env_metadata':
         user = request.user.username
         samples = json.loads(request.POST['samples'])
         message = '''
         Dear admin,
         user {user} has just request to download environmental metadata for samples {samples}.
         '''.format(user=user,
                    samples=', '.join([
                        Sample.objects.get(id=sample_id).sample_code
                        for sample_id in samples
                    ]))
     elif self.operation == 'get_barcode':
         user = request.user.username
         samples = json.loads(request.POST['samples'])
         message = '''
         Dear admin,
         user {user} has just request to download barcode for samples {samples}.
         '''.format(user=user,
                    samples=', '.join([
                        Sample.objects.get(id=sample_id).sample_code
                        for sample_id in samples
                    ]))
     elif self.operation == 'save_station':
         user = request.user.username
         values = json.loads(request.POST['values'])
         message = '''
         Dear admin,
         user {user} has just request to create a new station with name {station}.
         '''.format(user=user, station=values['station'])
     elif self.operation == 'ftp_file_received':
         ftp_obj = request
         user = ftp_obj.username
         file = args[1]
         message = '''
         Dear admin,
         user {user} has just sent a file named {file_name} to the FTP server.
         '''.format(user=user, file_name=os.path.basename(file))
     elif self.operation == 'add_tracking_comment':
         user = request.user.username
         params = json.loads(request.POST.get('params', None))
         sample = Sample.objects.get(id=params['id'])
         message = '''
         Dear admin,
         user {user} has just added a tracking comment for sample {sample}. The comment is: {comment}.
         '''.format(user=user,
                    sample=sample.sample_code,
                    comment=params['comment'])
     for admin in User.objects.filter(is_superuser=True):
         try:
             send_email(admin.email, title, message)
         except Exception as e:
             pass
     return f(*args)