askrobots / dbbasic-cli
Public
Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.
crm.py · 6.3 KB · 189 lines
| 1 | """Create instant CRM application""" |
| 2 | |
| 3 | import typer |
| 4 | from rich.console import Console |
| 5 | from rich.progress import Progress, SpinnerColumn, TextColumn |
| 6 | import time |
| 7 | from pathlib import Path |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | |
| 11 | from .config import DBBASIC_DIR |
| 12 | from ..bootstrap import ensure_dbbasic |
| 13 | |
| 14 | console = Console() |
| 15 | |
| 16 | def create_crm( |
| 17 | name: str = typer.Argument("crm", help="Project name"), |
| 18 | port: int = typer.Option(8000, "--port", "-p", help="Port to run on") |
| 19 | ): |
| 20 | """Create instant CRM application with contacts, deals, and pipeline""" |
| 21 | |
| 22 | # Ensure DBBasic is installed |
| 23 | dbbasic_dir = ensure_dbbasic() |
| 24 | |
| 25 | start_time = time.time() |
| 26 | |
| 27 | with Progress( |
| 28 | SpinnerColumn(), |
| 29 | TextColumn("[progress.description]{task.description}"), |
| 30 | console=console, |
| 31 | ) as progress: |
| 32 | |
| 33 | # Create project directory |
| 34 | task = progress.add_task("Creating CRM application...", total=5) |
| 35 | project_dir = Path.cwd() / name |
| 36 | |
| 37 | if project_dir.exists(): |
| 38 | console.print(f"[red]Directory {name} already exists![/red]") |
| 39 | raise typer.Exit(1) |
| 40 | |
| 41 | project_dir.mkdir(parents=True) |
| 42 | progress.advance(task) |
| 43 | progress.update(task, description="Setting up database...") |
| 44 | |
| 45 | # Create configuration |
| 46 | config = f""" |
| 47 | # DBBasic CRM Configuration |
| 48 | project: |
| 49 | name: {name} |
| 50 | type: crm |
| 51 | port: {port} |
| 52 | |
| 53 | models: |
| 54 | - contacts: |
| 55 | fields: |
| 56 | - {{name: name, type: string, required: true}} |
| 57 | - {{name: email, type: email, unique: true}} |
| 58 | - {{name: phone, type: string}} |
| 59 | - {{name: company, type: string}} |
| 60 | - {{name: status, type: enum, values: [lead, prospect, customer, inactive]}} |
| 61 | - {{name: value, type: decimal}} |
| 62 | - {{name: notes, type: text}} |
| 63 | |
| 64 | - deals: |
| 65 | fields: |
| 66 | - {{name: title, type: string, required: true}} |
| 67 | - {{name: contact_id, type: reference, model: contacts}} |
| 68 | - {{name: amount, type: decimal}} |
| 69 | - {{name: stage, type: enum, values: [discovery, proposal, negotiation, closed_won, closed_lost]}} |
| 70 | - {{name: probability, type: integer}} |
| 71 | - {{name: close_date, type: date}} |
| 72 | - {{name: notes, type: text}} |
| 73 | |
| 74 | - activities: |
| 75 | fields: |
| 76 | - {{name: type, type: enum, values: [call, email, meeting, task]}} |
| 77 | - {{name: subject, type: string, required: true}} |
| 78 | - {{name: contact_id, type: reference, model: contacts}} |
| 79 | - {{name: deal_id, type: reference, model: deals}} |
| 80 | - {{name: due_date, type: datetime}} |
| 81 | - {{name: completed, type: boolean}} |
| 82 | - {{name: notes, type: text}} |
| 83 | |
| 84 | views: |
| 85 | - dashboard: |
| 86 | type: metrics |
| 87 | queries: |
| 88 | - total_contacts: SELECT COUNT(*) FROM contacts |
| 89 | - active_deals: SELECT COUNT(*) FROM deals WHERE stage NOT IN ('closed_won', 'closed_lost') |
| 90 | - pipeline_value: SELECT SUM(amount) FROM deals WHERE stage NOT IN ('closed_won', 'closed_lost') |
| 91 | |
| 92 | - pipeline: |
| 93 | type: kanban |
| 94 | model: deals |
| 95 | group_by: stage |
| 96 | card_fields: [title, amount, contact_id, close_date] |
| 97 | |
| 98 | services: |
| 99 | - lead_scoring: |
| 100 | description: "Score leads based on engagement and value" |
| 101 | trigger: contacts.on_update |
| 102 | |
| 103 | - deal_probability: |
| 104 | description: "Calculate deal win probability" |
| 105 | trigger: deals.on_update |
| 106 | """ |
| 107 | |
| 108 | (project_dir / "dbbasic.yaml").write_text(config) |
| 109 | progress.advance(task) |
| 110 | |
| 111 | # Create sample data |
| 112 | progress.update(task, description="Adding sample data...") |
| 113 | |
| 114 | sample_data = f""" |
| 115 | # Sample CRM Data |
| 116 | INSERT INTO contacts (name, email, phone, company, status, value) VALUES |
| 117 | ('John Smith', 'john@acme.com', '555-0101', 'Acme Corp', 'customer', 50000), |
| 118 | ('Jane Doe', 'jane@globex.com', '555-0102', 'Globex Inc', 'prospect', 75000), |
| 119 | ('Bob Wilson', 'bob@initech.com', '555-0103', 'Initech', 'lead', 25000); |
| 120 | |
| 121 | INSERT INTO deals (title, contact_id, amount, stage, probability, close_date) VALUES |
| 122 | ('Acme Corp - Enterprise Plan', 1, 50000, 'negotiation', 75, '2024-02-01'), |
| 123 | ('Globex Inc - Starter Plan', 2, 15000, 'proposal', 50, '2024-02-15'), |
| 124 | ('Initech - Custom Solution', 3, 100000, 'discovery', 25, '2024-03-01'); |
| 125 | |
| 126 | INSERT INTO activities (type, subject, contact_id, due_date, completed) VALUES |
| 127 | ('call', 'Follow up on proposal', 1, '2024-01-20 14:00', false), |
| 128 | ('meeting', 'Demo presentation', 2, '2024-01-22 10:00', false), |
| 129 | ('email', 'Send contract', 3, '2024-01-21 09:00', false); |
| 130 | """ |
| 131 | |
| 132 | (project_dir / "sample_data.sql").write_text(sample_data) |
| 133 | progress.advance(task) |
| 134 | |
| 135 | # Create startup script |
| 136 | progress.update(task, description="Creating startup scripts...") |
| 137 | |
| 138 | startup_script = f'''#!/usr/bin/env python3 |
| 139 | """ |
| 140 | DBBasic CRM - Quick Start |
| 141 | """ |
| 142 | |
| 143 | import sys |
| 144 | from pathlib import Path |
| 145 | |
| 146 | # Try to use local engine first, fall back to installed |
| 147 | try: |
| 148 | # Use the bundled engine |
| 149 | from dbbasic.engine import create_app |
| 150 | except ImportError: |
| 151 | # Fall back to DBBasic core |
| 152 | sys.path.insert(0, '{dbbasic_dir}') |
| 153 | from dbbasic_crud_engine import create_app |
| 154 | |
| 155 | import uvicorn |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | app = create_app(config_file="dbbasic.yaml") |
| 159 | uvicorn.run(app, host="0.0.0.0", port={port}) |
| 160 | ''' |
| 161 | |
| 162 | (project_dir / "run.py").write_text(startup_script) |
| 163 | (project_dir / "run.py").chmod(0o755) |
| 164 | progress.advance(task) |
| 165 | |
| 166 | # Final setup |
| 167 | progress.update(task, description="Finalizing...") |
| 168 | time.sleep(0.5) # Brief pause for effect |
| 169 | progress.advance(task) |
| 170 | |
| 171 | elapsed = time.time() - start_time |
| 172 | |
| 173 | # Success message |
| 174 | console.print(f"\n[bold green]✅ CRM created in {elapsed:.1f} seconds![/bold green]\n") |
| 175 | console.print(f"[bold]Your CRM is ready at:[/bold] {project_dir}\n") |
| 176 | console.print("[bold]Features included:[/bold]") |
| 177 | console.print("• Contact management") |
| 178 | console.print("• Deal pipeline (Kanban view)") |
| 179 | console.print("• Activity tracking") |
| 180 | console.print("• Lead scoring (AI-powered)") |
| 181 | console.print("• Dashboard with metrics") |
| 182 | console.print("• API with documentation") |
| 183 | console.print("• Sample data\n") |
| 184 | |
| 185 | console.print("[bold]To start your CRM:[/bold]") |
| 186 | console.print(f" cd {name}") |
| 187 | console.print(f" python run.py\n") |
| 188 | console.print(f"Then open: [blue]http://localhost:{port}[/blue]\n") |
| 189 | console.print("[dim]API docs will be at:[/dim] [blue]http://localhost:{port}/docs[/blue]") |