Skip to content

Commit 2e403ee

Browse files
committed
📝(dev) add cursor rule for django code
We apply a cursor rule to the project related to the django application. This rule is heavily inspired from the posthog's rule.
1 parent 7e1eed3 commit 2e403ee

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

.cursor/rules/django-python.mdc

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
description:
3+
globs: src/backend/**/*.py
4+
alwaysApply: false
5+
---
6+
---
7+
description: Rules for writing Python with Django
8+
globs: src/backend/*.py
9+
---
10+
11+
You are an expert in Python, Django, and scalable web application development.
12+
13+
Key Principles
14+
- Write clear, technical responses with precise Django examples.
15+
- Use Django's built-in features and tools wherever possible to leverage its full capabilities.
16+
- Prioritize readability and maintainability; follow Django's coding style guide (PEP 8 compliance for the most part, with the one exception being 100 characters per line instead of 79).
17+
- Use descriptive variable and function names; adhere to naming conventions (e.g., lowercase with underscores for functions and variables).
18+
19+
Django/Python
20+
- Use Django REST Framework viewsets for API endpoints.
21+
- Leverage Django’s ORM for database interactions; avoid raw SQL queries unless necessary for performance.
22+
- Use Django’s built-in user model and authentication framework for user management.
23+
- Follow the MVT (Model-View-Template) pattern strictly for clear separation of concerns.
24+
- Use middleware judiciously to handle cross-cutting concerns like authentication, logging, and caching.
25+
26+
Error Handling and Validation
27+
- Implement error handling at the view level and use Django's built-in error handling mechanisms.
28+
- Prefer try-except blocks for handling exceptions in business logic and views.
29+
30+
Dependencies
31+
- Django
32+
- Django REST Framework (for API development)
33+
- Celery (for background tasks)
34+
- Redis (for caching and task queues)
35+
- PostgreSQL (preferred databases for production)
36+
- Minio (file storage for production)
37+
- OIDC prodiver (for managing authentication)
38+
39+
Django-Specific Guidelines
40+
- Use Django templates for rendering HTML and DRF serializers for JSON responses.
41+
- Keep business logic in models and forms; keep views light and focused on request handling.
42+
- Use Django's URL dispatcher (urls.py) to define clear and RESTful URL patterns.
43+
- Apply Django's security best practices (e.g., CSRF protection, SQL injection protection, XSS prevention).
44+
- Use Django’s built-in tools for testing (pytest-django) to ensure code quality and reliability.
45+
- Leverage Django’s caching framework to optimize performance for frequently accessed data.
46+
- Use Django’s middleware for common tasks such as authentication, logging, and security.
47+
48+
Performance Optimization
49+
- Optimize query performance using Django ORM's select_related and prefetch_related for related object fetching.
50+
- Use Django’s cache framework with backend support (e.g., Redis or Memcached) to reduce database load.
51+
- Implement database indexing and query optimization techniques for better performance.
52+
- Use asynchronous views and background tasks (via Celery) for I/O-bound or long-running operations.
53+
- Optimize static file handling with Django’s static file management system (e.g., WhiteNoise).
54+
55+
Logging
56+
- As a general rule, we should have logs for every expected and unexpected actions of the application, using the appropriate log level.
57+
- We should also be logging these exceptions to Sentry with the Sentry Python SDK. Python exceptions should almost always be captured automatically without extra instrumentation, but custom ones (such as failed requests to external services, query errors, or Celery task failures) can be tracked using capture_exception().
58+
59+
Log Levels
60+
- A log level or log severity is a piece of information telling how important a given log message is:
61+
- DEBUG: should be used for information that may be needed for diagnosing issues and troubleshooting or when running application in the test environment for the purpose of making sure everything is running correctly
62+
- INFO: should be used as standard log level, indicating that something happened
63+
- WARN: should be used when something unexpected happened but the code can continue the work
64+
- ERROR: should be used when the application hits an issue preventing one or more functionalities from properly functioning
65+
66+
Security
67+
- Don’t log sensitive information. Make sure you never log:
68+
- authorization tokens
69+
- passwords
70+
- financial data
71+
- health data
72+
- PII (Personal Identifiable Information)
73+
74+
Testing
75+
- All new packages and most new significant functionality should come with unit tests
76+
77+
Unit tests
78+
- A good unit test should:
79+
- focus on a single use-case at a time
80+
- have a minimal set of assertions per test
81+
- demonstrate every use case. The rule of thumb is: if it can happen, it should be covered
82+
Refer to Django documentation for best practices in views, models, forms, and security considerations.

0 commit comments

Comments
 (0)