<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Webdev on dev notes</title>
    <link>https://juhanakristianblog.netlify.app/tags/webdev/</link>
    <description>Recent content in Webdev on dev notes</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 05 Sep 2023 09:33:01 +0000</lastBuildDate>
    <atom:link href="https://juhanakristianblog.netlify.app/tags/webdev/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Dynamic aggregation fields in Django</title>
      <link>https://juhanakristianblog.netlify.app/posts/dynamic-aggregation-fields-in-django/</link>
      <pubDate>Tue, 05 Sep 2023 09:33:01 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/dynamic-aggregation-fields-in-django/</guid>
      <description>&lt;p&gt;In this article I will walk you through on how to create dynamic aggregation in Django. By dynamic aggregation I refer to the ability to define the resulting aggregation fields programmatically.&lt;/p&gt;&#xA;&lt;p&gt;Aggregation is a powerful tool when working with data in Django. With it you can have your database summarize or convert data into the format you need.&lt;/p&gt;&#xA;&lt;p&gt;Here&amp;rsquo;s an example of a basic &lt;code&gt;Sum&lt;/code&gt; aggregation.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; django.db &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; models&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; django.db.models &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Sum&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Order&lt;/span&gt;(models&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Model):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    total_price &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; models&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;DecimalField(max_digits&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;, decimal_places&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Calculate the total sum of all order prices&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;total_sum &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Order&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;objects&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aggregate(total&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;Sum(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;total_price&amp;#39;&lt;/span&gt;))[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;total&amp;#39;&lt;/span&gt;]&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Total sum of all order prices: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;total_sum&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We have a &lt;code&gt;Order&lt;/code&gt; model with total price. If we want the total sum in some collection of orders (all orders in the example), we can use &lt;code&gt;aggregate&lt;/code&gt; with &lt;code&gt;Sum&lt;/code&gt; .&lt;/p&gt;</description>
    </item>
    <item>
      <title>Use Model Manager to filter queryset by default</title>
      <link>https://juhanakristianblog.netlify.app/posts/use-model-manager-to-filter-queryset-by-default/</link>
      <pubDate>Tue, 18 Jan 2022 00:00:00 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/use-model-manager-to-filter-queryset-by-default/</guid>
      <description>&lt;p&gt;How can you filter a queryset by default so that no matter where you use the model, the filter will be applied?&lt;/p&gt;&#xA;&lt;p&gt;This can be achieved by creating a custom &lt;a href=&#34;https://docs.djangoproject.com/en/4.0/topics/db/managers/&#34;&gt;Manager&lt;/a&gt; and overriding its &lt;code&gt;get_queryset&lt;/code&gt; method.&lt;/p&gt;&#xA;&lt;p&gt;For example, if we have a &lt;strong&gt;soft delete&lt;/strong&gt; feature in our app implemented by adding a &lt;code&gt;deleted&lt;/code&gt; column, we can force only returing non-deleted objects by creating a custom model manager which filters for &lt;code&gt;deleted=false&lt;/code&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Should you use React.FC to type your components?</title>
      <link>https://juhanakristianblog.netlify.app/posts/should-you-use-reactfc-to-type-your-components/</link>
      <pubDate>Tue, 07 Dec 2021 00:00:00 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/should-you-use-reactfc-to-type-your-components/</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; &lt;a href=&#34;https://github.com/facebook/create-react-app/pull/8177&#34;&gt;No&lt;/a&gt;. &lt;a href=&#34;https://github.com/typescript-cheatsheets/react#function-components&#34;&gt;You&lt;/a&gt; &lt;a href=&#34;https://kentcdodds.com/blog/how-to-write-a-react-component-in-typescript&#34;&gt;should&lt;/a&gt; probably &lt;a href=&#34;https://twitter.com/nickemccurdy/status/1365384372908621833&#34;&gt;not&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-ts&#34; data-lang=&#34;ts&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;Dialog&lt;/span&gt;: &lt;span style=&#34;color:#66d9ef&#34;&gt;React.FC&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ({&lt;span style=&#34;color:#a6e22e&#34;&gt;children&lt;/span&gt;}) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; {&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; (&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;&#x9;...&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#x9;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The consensus in the React community now seems to be that, you should avoid using &lt;code&gt;React.FC&lt;/code&gt; because it causes the component to implicitly take &lt;code&gt;children&lt;/code&gt;. This means the component will accept children, even if they&amp;rsquo;re not supposed to.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s take a look at the pros and cons of &lt;code&gt;React.FC&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;pros-of-reactfc&#34;&gt;Pros of React.FC&lt;/h2&gt;&#xA;&lt;h3 id=&#34;excplicit-return-type&#34;&gt;Excplicit return type&lt;/h3&gt;&#xA;&lt;p&gt;In addition to the &lt;code&gt;children&lt;/code&gt; prop, &lt;code&gt;React.FC&lt;/code&gt; declares an explicit return type for the component. The return type is &lt;code&gt;ReactElement | null;&lt;/code&gt; and we will get a type error if we try to return something else from the component.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Why you should use the URL to store state in your application</title>
      <link>https://juhanakristianblog.netlify.app/posts/why-you-should-use-url-to-store-state-in-your-application/</link>
      <pubDate>Sun, 28 Nov 2021 00:00:00 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/why-you-should-use-url-to-store-state-in-your-application/</guid>
      <description>&lt;p&gt;It seems we have, in the web community, collectively forgotten the role of the URL in storing the state of our application. For good or bad, we&amp;rsquo;ve started developing websites like they are apps and forgotten one of the key elements of the web. I&amp;rsquo;ve seen (and built) applications where opening modals, searching, filtering, or navigating a wizard doesn&amp;rsquo;t change the URL. And this is wrong because it doesn&amp;rsquo;t fit the web paradigm.&lt;/p&gt;</description>
    </item>
    <item>
      <title>What are HTTP cookies</title>
      <link>https://juhanakristianblog.netlify.app/posts/what-are-http-cookies/</link>
      <pubDate>Thu, 28 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/what-are-http-cookies/</guid>
      <description>&lt;p&gt;A HTTP cookie is a small piece of data that a server sends to a user&amp;rsquo;s web browser. The browser may then store the cookie and send it back to the same server with later requests.&lt;/p&gt;&#xA;&lt;p&gt;Cookies are used typically to tell if two HTTP requests come from the same browser/user. So for &lt;strong&gt;session management&lt;/strong&gt;, &lt;strong&gt;tracking&lt;/strong&gt; and to a lesser extent &lt;strong&gt;personalization&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;For general data storage on the client, there are more modern APIs.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
