python
import csv
from django.core.management.base import BaseCommand, CommandError
from products.models import Product, Category


class Command(BaseCommand):

Django custom management command for data import

django python cli
by Priya Sharma 1 tab
python
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
from .models import User, Profile

Django signals for decoupled event handling

django python signals
by Priya Sharma 2 tabs
python
from django.db.models import Prefetch
from django.views.generic import ListView
from .models import Post, Comment


class PostListView(ListView):

Django select_related and prefetch_related for N+1 query optimization

django python performance
by Priya Sharma 1 tab
python
from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.

Django REST Framework viewset with custom permissions

django python rest
by Priya Sharma 2 tabs
python
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models


class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):

Django custom user model with email authentication

django python authentication
by Priya Sharma 1 tab
erb
<%= turbo_frame_tag 'quick_view' do %>
  <div class="text-gray-500">Select a post…</div>
<% end %>

<% @posts.each do |post| %>
  <%= link_to post.title, post_path(post), data: { turbo_frame: 'quick_view' }, class: 'block py-1 hover:underline' %>

Frame-powered inline “quick view” that falls back to full page

rails hotwire turbo
by Henry Kim 2 tabs
erb
<%= link_to 'Newest', posts_path(sort: 'new'), data: { turbo_frame: 'results', turbo_action: 'replace' } %>
<%= link_to 'Popular', posts_path(sort: 'popular'), data: { turbo_frame: 'results', turbo_action: 'replace' } %>

Hotwire-friendly “sort by” links that replace only the list

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class ActivityPanelController < ApplicationController
  def show
    @project = Project.find(params[:project_id])
    @events = @project.events.order(created_at: :desc).limit(50)

    latest = @events.maximum(:updated_at)

ETag + last_modified for expensive Turbo Frame endpoints

rails hotwire turbo
by Henry Kim 2 tabs
ruby
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_member

    def connect
      self.current_member = env['warden']&.user || reject_unauthorized_connection

Presence indicator with ActionCable + Turbo Streams

rails hotwire turbo
by Henry Kim 3 tabs
ruby
class NotificationsController < ApplicationController
  def mark_all_read
    current_member.notifications.unread.update_all(read_at: Time.current)

    streams = Turbo::Streams::TagBuilder.new(view_context)

Build Turbo Stream responses in the controller (TagBuilder)

rails hotwire turbo
by Henry Kim 2 tabs
erb
<%= turbo_stream.prepend 'items' do %>
  <%= render @item %>
<% end %>

<turbo-stream action="reset_form" target="new_item_form"></turbo-stream>

Custom Turbo Stream action: reset a form after success

rails hotwire turbo
by Henry Kim 3 tabs
ruby
class SupportMailbox < ApplicationMailbox
  def process
    ticket = Ticket.find_or_create_by!(message_id: mail.message_id) do |t|
      t.subject = mail.subject
      t.from_email = mail.from&.first
      t.body = mail.decoded

Action Mailbox: broadcast incoming emails into a feed

rails hotwire turbo
by Henry Kim 3 tabs