๐Ÿ“‹ Coding Standards & Style Guide

Results

๐Ÿ“‹ Q9 Coding Standards & Style Guide

Overview

This guide documents the coding standards and conventions used throughout the Q9 project. Following these standards ensures consistency, maintainability, and a cohesive user experience.

๐Ÿ”— URL Conventions

REST-style URLs

โœ“ DO
/projects/
/projects/123/
/projects/123/workers/
/tasks/
/contacts/

Use Underscores (NOT Hyphens)

โœ“ DO
path('task_create/', views.TaskCreateView.as_view(), name='task_create')
path('contact_list/', views.ContactListView.as_view(), name='contact_list')
โœ— DON'T
path('task-create/', ...)
path('contact-list/', ...)

URL Namespacing

โœ“ DO
# In app's urls.py
app_name = 'events'

# In templates
{% url 'events:event_list' %}
{% url 'events:event_create' %}

๐Ÿ‘๏ธ View Patterns

Class-Based Views for CRUD

โœ“ DO
class ContactListView(LoginRequiredMixin, SingleTableMixin, FilterView):
    model = Contact
    table_class = ContactTable
    filterset_class = ContactFilter
    template_name = 'contacts/contact_list.html'

Always Filter by User

โœ“ DO
def get_queryset(self):
    return Task.objects.filter(user=self.request.user)
โœ— DON'T
def get_queryset(self):
    return Task.objects.all() # Security risk!

๐Ÿ“ Template Organization

Directory Structure

app_name/templates/app_name/
โ”œโ”€โ”€ model_list.html
โ”œโ”€โ”€ model_detail.html
โ”œโ”€โ”€ model_form.html (for create/edit)
โ””โ”€โ”€ partials/
    โ””โ”€โ”€ _model_row.html

Template Naming

โœ“ DO
contacts/contact_list.html
contacts/contact_detail.html
contacts/contact_form.html
contacts/partials/_contact_row.html

๐Ÿ—ƒ๏ธ Model Conventions

Required Fields

โœ“ DO
class Contact(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    # Optional project association
    project = models.ForeignKey(Project, null=True, blank=True)

UUID Primary Keys (When Appropriate)

import uuid

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

๐ŸŽจ UI Components

Retro Windows 95 Style

โœ“ DO
{% retro_window "๐Ÿ“ My Notes" %}
    
{% render_table table %}
{% endretro_window %}

Button Styling

โœ“ DO
{% retro_button "Save" btn_type="primary" %}
{% retro_link_button url "Edit" btn_type="secondary" %}

Emoji Usage

โœ“ DO
{% block body_title %}๐Ÿ“ Notes{% endblock %}
๐Ÿ  Home
{% retro_button "โž• Add New" %}

๐Ÿ“ฑ Responsive Design

Column Classes

โœ“ DO
<div class="retro-col-12 retro-col-md-8">Content</div>
<div class="retro-col-12 retro-col-md-4">Sidebar</div>

Mobile Touch Targets

.retro-btn {
    min-height: 44px; /* Apple's recommendation */
    padding: 10px 16px;
    font-size: 16px; /* Prevents iOS zoom */
}

Table Handling

โœ“ DO
{% render_table table %}
/* CSS */ .retro-table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }

๐ŸŒ™ Dark Mode Support

Forum-Style Light Wrapper Approach

@media (prefers-color-scheme: dark) {
    main {
        background-color: #c0c0c0 !important;
    }
    
    .retro-window-content,
    .retro-table-wrapper {
        background: #ffffff !important;
        color: #000000 !important;
    }
}

Window Titles

.retro-window-title {
    background: #000080 !important;
    color: #ffffff !important;
}

๐Ÿงช Testing Standards

Test Setup

โœ“ DO
# Set environment variable before running tests
export CELERY_BROKER_URL=redis://localhost:6379/0
python manage.py test --settings=q9.settings.test

Test User Creation

โœ“ DO
from django.contrib.auth import get_user_model
from allauth.account.models import EmailAddress

User = get_user_model()
self.user = User.objects.create_user(
    username='testuser',
    email='test@example.com',
    password='testpass123'
)
# Create verified email for allauth
EmailAddress.objects.create(
    user=self.user,
    email=self.user.email,
    verified=True,
    primary=True
)

๐Ÿ”’ Security Practices

Row-Level Security

โœ“ DO
# Always filter by user
queryset = Model.objects.filter(user=request.user)

Form Security

โœ“ DO
def form_valid(self, form):
    form.instance.user = self.request.user
    return super().form_valid(form)

Never Expose Secrets

โœ— DON'T
SECRET_KEY = 'hardcoded-secret-key'
API_KEY = 'my-api-key-12345'
โœ“ DO
SECRET_KEY = env('SECRET_KEY')
API_KEY = env('API_KEY')

๐Ÿ“š Additional Resources