Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"scripts": {
"vscode:prepublish": "npm run build",
"build": "tsc -p ./",
"test": "npm run build && node --test",
"watch": "tsc -watch -p ./",
"package": "vsce package",
"publish": "vsce publish"
Expand Down
3 changes: 2 additions & 1 deletion src/graphWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function escapeLabel(s: string): string {
return s.replace(/[^A-Za-z0-9_]/g, '_');
}

function buildMermaid(index: WorkspaceIndex): string {
export function buildMermaid(index: WorkspaceIndex): string {
const lines: string[] = ['erDiagram'];
const modelNames = new Set<string>();
for (const app of index.apps) {
Expand Down Expand Up @@ -46,6 +46,7 @@ function buildMermaid(index: WorkspaceIndex): string {
: '}o--||';
const parts: string[] = [f.name];
if (f.onDelete) parts.push(f.onDelete);
if (f.throughModel) parts.push(`through ${f.throughModel}`);
if (f.relatedName) parts.push(`as ${f.relatedName}`);
const label = parts.length > 1
? `${parts[0]} [${parts.slice(1).join(', ')}]`
Expand Down
11 changes: 11 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ function extractRelatedName(argsBlock: string): string | undefined {
return m ? m[1] || m[2] : undefined;
}

function extractThroughModel(argsBlock: string): string | undefined {
const m = argsBlock.match(
/\bthrough\s*=\s*(?:'([^']+)'|"([^"]+)"|([A-Za-z_][A-Za-z0-9_.]*))/
);
return m ? m[1] || m[2] || m[3] : undefined;
}

function readBalancedArgs(lines: string[], startIdx: number): {
argsBlock: string;
endIdx: number;
Expand Down Expand Up @@ -241,6 +248,10 @@ export function parseModelsFile(filePath: string, content: string): ParsedModel[
if (onDelete) field.onDelete = onDelete;
const relatedName = extractRelatedName(argsInner);
if (relatedName) field.relatedName = relatedName;
if (fieldType === 'ManyToManyField') {
const throughModel = extractThroughModel(argsInner);
if (throughModel) field.throughModel = throughModel;
}
}
model.fields.push(field);
j = endIdx + 1;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ParsedField {
relationKind?: RelationKind;
onDelete?: string;
relatedName?: string;
throughModel?: string;
lineNumber: number;
}

Expand Down
45 changes: 45 additions & 0 deletions test/edge-labels.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const assert = require('node:assert/strict');
const Module = require('node:module');
const test = require('node:test');

const originalLoad = Module._load;
Module._load = function (request, parent, isMain) {
if (request === 'vscode') {
return {};
}
return originalLoad.call(this, request, parent, isMain);
};

const { buildMermaid } = require('../out/graphWebview');
const { parseModelsFile } = require('../out/parser');

test.after(() => {
Module._load = originalLoad;
});

test('labels OneToOneField metadata and ManyToManyField through models', () => {
const models = parseModelsFile('/project/app/models.py', `
class User(models.Model):
profile = models.OneToOneField(
'Profile', on_delete=models.CASCADE, related_name='user'
)

class Profile(models.Model):
pass

class Group(models.Model):
members = models.ManyToManyField(
'User', passthrough='Ignored', through='Membership', related_name='groups'
)

class Membership(models.Model):
pass
`);
const mermaid = buildMermaid({
apps: [{ name: 'app', path: '/project/app', models }],
scannedAt: 0,
});

assert.match(mermaid, /User \|\|--\|\| Profile : "profile \[CASCADE, as user\]"/);
assert.match(mermaid, /Group \}o--o\{ User : "members \[through Membership, as groups\]"/);
});
Loading