def update(request): if request.method == 'POST': form = UpdateForm(request.POST) if form.is_valid(): user = request.user user.first_name = form.cleaned_data.get('first_name') user.last_name = form.cleaned_data.get('last_name') user.email = form.cleaned_data.get('email') user.profile.utype = form.cleaned_data.get('utype') user.profile.institution = form.cleaned_data.get('institution') user.profile.position = form.cleaned_data.get('position') user.profile.address = form.cleaned_data.get('address') user.profile.miscellaneous = form.cleaned_data.get('miscellaneous') user.save() review_notification("update", "users", user.pk) return redirect('homepage') elif request.user.is_authenticated: instance = request.user utype = request.user.profile.utype institution = request.user.profile.institution position = request.user.profile.position address = request.user.profile.address miscellaneous = request.user.profile.miscellaneous form = UpdateForm(instance=instance, initial={'utype': utype, 'institution': institution, 'position': position, 'address': address, 'miscellaneous': miscellaneous}) return render(request, 'users/update.html', {'form': form, 'homepage': True})
def PropCreate(request): xvgpath = '' if request.method == 'POST': file_data = {} file_data, xvgpath = FileData(request, 'data_file', 'xvgpath', file_data) form = LI_PropertyForm(request.POST, file_data) if form.is_valid(): prop = form.save(commit=False) prop.curator = request.user indexlist = LI_Property.objects.filter( membranetopol=prop.membranetopol.id, prop=prop.prop).values_list('index', flat=True) index = 1 while index in indexlist: index += 1 prop.index = index proptype = properties_types() prop.search_name = 'LIM{0}_{1}-{2}'.format(prop.membranetopol.id, proptype[prop.prop], index) prop.save() if os.path.isfile('media/' + xvgpath): os.remove('media/' + xvgpath) review_notification('creation', 'properties', prop.pk) return HttpResponseRedirect(reverse('memlist')) else: form = LI_PropertyForm() return render(request, 'properties/prop_form.html', { 'form': form, 'membranes': True, 'propcreate': True, 'xvgpath': xvgpath })
def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save() user.refresh_from_db() # load the profile instance created by the signal user.profile.utype = form.cleaned_data.get('utype') user.profile.institution = form.cleaned_data.get('institution') user.profile.position = form.cleaned_data.get('position') user.profile.address = form.cleaned_data.get('address') user.profile.miscellaneous = form.cleaned_data.get('miscellaneous') user.is_active = False user.save() review_notification("creation", "users", user.pk) current_site = get_current_site(request) email = form.cleaned_data.get('email') subject = 'Activate your LimonadaMD account.' message = render_to_string('users/activation_email.html', {'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user)}) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email, ]) return render(request, 'users/activation_confirm.html', {'homepage': True}) else: form = SignUpForm() return render(request, 'users/signup.html', {'form': form, 'homepage': True})
def RefUpdate(request, pk=None): if Reference.objects.filter(pk=pk).exists(): ref = Reference.objects.get(pk=pk) text = '' for author in ref.authorslist_set.all(): text += '%s %s, ' % (author.author.familly, author.author.given) text = text[0:-2] if request.method == 'POST': form_add = ReferenceForm(request.POST, instance=ref) form_authors = AuthorsForm(request.POST) if form_add.is_valid() and form_authors.is_valid(): ref.refid = form_add.cleaned_data['refid'] ref.title = form_add.cleaned_data['title'] ref.journal = form_add.cleaned_data['journal'] ref.volume = form_add.cleaned_data['volume'] ref.year = form_add.cleaned_data['year'] ref.doi = form_add.cleaned_data['doi'] ref.save() aupos = 0 aulist = [] authors = form_authors.cleaned_data['authors'] for author in authors.split(','): aupos += 1 fullname = author.strip() familly = author.strip().split()[0].strip() given = " ".join(author.strip().split()[1:]).strip() if not Author.objects.filter(fullname=fullname).count(): au = Author.objects.create(fullname=fullname, familly=familly, given=given, curator=request.user) else: au = Author.objects.filter(fullname=fullname)[0] aulist.append( AuthorsList(reference=ref, author=au, position=aupos)) with transaction.atomic(): AuthorsList.objects.filter(reference=ref).delete() AuthorsList.objects.bulk_create(aulist) ref.save() review_notification("update", "references", ref.pk) return HttpResponseRedirect(reverse('reflist')) else: form_authors = AuthorsForm(initial={'authors': text}) form_add = ReferenceForm(instance=ref) return render( request, 'homepage/ref_form.html', { 'form_authors': form_authors, 'form_add': form_add, 'references': True, 'search': False }) else: return HttpResponseRedirect(reverse('reflist'))
def TopCreate(request): itppath = '' gropath = '' if request.method == 'POST': file_data = {} file_data, itppath = FileData(request, 'itp_file', 'itppath', file_data) file_data, gropath = FileData(request, 'gro_file', 'gropath', file_data) form = TopologyForm(request.POST, file_data) if form.is_valid(): top = form.save(commit=False) top.curator = request.user top.save() softwares = form.cleaned_data['software'] for soft in softwares: top.software.add(soft) residues = get_residues(top.gro_file) respos = 0 reslist = [] for resname in residues: respos += 1 if not TopologyResidue.objects.filter(name=resname).count(): res = TopologyResidue.objects.create(name=resname) else: res = TopologyResidue.objects.filter(name=resname)[0] reslist.append( ResidueList(topology=top, residue=res, position=respos)) with transaction.atomic(): ResidueList.objects.filter(topology=top).delete() ResidueList.objects.bulk_create(reslist) refs = form.cleaned_data['reference'] for ref in refs: top.reference.add(ref) top.save() if os.path.isfile('media/' + itppath): os.remove('media/' + itppath) if os.path.isfile('media/' + gropath): os.remove('media/' + gropath) review_notification('creation', 'topologies', top.pk) return HttpResponseRedirect(reverse('toplist')) else: form = TopologyForm() return render( request, 'lipids/top_form.html', { 'form': form, 'topologies': True, 'topcreate': True, 'itppath': itppath, 'gropath': gropath })
def FfUpdate(request, pk=None): if Forcefield.objects.filter(pk=pk).exists(): ff = Forcefield.objects.get(pk=pk) software_init = ff.software.all()[0].name dir_init = 'media/forcefields/%s' % (ff.software.all()[0].name) if ff.curator != request.user: return redirect('homepage') if request.method == 'POST': file_data = {} file_data, ffpath = FileData(request, 'ff_file', 'ffpath', file_data) mdppath = '' file_data, mdppath = FileData(request, 'mdp_file', 'mdppath', file_data) form = ForcefieldForm(request.POST, file_data, instance=ff) if form.is_valid(): ff = form.save(commit=False) ff.software.clear() softwares = form.cleaned_data['software'] for soft in softwares: ff.software.add(soft) ff.reference.clear() refs = form.cleaned_data['reference'] for ref in refs: ff.reference.add(ref) ff.save() if os.path.isfile('media/' + ffpath): os.remove('media/' + ffpath) if mdppath != '' and os.path.isfile('media/' + mdppath): os.remove('media/' + mdppath) if software_init != ff.software.all()[0].name: if os.path.isdir(dir_init): shutil.rmtree(dir_init, ignore_errors=True) review_notification("update", "forcefields", ff.pk) return HttpResponseRedirect(reverse('fflist')) else: ffpath = 'tmp/%s' % os.path.basename(ff.ff_file.name) shutil.copy(ff.ff_file.url[1:], 'media/tmp/') mdppath = '' if ff.mdp_file: mdppath = 'tmp/%s' % os.path.basename(ff.mdp_file.name) shutil.copy(ff.mdp_file.url[1:], 'media/tmp/') form = ForcefieldForm(instance=ff) return render( request, 'forcefields/ff_form.html', { 'form': form, 'forcefields': True, 'ffpath': ffpath, 'mdppath': mdppath })
def PropUpdate(request, pk=None): if LI_Property.objects.filter(pk=pk).exists(): prop = LI_Property.objects.get(pk=pk) mt_init = prop.membranetopol.id prop_init = prop.prop name_init = prop.search_name if prop.curator != request.user: return redirect('homepage') if request.method == 'POST': file_data = {} file_data, xvgpath = FileData(request, 'data_file', 'xvgpath', file_data) form = LI_PropertyForm(request.POST, file_data, instance=prop) if form.is_valid(): prop = form.save(commit=False) if prop.membranetopol.id != mt_init or prop.prop != prop_init: indexlist = LI_Property.objects.filter( membranetopol=prop.membranetopol.id, prop=prop.prop).values_list('index', flat=True) index = 1 while index in indexlist: index += 1 prop.index = index proptype = properties_types() prop.search_name = 'LIM{0}_{1}-{2}'.format( prop.membranetopol.id, proptype[prop.prop], index) if os.path.isfile('media/properties/%s.xvg' % name_init): os.remove('media/properties/%s.xvg' % name_init) prop.save() if os.path.isfile('media/' + xvgpath): os.remove('media/' + xvgpath) review_notification('update', 'properties', prop.pk) return HttpResponseRedirect(reverse('memlist')) else: xvgpath = 'tmp/%s' % os.path.basename(prop.data_file.name) shutil.copy(prop.data_file.url[1:], 'media/tmp/') form = LI_PropertyForm(instance=prop) return render(request, 'properties/prop_form.html', { 'form': form, 'membranes': True, 'xvgpath': xvgpath }) else: return HttpResponseRedirect(reverse('memlist'))
def LipUpdate(request, slug=None): if Lipid.objects.filter(slug=slug).exists(): lipid = Lipid.objects.get(slug=slug) if lipid.curator != request.user: return redirect('homepage') if request.method == 'POST': file_data = {} file_data, imgpath = FileData(request, 'img', 'imgpath', file_data) form_add = LipidForm(request.POST, file_data, instance=lipid) if form_add.is_valid(): lipid = form_add.save(commit=False) name = form_add.cleaned_data['name'] lmid = form_add.cleaned_data['lmid'] com_name = form_add.cleaned_data['com_name'] lipid.search_name = '%s - %s - %s' % (name, lmid, com_name) lipid.slug = slugify('%s' % (lmid), allow_unicode=True) lipid.save() if imgpath != '' and os.path.isfile("media/" + imgpath): os.remove("media/" + imgpath) review_notification("update", "lipids", lipid.slug) return HttpResponseRedirect(reverse('liplist')) else: imgpath = '' if lipid.img: imgpath = 'tmp/%s' % os.path.basename(lipid.img.name) shutil.copy(lipid.img.url[1:], 'media/tmp/') form_add = LipidForm(instance=lipid) return render( request, 'lipids/lip_form.html', { 'form_add': form_add, 'lipids': True, 'search': False, 'imgpath': imgpath }) else: return HttpResponseRedirect(reverse('liplist'))
def FfCreate(request): ffpath = '' mdppath = '' if request.method == 'POST': file_data = {} file_data, ffpath = FileData(request, 'ff_file', 'ffpath', file_data) file_data, mdppath = FileData(request, 'mdp_file', 'mdppath', file_data) form = ForcefieldForm(request.POST, file_data) if form.is_valid(): ff = form.save(commit=False) ff.curator = request.user ff.save() if os.path.isfile('media/' + ffpath): os.remove('media/' + ffpath) if os.path.isfile('media/' + mdppath): os.remove('media/' + mdppath) softwares = form.cleaned_data['software'] for soft in softwares: ff.software.add(soft) refs = form.cleaned_data['reference'] for ref in refs: ff.reference.add(ref) ff.save() review_notification("creation", "forcefields", ff.pk) return HttpResponseRedirect(reverse('fflist')) else: form = ForcefieldForm() return render( request, 'forcefields/ff_form.html', { 'form': form, 'forcefields': True, 'ffcreate': True, 'ffpath': ffpath, 'mdppath': mdppath })
def TopUpdate(request, pk=None): if Topology.objects.filter(pk=pk).exists(): top = Topology.objects.get(pk=pk) version_init = top.version software_init = top.software.all()[0].name forcefield_init = top.forcefield dir_init = 'media/topologies/%s/%s/%s/%s' % (top.software.all( )[0].name, top.forcefield, top.lipid.name, top.version) if top.curator != request.user: return redirect('homepage') if request.method == 'POST': file_data = {} file_data, itppath = FileData(request, 'itp_file', 'itppath', file_data) file_data, gropath = FileData(request, 'gro_file', 'gropath', file_data) form = TopologyForm(request.POST, file_data, instance=top) if form.is_valid(): top = form.save(commit=False) top.software.clear() softwares = form.cleaned_data['software'] for soft in softwares: top.software.add(soft) residues = get_residues(top.gro_file) respos = 0 reslist = [] for resname in residues: respos += 1 if not TopologyResidue.objects.filter( name=resname).count(): res = TopologyResidue.objects.create(name=resname) else: res = TopologyResidue.objects.filter(name=resname)[0] reslist.append( ResidueList(topology=top, residue=res, position=respos)) with transaction.atomic(): ResidueList.objects.filter(topology=top).delete() ResidueList.objects.bulk_create(reslist) top.reference.clear() refs = form.cleaned_data['reference'] for ref in refs: top.reference.add(ref) top.save() if os.path.isfile('media/' + itppath): os.remove('media/' + itppath) if os.path.isfile('media/' + gropath): os.remove('media/' + gropath) if version_init != top.version or software_init != top.software.all( )[0].name or forcefield_init != top.forcefield: if os.path.isdir(dir_init): shutil.rmtree(dir_init, ignore_errors=True) review_notification('update', 'topologies', top.pk) return HttpResponseRedirect(reverse('toplist')) else: itppath = 'tmp/%s' % os.path.basename(top.itp_file.name) shutil.copy(top.itp_file.url[1:], 'media/tmp/') gropath = 'tmp/%s' % os.path.basename(top.gro_file.name) shutil.copy(top.gro_file.url[1:], 'media/tmp/') form = TopologyForm(instance=top) return render( request, 'lipids/top_form.html', { 'form': form, 'topologies': True, 'itppath': itppath, 'gropath': gropath }) else: return HttpResponseRedirect(reverse('toplist'))
def LipCreate(request): imgpath = '' if request.method == 'POST' and 'search' in request.POST: form_search = LmidForm(request.POST) form_add = LipidForm() if form_search.is_valid(): lm_data = {} lm_data['lmid'] = form_search.cleaned_data['lmidsearch'] lm_response = requests.get( 'http://www.lipidmaps.org/rest/compound/lm_id/%s/all/json' % lm_data['lmid']) if 'lm_id' in lm_response.json().keys(): lm_data_raw = lm_response.json() elif 'Row1' in lm_response.json().keys(): lm_data_raw = lm_response.json()['Row1'] for key in [ 'pubchem_cid', 'name', 'sys_name', 'formula', 'abbrev_chains' ]: if key == 'name' and 'name' in lm_data_raw.keys(): lm_data['com_name'] = lm_data_raw[key] elif key in lm_data_raw.keys(): lm_data[key] = lm_data_raw[key] filename = 'media/tmp/%s' % lm_data['lmid'] url = 'http://www.lipidmaps.org/data/LMSDRecord.php?Mode=File&LMID=%s' % lm_data[ 'lmid'] response = requests.get(url) with open('%s.mol' % filename, 'wb') as f: f.write(response.content) f.close() R = molsize(filename) if R < 1: shutil.copy('%s_rot.mol' % filename, '%s.mol' % filename) try: args = shlex.split( 'obabel %s.mol -O %s.png -xC -xh 1200 -xw 1000 -x0 molfile -xd --title' % (filename, filename)) process = subprocess.Popen(args, stdout=subprocess.PIPE) process.communicate() except OSError: pass if os.path.isfile('media/tmp/%s.png' % lm_data['lmid']): f = open('media/tmp/%s.png' % lm_data['lmid'], "rb") file_data = { 'img': SimpleUploadedFile(name=f.name, content=f.read(), content_type="image/png") } imgpath = 'tmp/%s.png' % lm_data['lmid'] else: file_data = {} imgpath = '' if os.path.isfile('%s_rot.mol' % filename): os.remove('%s_rot.mol' % filename) if os.path.isfile('%s.mol' % filename): os.remove('%s.mol' % filename) if 'pubchem_cid' in lm_data.keys(): if lm_data['pubchem_cid'] != '': try: pubchem_response = requests.get( 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/%s/JSON/' % lm_data['pubchem_cid']) pubchem_data_raw = pubchem_response.json()['Record'] if pubchem_data_raw != []: for s1 in pubchem_data_raw['Section']: if s1['TOCHeading'] == 'Names and Identifiers': for s2 in s1['Section']: if s2['TOCHeading'] == 'Computed Descriptors': for s3 in s2['Section']: if s3['TOCHeading'] == 'IUPAC Name': lm_data['iupac_name'] = s3[ 'Information'][0]['Value'][ 'StringWithMarkup'][ 0]['String'] if s2['TOCHeading'] == 'Synonyms': for s3 in s2['Section']: if s3['TOCHeading'] == 'Depositor-Supplied Synonyms': key1 = 'Information' key2 = 'Value' key3 = 'StringWithMarkup' key4 = 'String' nb = len(s3[key1][0][key2] [key3]) for i in range(nb): if len(s3[key1][0] [key2][key3] [nb - 1 - i][key4]) == 4: lm_data['name'] = s3[ key1][0][key2][ key3][ nb - 1 - i][key4] except KeyError: pass form_add = LipidForm(lm_data, file_data) return render( request, 'lipids/lip_form.html', { 'form_search': form_search, 'form_add': form_add, 'lipids': True, 'search': True, 'imgpath': imgpath }) elif request.method == 'POST' and 'add' in request.POST: lmclass, lmdict = lm_class() form_search = LmidForm() file_data = {} file_data, imgpath = FileData(request, 'img', 'imgpath', file_data) form_add = LipidForm(request.POST, file_data) if form_add.is_valid(): lipid = form_add.save(commit=False) name = form_add.cleaned_data['name'] lmid = form_add.cleaned_data['lmid'] com_name = form_add.cleaned_data['com_name'] lipid.search_name = '%s - %s - %s' % (name, lmid, com_name) core = form_add.cleaned_data['core'] main_class = form_add.cleaned_data['main_class'] sub_class = form_add.cleaned_data['sub_class'] l4_class = form_add.cleaned_data['l4_class'] lipid.core = lmdict[core] lipid.main_class = lmdict[main_class] if sub_class != '': lipid.sub_class = lmdict[sub_class] if l4_class != '': lipid.l4_class = lmdict[l4_class] lipid.slug = slugify('%s' % (lmid), allow_unicode=True) lipid.curator = request.user lipid.save() if os.path.isfile("media/" + imgpath): os.remove("media/" + imgpath) review_notification("creation", "lipids", lipid.slug) return HttpResponseRedirect(reverse('liplist')) else: form_search = LmidForm() form_add = LipidForm() return render( request, 'lipids/lip_form.html', { 'form_search': form_search, 'form_add': form_add, 'lipids': True, 'search': True, 'imgpath': imgpath })
def RefCreate(request): if request.method == 'POST' and 'search' in request.POST: form_search = DoiForm(request.POST) form_authors = AuthorsForm() form_add = ReferenceForm() if form_search.is_valid(): doi = form_search.cleaned_data['doisearch'] url = 'https://dx.doi.org/%s' % doi headers = {'Accept': 'application/citeproc+json'} doidata = {} response = requests.get(url, headers=headers) doidata_raw = response.json() if doidata_raw != []: if 'author' in doidata_raw.keys(): text = '' for author in doidata_raw['author']: text += '%s %s, ' % (author['family'], author['given']) doidata['authors'] = text[0:-2] if 'title' in doidata_raw.keys(): doidata['title'] = doidata_raw['title'] if 'container-title' in doidata_raw.keys(): doidata['journal'] = doidata_raw['container-title'] if 'volume' in doidata_raw.keys(): doidata['volume'] = doidata_raw['volume'] if 'DOI' in doidata_raw.keys(): doidata['doi'] = doidata_raw['DOI'] if 'created' in doidata_raw.keys(): if 'date-parts' in doidata_raw['created'].keys(): doidata['year'] = doidata_raw['created']['date-parts'][ 0][0] if 'authors' in doidata.keys() and 'year' in doidata.keys(): doidata['refid'] = '%s%s' % (doidata['authors'].split()[0], doidata['year']) form_add = ReferenceForm(initial=doidata) form_authors = AuthorsForm(initial=doidata) return render( request, 'homepage/ref_form.html', { 'form_search': form_search, 'form_authors': form_authors, 'form_add': form_add, 'references': True, 'search': True }) elif request.method == 'POST' and 'add' in request.POST: form_search = DoiForm() form_authors = AuthorsForm(request.POST) form_add = ReferenceForm(request.POST) if form_add.is_valid() and form_authors.is_valid(): ref = form_add.save(commit=False) ref.curator = request.user ref.save() aupos = 0 aulist = [] authors = form_authors.cleaned_data['authors'] for author in authors.split(','): aupos += 1 fullname = author.strip() familly = author.strip().split()[0].strip() given = " ".join(author.strip().split()[1:]).strip() if not Author.objects.filter(fullname=fullname).count(): au = Author.objects.create(fullname=fullname, familly=familly, given=given, curator=request.user) else: au = Author.objects.filter(fullname=fullname)[0] aulist.append( AuthorsList(reference=ref, author=au, position=aupos)) with transaction.atomic(): AuthorsList.objects.filter(reference=ref).delete() AuthorsList.objects.bulk_create(aulist) ref.save() review_notification("creation", "references", ref.pk) return HttpResponseRedirect(reverse('reflist')) else: form_search = DoiForm() form_authors = AuthorsForm() form_add = ReferenceForm() return render( request, 'homepage/ref_form.html', { 'form_search': form_search, 'form_authors': form_authors, 'form_add': form_add, 'references': True, 'search': True })