claude-code-templates 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ # Flask Route Generator
2
+
3
+ Create Flask routes with proper structure and error handling.
4
+
5
+ ## Purpose
6
+
7
+ This command helps you quickly create Flask routes with validation, error handling, and best practices.
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ /flask-route
13
+ ```
14
+
15
+ ## What this command does
16
+
17
+ 1. **Creates route functions** with proper decorators
18
+ 2. **Adds request validation** and error handling
19
+ 3. **Includes JSON responses** and status codes
20
+ 4. **Implements authentication** if needed
21
+ 5. **Follows Flask conventions** and best practices
22
+
23
+ ## Example Output
24
+
25
+ ```python
26
+ # routes.py or app.py
27
+ from flask import Flask, request, jsonify, abort
28
+ from flask_sqlalchemy import SQLAlchemy
29
+ from werkzeug.exceptions import BadRequest
30
+
31
+ app = Flask(__name__)
32
+
33
+ @app.route('/users', methods=['GET'])
34
+ def get_users():
35
+ """Get all users with optional pagination."""
36
+ try:
37
+ page = request.args.get('page', 1, type=int)
38
+ per_page = request.args.get('per_page', 10, type=int)
39
+
40
+ users = User.query.paginate(
41
+ page=page,
42
+ per_page=per_page,
43
+ error_out=False
44
+ )
45
+
46
+ return jsonify({
47
+ 'users': [user.to_dict() for user in users.items],
48
+ 'total': users.total,
49
+ 'pages': users.pages,
50
+ 'current_page': page
51
+ }), 200
52
+
53
+ except Exception as e:
54
+ return jsonify({'error': 'Failed to fetch users'}), 500
55
+
56
+ @app.route('/users/<int:user_id>', methods=['GET'])
57
+ def get_user(user_id):
58
+ """Get a specific user by ID."""
59
+ try:
60
+ user = User.query.get_or_404(user_id)
61
+ return jsonify(user.to_dict()), 200
62
+
63
+ except Exception as e:
64
+ return jsonify({'error': 'User not found'}), 404
65
+
66
+ @app.route('/users', methods=['POST'])
67
+ def create_user():
68
+ """Create a new user."""
69
+ try:
70
+ data = request.get_json()
71
+
72
+ if not data:
73
+ return jsonify({'error': 'No data provided'}), 400
74
+
75
+ # Validate required fields
76
+ required_fields = ['name', 'email']
77
+ for field in required_fields:
78
+ if field not in data:
79
+ return jsonify({'error': f'{field} is required'}), 400
80
+
81
+ # Check if email already exists
82
+ if User.query.filter_by(email=data['email']).first():
83
+ return jsonify({'error': 'Email already exists'}), 409
84
+
85
+ # Create new user
86
+ user = User(
87
+ name=data['name'],
88
+ email=data['email'],
89
+ phone=data.get('phone'),
90
+ address=data.get('address')
91
+ )
92
+
93
+ db.session.add(user)
94
+ db.session.commit()
95
+
96
+ return jsonify(user.to_dict()), 201
97
+
98
+ except BadRequest:
99
+ return jsonify({'error': 'Invalid JSON data'}), 400
100
+ except Exception as e:
101
+ db.session.rollback()
102
+ return jsonify({'error': 'Failed to create user'}), 500
103
+
104
+ @app.route('/users/<int:user_id>', methods=['PUT'])
105
+ def update_user(user_id):
106
+ """Update an existing user."""
107
+ try:
108
+ user = User.query.get_or_404(user_id)
109
+ data = request.get_json()
110
+
111
+ if not data:
112
+ return jsonify({'error': 'No data provided'}), 400
113
+
114
+ # Update fields
115
+ if 'name' in data:
116
+ user.name = data['name']
117
+ if 'email' in data:
118
+ # Check if new email already exists
119
+ existing_user = User.query.filter_by(email=data['email']).first()
120
+ if existing_user and existing_user.id != user_id:
121
+ return jsonify({'error': 'Email already exists'}), 409
122
+ user.email = data['email']
123
+ if 'phone' in data:
124
+ user.phone = data['phone']
125
+ if 'address' in data:
126
+ user.address = data['address']
127
+
128
+ db.session.commit()
129
+
130
+ return jsonify(user.to_dict()), 200
131
+
132
+ except BadRequest:
133
+ return jsonify({'error': 'Invalid JSON data'}), 400
134
+ except Exception as e:
135
+ db.session.rollback()
136
+ return jsonify({'error': 'Failed to update user'}), 500
137
+
138
+ @app.route('/users/<int:user_id>', methods=['DELETE'])
139
+ def delete_user(user_id):
140
+ """Delete a user."""
141
+ try:
142
+ user = User.query.get_or_404(user_id)
143
+
144
+ db.session.delete(user)
145
+ db.session.commit()
146
+
147
+ return jsonify({'message': 'User deleted successfully'}), 200
148
+
149
+ except Exception as e:
150
+ db.session.rollback()
151
+ return jsonify({'error': 'Failed to delete user'}), 500
152
+
153
+ # Error handlers
154
+ @app.errorhandler(404)
155
+ def not_found(error):
156
+ return jsonify({'error': 'Resource not found'}), 404
157
+
158
+ @app.errorhandler(400)
159
+ def bad_request(error):
160
+ return jsonify({'error': 'Bad request'}), 400
161
+
162
+ @app.errorhandler(500)
163
+ def internal_error(error):
164
+ return jsonify({'error': 'Internal server error'}), 500
165
+ ```
166
+
167
+ ## Route Patterns Supported
168
+
169
+ ### Basic Routes
170
+ ```python
171
+ @app.route('/')
172
+ @app.route('/users')
173
+ @app.route('/users/<int:user_id>')
174
+ ```
175
+
176
+ ### HTTP Methods
177
+ ```python
178
+ @app.route('/users', methods=['GET', 'POST'])
179
+ @app.route('/users/<int:id>', methods=['GET', 'PUT', 'DELETE'])
180
+ ```
181
+
182
+ ### URL Parameters
183
+ ```python
184
+ @app.route('/users/<int:user_id>')
185
+ @app.route('/posts/<string:slug>')
186
+ @app.route('/files/<path:filename>')
187
+ ```
188
+
189
+ ## Best Practices Included
190
+
191
+ - **Input validation** for all user data
192
+ - **Proper HTTP status codes** (200, 201, 400, 404, 500)
193
+ - **JSON responses** with consistent structure
194
+ - **Error handling** with try/catch blocks
195
+ - **Database rollback** on errors
196
+ - **RESTful conventions** for URL design
197
+ - **Documentation strings** for each route
198
+ - **Request data validation** before processing
199
+
200
+ ## Common Response Patterns
201
+
202
+ ```python
203
+ # Success with data
204
+ return jsonify({'data': result}), 200
205
+
206
+ # Created resource
207
+ return jsonify({'data': new_resource, 'id': new_id}), 201
208
+
209
+ # Validation error
210
+ return jsonify({'error': 'Field is required'}), 400
211
+
212
+ # Not found
213
+ return jsonify({'error': 'Resource not found'}), 404
214
+
215
+ # Server error
216
+ return jsonify({'error': 'Internal server error'}), 500
217
+ ```
@@ -0,0 +1,111 @@
1
+ # Python Linter
2
+
3
+ Run Python code linting and formatting tools.
4
+
5
+ ## Purpose
6
+
7
+ This command helps you maintain code quality using Python's best linting and formatting tools.
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ /lint
13
+ ```
14
+
15
+ ## What this command does
16
+
17
+ 1. **Runs multiple linters** (flake8, pylint, black, isort)
18
+ 2. **Provides detailed feedback** on code quality issues
19
+ 3. **Auto-fixes formatting** where possible
20
+ 4. **Checks type hints** if mypy is configured
21
+
22
+ ## Example Commands
23
+
24
+ ### Black (code formatting)
25
+ ```bash
26
+ # Format all Python files
27
+ black .
28
+
29
+ # Check formatting without changing files
30
+ black --check .
31
+
32
+ # Format specific file
33
+ black src/main.py
34
+ ```
35
+
36
+ ### flake8 (style guide enforcement)
37
+ ```bash
38
+ # Check all Python files
39
+ flake8 .
40
+
41
+ # Check specific directory
42
+ flake8 src/
43
+
44
+ # Check with specific rules
45
+ flake8 --max-line-length=88 .
46
+ ```
47
+
48
+ ### isort (import sorting)
49
+ ```bash
50
+ # Sort imports in all files
51
+ isort .
52
+
53
+ # Check import sorting
54
+ isort --check-only .
55
+
56
+ # Sort imports in specific file
57
+ isort src/main.py
58
+ ```
59
+
60
+ ### pylint (comprehensive linting)
61
+ ```bash
62
+ # Run pylint on all files
63
+ pylint src/
64
+
65
+ # Run with specific score threshold
66
+ pylint --fail-under=8.0 src/
67
+
68
+ # Generate detailed report
69
+ pylint --output-format=html src/ > pylint_report.html
70
+ ```
71
+
72
+ ### mypy (type checking)
73
+ ```bash
74
+ # Check types in all files
75
+ mypy .
76
+
77
+ # Check specific module
78
+ mypy src/models.py
79
+
80
+ # Check with strict mode
81
+ mypy --strict src/
82
+ ```
83
+
84
+ ## Configuration Files
85
+
86
+ Most projects benefit from configuration files:
87
+
88
+ ### .flake8
89
+ ```ini
90
+ [flake8]
91
+ max-line-length = 88
92
+ exclude = .git,__pycache__,venv
93
+ ignore = E203,W503
94
+ ```
95
+
96
+ ### pyproject.toml
97
+ ```toml
98
+ [tool.black]
99
+ line-length = 88
100
+
101
+ [tool.isort]
102
+ profile = "black"
103
+ ```
104
+
105
+ ## Best Practices
106
+
107
+ - Run linters before committing code
108
+ - Use consistent formatting across the project
109
+ - Fix linting issues promptly
110
+ - Configure linters to match your team's style
111
+ - Use type hints for better code documentation
@@ -0,0 +1,73 @@
1
+ # Test Runner
2
+
3
+ Run Python tests with pytest, unittest, or other testing frameworks.
4
+
5
+ ## Purpose
6
+
7
+ This command helps you run Python tests effectively with proper configuration and reporting.
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ /test
13
+ ```
14
+
15
+ ## What this command does
16
+
17
+ 1. **Detects test framework** (pytest, unittest, nose2)
18
+ 2. **Runs appropriate tests** with proper configuration
19
+ 3. **Provides coverage reporting** if available
20
+ 4. **Shows clear test results** with failure details
21
+
22
+ ## Example Commands
23
+
24
+ ### pytest (recommended)
25
+ ```bash
26
+ # Run all tests
27
+ pytest
28
+
29
+ # Run with coverage
30
+ pytest --cov=src --cov-report=html
31
+
32
+ # Run specific test file
33
+ pytest tests/test_models.py
34
+
35
+ # Run with verbose output
36
+ pytest -v
37
+
38
+ # Run tests matching pattern
39
+ pytest -k "test_user"
40
+ ```
41
+
42
+ ### unittest
43
+ ```bash
44
+ # Run all tests
45
+ python -m unittest discover
46
+
47
+ # Run specific test file
48
+ python -m unittest tests.test_models
49
+
50
+ # Run with verbose output
51
+ python -m unittest -v
52
+ ```
53
+
54
+ ### Django tests
55
+ ```bash
56
+ # Run all Django tests
57
+ python manage.py test
58
+
59
+ # Run specific app tests
60
+ python manage.py test myapp
61
+
62
+ # Run with coverage
63
+ coverage run --source='.' manage.py test
64
+ coverage report
65
+ ```
66
+
67
+ ## Best Practices
68
+
69
+ - Write tests for all critical functionality
70
+ - Use descriptive test names
71
+ - Keep tests isolated and independent
72
+ - Mock external dependencies
73
+ - Aim for high test coverage (80%+)