<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Praduman Goyal on Medium]]></title>
        <description><![CDATA[Stories by Praduman Goyal on Medium]]></description>
        <link>https://medium.com/@goyal.praduman611?source=rss-a19269e76659------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kEvCAFqlQWObiIdeAmuOfg.jpeg</url>
            <title>Stories by Praduman Goyal on Medium</title>
            <link>https://medium.com/@goyal.praduman611?source=rss-a19269e76659------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 08:08:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@goyal.praduman611/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Django — A Story of Race conditions with get_or_create and unique constraints]]></title>
            <link>https://medium.com/newton-school/django-a-story-of-race-conditions-with-get-or-create-and-unique-constraints-39b04aa12a54?source=rss-a19269e76659------2</link>
            <guid isPermaLink="false">https://medium.com/p/39b04aa12a54</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Praduman Goyal]]></dc:creator>
            <pubDate>Wed, 30 Nov 2022 15:41:04 GMT</pubDate>
            <atom:updated>2022-11-30T15:41:04.992Z</atom:updated>
            <content:encoded><![CDATA[<h3>Django — A Story of Race conditions with get_or_create and unique constraints</h3><blockquote><strong><em>Don’t repeat yourself.</em></strong><em> Ever. Ever. Oh, wait, my bad. Sorry.</em></blockquote><p>Here, I spilled out one of the 101 secret potions we make everyone drink before joining our team! It simply means if you are writing something similar twice, there must be a better way to do that.</p><p>Recently while we were writing our Document collection service, under the effects of the above potion. The aim was to assemble a more generic document collection service to collect all kinds of user documents. I will talk about how we, our code and then the sentry got into a weird dilemma during this time and how we solved that.</p><p>A critical table in any such service will be a Document table with key fields, namely: user , document_type (AADHAAR_CARD/PAN_CARD/SALARY_SLIP) , document. At first, adding a unique_together constraint for the fields users , and document_type seems logical, keeping in mind that one user should possess only one Aadhaar or PAN Card. But it was not one of the best decisions by us given we will be saving Salary slips, Tax Returns in the same table, which one user can possess in multiple quantities. Confidently we put a validation on the save method of our Django Model like this and went on to sleep peacefully.</p><pre>def save(self):<br>  if not self.document_type not in [&#39;AADHAAR_CARD&#39;, &#39;PAN_CARD&#39;]:<br>    if Document.objects.filter(<br>          user=self.user, <br>          document_type=self.document_type<br>       ).exclude(id=self.id).exists():<br>      raise ValidationError(&quot;Not allowed&quot;)<br>  super().save()</pre><p>Getting an aadhaar document of a user is a seemingly easy task ahead with something like the following:</p><pre>user_aadhaar_card = None<br>try:<br>  user_aadhaar_card = Document.objects.get(<br>        user=request.user, <br>        document_type=&quot;AADHAAR_CARD&quot;<br>)<br>except UserDocumentMapping.DoesNotExist:<br>  user_aadhaar_card = Document.objects.create(<br>        user=request.user, <br>        document_type=AADHAAR_CARD<br>)</pre><p>If you ever find yourself writing this, do remember excellent util <a href="https://docs.djangoproject.com/en/4.1/ref/models/querysets/#get-or-create">get_or_create</a> provided by Django, which does a bit more necessity that we generally ignore. Let’s see what get_or_create should be doing under the hood:</p><pre>try:<br>  return some_model.get(**queryset), False<br>except some_model.DoesNotExist:<br>  try:<br>    instance = some_model.create(**queryset)<br>    return instance, True<br>  except IntegrityError:<br>    return some_model.get(**queryset), False</pre><p>This might seem weird repeating the same logic in the except clause, but this comes into the picture when we are working with concurrent requests. Let us try to understand this pictorially.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-aXqK4i2zOUTjb93Sfdskg.jpeg" /><figcaption>The execution of the above-mentioned functions for concurrent requests</figcaption></figure><p>So, assuming the required unique constraints in their place, your implementation will throw an integrity error, but Django’s get_or_create will catch that and provide a neat response for you.</p><p>But as we could not put any unique_contraint above, in our case, it will happily create 2 Addhaar Cards for a single user (UIDAI, code red!) and the next time when I confidently run get(user=user, document_type=&quot;AADHAAR_CARD&quot;) relying on my poor clean function and Django’s get_or_create it will return me never expected Multiple objects. FML!</p><p>Django 2.2 to the rescue; introduced UniqueConstraint , which allows you to apply unique constraints conditionally to your models. In code, something similar to:</p><pre>class Document(models.Model)<br>  user = &lt;&gt;<br>  document_type = &lt;&gt;<br>  document = &lt;&gt;<br><br>  class Meta:<br>      constraints = [<br>        models.UniqueConstraint(<br>          fields=[&#39;user&#39;, &#39;document_type&#39;], <br>          condition=Q(<br>            document_type__in=[&quot;AADHAAR_CARD&quot;, &quot;PAN_CARD&quot;]<br>          ),<br>          name=&#39;unique_user_unique_document_type&#39;<br>        )<br>      ]</pre><p>And then we live happily. No more sentry alerts, no more users with multiple Aadhaar cards! Phew!</p><p>Coming to an end, the moral of the story is to —</p><blockquote>Never ever use get_or_create or similar logic with a lookup which is not “unique-constrained” on the database level!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39b04aa12a54" width="1" height="1" alt=""><hr><p><a href="https://medium.com/newton-school/django-a-story-of-race-conditions-with-get-or-create-and-unique-constraints-39b04aa12a54">Django — A Story of Race conditions with get_or_create and unique constraints</a> was originally published in <a href="https://medium.com/newton-school">Newton School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GSoC’19 with Ensembl — Final submission]]></title>
            <link>https://medium.com/@goyal.praduman611/gsoc19-with-ensembl-final-submission-60390448a2b2?source=rss-a19269e76659------2</link>
            <guid isPermaLink="false">https://medium.com/p/60390448a2b2</guid>
            <category><![CDATA[gsoc]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[genome]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[bioinformatics]]></category>
            <dc:creator><![CDATA[Praduman Goyal]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 06:09:42 GMT</pubDate>
            <atom:updated>2019-08-22T06:09:42.424Z</atom:updated>
            <content:encoded><![CDATA[<h3>GSoC’19 with Ensembl — Final submission</h3><p>It’s been three months I ‘ve been working with <a href="http://ensembl.org/">Ensembl</a>, an open-source organisation selected for <a href="https://summerofcode.withgoogle.com/">Google Summer of Code</a> program. It was an amazing experience working with my mentors <a href="https://www.ebi.ac.uk/about/people/fergal-martin">Fergal Martin</a> &amp; <a href="https://www.ebi.ac.uk/about/people/osagie-izuogu">Osagie Izuogu</a>. I have learned a lot under their mentorship.</p><p>My project is titled as <strong>ecircdb, </strong>an analytics dashboard for circular RNA integrated with an in house circRNA catalogue. I have submitted approximately <strong>130 commits, 25000+ additions </strong>on this project. Working on this project I completed my <strong>1000 GitHub contributions </strong>for a year in the last phase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*uUHQVcjGcVpmrGLnyHns4Q.png" /></figure><h3>Project overview</h3><p>The project ecircdb is the first-ever developed prototype for a circular RNA analytics dashboard. The project is integrated into an in-house catalogue of circRNAs identified from multiple species with the following functionalities which were on uttermost priority for such kind of prototype.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/0*lQhP90XgrkWRTJkB" /><figcaption>ecircdb logo</figcaption></figure><ol><li><em>Species view:</em> The view contains the highlights, plots and the export list(based on tissues, min. NMethods, min TPM etc) options based on the selected assembly of a species</li><li><em>Sample list &amp; Sample view:</em> On selecting a sample from the list of samples from an assembly, the view shows statistics, plot, quality report and export list option for the selected sample.</li><li><em>Location view:</em> The view integrates the Genoverse with ecircdb. The genome browser shows the track for circRNAs for the given assembly with the gene track. The view allows selecting from top-X chromosomes based on circRNAs and selecting coordinates for all the circRNA producing genes.</li></ol><p>Talking about the tech-stack of the project, the flow of the project is very simple and easy-to-understand. The computational part is mainly in python and served on the API endpoints using Django(Python). The served data is consumed on frontend by a ReactJS(JavaScript) app and the plots are shown using PlotlyJS.</p><h3>Links to my work</h3><ol><li>Documentation — <a href="https://github.com/EnsemblGSOC/ecircdb-backend/wiki">https://github.com/EnsemblGSOC/ecircdb-backend/wiki</a></li><li>Backend repository —<a href="https://github.com/EnsemblGSOC/ecircdb-backend/wiki">https://github.com/EnsemblGSOC/ecircdb-backend</a></li><li>Frontend repository — <a href="https://github.com/EnsemblGSOC/ecircdb-backend/wiki">https://github.com/EnsemblGSOC/ecircdb-frontend</a></li><li>Design mock-ups — <a href="https://www.figma.com/file/cYST0BAkOG9xaglAi2UF2nq8/GSoC-Ensembl-ecircdb?node-id=0%3A1">https://www.figma.com/file/cYST0BAkOG9xaglAi2UF2nq8/GSoC-Ensembl-ecircdb?node-id=0%3A1</a></li></ol><h3>Status of the project</h3><p>As mentioned earlier this project is the first try to make a prototype for circRNA, so it can be extended in various directions. There are various features that can be extended and were discussed during the GSoC period like Tissue view and comparing across different samples. Except these, the project contains various views and plots that must have been included in the prototype as discussed with mentors.</p><h3>Not ‘the end’</h3><p>I had like to thank my mentors for the constant support and motivation. Had they not been, the project wouldn’t have been completed in the first place.</p><p>It has been a thrilling summer overall. I would love to remain associated with open-source after this program too. GSoC is <strong>not the end</strong>.</p><p>Not to forget, Thanks to Google for organising such an awesome program.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60390448a2b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I integrated Google reCAPTCHA with my React+Django-based portfolio]]></title>
            <link>https://medium.com/img-iit-roorkee/how-i-integrated-google-recaptcha-with-my-react-django-based-portfolio-8fe93cf52b9d?source=rss-a19269e76659------2</link>
            <guid isPermaLink="false">https://medium.com/p/8fe93cf52b9d</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[portfolio]]></category>
            <dc:creator><![CDATA[Praduman Goyal]]></dc:creator>
            <pubDate>Sun, 09 Dec 2018 17:49:18 GMT</pubDate>
            <atom:updated>2018-12-10T06:12:55.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HjkKSsJ6XKcfQ0IEq5QOBQ.png" /></figure><p>JavaScript and the browser console have already provided a lot of power to the client side of a webpage. Anyone with a little knowledge of JavaScript can flood your database with fake entries if it’s just an information collecting form, leaving you with nothing but a ton of validated data, nearly impossible to filter out. Generally, it is a lot of fun for the person running scripts and it provides him with a boundless source of happiness so ethics can be ruled out as a guard for your database. Even I try to run JavaScript scripts wherever possible 😜</p><p>I encountered the same problem when I tried to render a Contact Me form in my portfolio as shown in the following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*-1LGthWNZQLtUkqUGUhcMg.png" /><figcaption>My ContactMe form without Google reCAPTCHA :(</figcaption></figure><p>So, it’s really easy to run a script here with validated data and ruin my database. That’s where reCAPTCHA comes into the picture. reCAPTCHA is a free service with AI support from Google so it is pretty obvious that it can protect your website from these spam requests. So following are the steps you can follow to integrate one in your app too.</p><ol><li><strong><em>Get Keys</em></strong><em><br></em>Visit <a href="https://www.google.com/recaptcha/admin">My reCAPTCHA</a> where, after signing in, you get an option to register a new site.<br>i. <em>Label</em>: Label is just an identifier to identify your key for future reference because it gives you the option to have multiple keys for different websites.<br>ii. <em>reCAPTCHAv2</em>: I go for reCAPTCHAv2 because I found a Google-reCAPTCHA v2 npm package to integrate with my React app<br>After providing the domain you want to run your app on (you can also put localhost) and registering, the admin dashboard provides you with two keys.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*GvXPjKIueCcPTNSp" /><figcaption>Be sure to keep your Secret key secret :P</figcaption></figure><p>2. <strong><em>Frontend</em></strong><em><br></em>For the frontend, I found <a href="https://www.npmjs.com/package/react-google-recaptcha">react-google-recaptcha</a>, the package most suitable for my use case.</p><p>i. Add this package to your project.</p><pre>$ yarn add react-google-recaptcha</pre><p>ii. Use the component in the JS file.</p><p>Component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfdec00d9e1262933bf02e459aa72ff8/href">https://medium.com/media/bfdec00d9e1262933bf02e459aa72ff8/href</a></iframe><p>Function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b7d9a466132fa8e5ff98eda1cbbb599/href">https://medium.com/media/9b7d9a466132fa8e5ff98eda1cbbb599/href</a></iframe><p>Here, key is the key provided by the captcha you need to verify on the backend.</p><p>Submit Function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfdbe01636f4056032a4dd1e5700b5c2/href">https://medium.com/media/bfdbe01636f4056032a4dd1e5700b5c2/href</a></iframe><p>You can check out more props to pass to <a href="https://www.npmjs.com/package/react-google-recaptcha">react-google-recaptcha</a> (or <a href="https://medium.com/codeep-io/how-to-use-google-recaptcha-with-react-38a5cd535e0d">here</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*f-Vo043YaRbkTQY5JvaASw.png" /><figcaption>My Contact Me form with Google ReCAPTCHA :)</figcaption></figure><p>3.<em> </em><strong><em>Backend</em></strong></p><p>Now, let us go to the server with the data we just posted. The backend doesn’t consist of anything other than making a post request to Google with the key obtained from frontend, Your secret site key, and optionally the user’s IP address. I have used the <a href="https://pypi.org/project/requests/">requests</a> PyPI package for the purpose.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/361d837b18b5114e9fb80eae7f45988b/href">https://medium.com/media/361d837b18b5114e9fb80eae7f45988b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66b0261998d00c3d76233b21f637de62/href">https://medium.com/media/66b0261998d00c3d76233b21f637de62/href</a></iframe><p>The code snippet is a part of the view which is written in Django and Django REST Framework. You may have a different backend stack but the idea remains the same, i.e. to make a POST request to <a href="https://www.google.com/recaptcha/api/siteverify">https://www.google.com/recaptcha/api/siteverify</a>, with these parameters. We have ignored the other parameters in Google’s response, you can check them out <a href="https://developers.google.com/recaptcha/docs/verify#api-request">here</a>.</p><p>Hopefully, this article will help you filter out spam responses in your forms.</p><p>Happy Rendering!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8fe93cf52b9d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/img-iit-roorkee/how-i-integrated-google-recaptcha-with-my-react-django-based-portfolio-8fe93cf52b9d">How I integrated Google reCAPTCHA with my React+Django-based portfolio</a> was originally published in <a href="https://medium.com/img-iit-roorkee">IMG IIT Roorkee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>