Skip to content

Commit 018f4a5

Browse files
committed
Remove the legacy db model implementation.
1 parent b90fa83 commit 018f4a5

File tree

12 files changed

+6
-730
lines changed

12 files changed

+6
-730
lines changed

django_unicorn/decorators.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,11 @@
11
import logging
22
import time
3-
import warnings
43

54
from django.conf import settings
65

76
from decorator import decorator
87

98

10-
@decorator
11-
def db_model(func, *args, **kwargs):
12-
"""
13-
Converts a JSON representation of a Django model into an actual model.
14-
15-
For example:
16-
@db_model
17-
def delete(self, model):
18-
...
19-
20-
Will get converted to:
21-
`component.delete({ 'name': 'modelName', pk: 1})` -> `component.delete(modelInstance)`
22-
"""
23-
24-
warnings.warn(
25-
"db_model is deprecated and will be removed in 0.28.0", stacklevel=2,
26-
)
27-
28-
instance = args[0]
29-
model_dictionary = args[1]
30-
31-
if hasattr(instance, "Meta") and hasattr(instance.Meta, "db_models"):
32-
db_model_name = model_dictionary.get("name")
33-
assert db_model_name, "Missing db model name"
34-
db_model_pk = model_dictionary.get("pk")
35-
assert db_model_pk, "Missing db model pk"
36-
db_model_found = False
37-
38-
for db_model in instance.Meta.db_models:
39-
if db_model.name == db_model_name:
40-
model = db_model.model_class.objects.get(pk=db_model_pk)
41-
args = (instance, model,) + args[2:]
42-
db_model_found = True
43-
break
44-
45-
assert db_model_found, f"No db_model found that matches '{db_model_name}'"
46-
else:
47-
raise AssertionError("No db_models defined")
48-
49-
result = func(*args, **kwargs)
50-
51-
return result
52-
53-
549
@decorator
5510
def timed(func, *args, **kwargs):
5611
"""

django_unicorn/static/unicorn/js/attribute.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export class Attribute {
1010
this.value = this.attribute.value;
1111
this.isUnicorn = false;
1212
this.isModel = false;
13-
this.isField = false;
1413
this.isPoll = false;
1514
this.isLoading = false;
1615
this.isTarget = false;
1716
this.isPartial = false;
1817
this.isDirty = false;
1918
this.isKey = false;
20-
this.isPK = false;
2119
this.isError = false;
2220
this.modifiers = {};
2321
this.eventType = null;
@@ -35,10 +33,6 @@ export class Attribute {
3533
// Use `contains` when there could be modifiers
3634
if (contains(this.name, ":model")) {
3735
this.isModel = true;
38-
} else if (contains(this.name, ":field")) {
39-
this.isField = true;
40-
} else if (contains(this.name, ":db")) {
41-
this.isDb = true;
4236
} else if (contains(this.name, ":poll.disable")) {
4337
this.isPollDisable = true;
4438
} else if (contains(this.name, ":poll")) {
@@ -53,8 +47,6 @@ export class Attribute {
5347
this.isDirty = true;
5448
} else if (this.name === "unicorn:key" || this.name === "u:key") {
5549
this.isKey = true;
56-
} else if (this.name === "unicorn:pk" || this.name === "u:pk") {
57-
this.isPK = true;
5850
} else if (contains(this.name, ":error:")) {
5951
this.isError = true;
6052
} else {

django_unicorn/static/unicorn/js/component.js

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { debounce } from "./delayers.js";
22
import { Element } from "./element.js";
33
import {
44
addActionEventListener,
5-
addDbEventListener,
65
addModelEventListener,
76
} from "./eventListeners.js";
87
import { components, lifecycleEvents } from "./store.js";
@@ -38,7 +37,6 @@ export class Component {
3837

3938
this.root = undefined;
4039
this.modelEls = [];
41-
this.dbEls = [];
4240
this.loadingEls = [];
4341
this.keyEls = [];
4442
this.errors = {};
@@ -52,7 +50,6 @@ export class Component {
5250
this.actionEvents = {};
5351
this.attachedEventTypes = [];
5452
this.attachedModelEvents = [];
55-
this.attachedDbEvents = [];
5653

5754
this.init();
5855
this.refreshEventListeners();
@@ -168,7 +165,6 @@ export class Component {
168165
refreshEventListeners() {
169166
this.actionEvents = {};
170167
this.modelEls = [];
171-
this.dbEls = [];
172168

173169
try {
174170
this.walker(
@@ -182,26 +178,7 @@ export class Component {
182178
const element = new Element(el);
183179

184180
if (element.isUnicorn) {
185-
if (hasValue(element.field) && hasValue(element.db)) {
186-
if (!this.attachedDbEvents.some((e) => e.isSame(element))) {
187-
this.attachedDbEvents.push(element);
188-
addDbEventListener(this, element);
189-
190-
// If a field is lazy, also add an event listener for input for dirty states
191-
if (element.field.isLazy) {
192-
// This input event for isLazy will be stopped after dirty is checked when the event fires
193-
addDbEventListener(this, element, "input");
194-
}
195-
}
196-
197-
if (!this.dbEls.some((e) => e.isSame(element))) {
198-
this.dbEls.push(element);
199-
}
200-
} else if (
201-
hasValue(element.model) &&
202-
isEmpty(element.db) &&
203-
isEmpty(element.field)
204-
) {
181+
if (hasValue(element.model)) {
205182
if (!this.attachedModelEvents.some((e) => e.isSame(element))) {
206183
this.attachedModelEvents.push(element);
207184
addModelEventListener(this, element);
@@ -274,7 +251,6 @@ export class Component {
274251
// Can hard-code `forceModelUpdate` to `true` since it is always required for
275252
// `callMethod` actions
276253
this.setModelValues(triggeringElements, true);
277-
this.setDbModelValues();
278254
}
279255
});
280256
}
@@ -423,42 +399,6 @@ export class Component {
423399
}
424400
}
425401

426-
/**
427-
* Sets all db model values.
428-
*/
429-
setDbModelValues() {
430-
this.dbEls.forEach((element) => {
431-
if (isEmpty(element.db.pk)) {
432-
// Empty string for the PK implies that the model is not associated to an actual model instance
433-
element.setValue("");
434-
} else {
435-
const dbName = element.db.name || element.model.name;
436-
437-
if (isEmpty(dbName)) {
438-
throw Error(
439-
"Setting a field value requires a db or model name to be set"
440-
);
441-
}
442-
443-
let datas = this.data[dbName];
444-
445-
// Force the data to be an array if it isn't already for the next step
446-
if (!Array.isArray(datas)) {
447-
datas = [datas];
448-
}
449-
450-
datas.forEach((model) => {
451-
// Convert the model's pk to a string because it will always be a string on the element
452-
if (hasValue(model) && hasValue(model.pk)) {
453-
if (model.pk.toString() === element.db.pk) {
454-
element.setValue(model[element.field.name]);
455-
}
456-
}
457-
});
458-
}
459-
});
460-
}
461-
462402
/**
463403
* Sets all model values.
464404
* @param {[Element]} triggeringElements The elements that triggered the event.

django_unicorn/static/unicorn/js/element.js

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Attribute } from "./attribute.js";
2-
import { isEmpty, generateDbKey, hasValue } from "./utils.js";
2+
import { isEmpty, hasValue } from "./utils.js";
33

44
/**
55
* Encapsulate DOM element for Unicorn-related information.
@@ -29,8 +29,6 @@ export class Element {
2929
this.loading = {};
3030
this.dirty = {};
3131
this.actions = [];
32-
this.db = {};
33-
this.field = {};
3432
this.partials = [];
3533
this.target = null;
3634
this.key = null;
@@ -49,11 +47,8 @@ export class Element {
4947
this.isUnicorn = true;
5048
}
5149

52-
if (attribute.isModel || attribute.isField) {
53-
let key = "model";
54-
if (attribute.isField) {
55-
key = "field";
56-
}
50+
if (attribute.isModel) {
51+
const key = "model";
5752

5853
this[key].name = attribute.value;
5954
this[key].eventType = attribute.modifiers.lazy ? "blur" : "input";
@@ -62,10 +57,6 @@ export class Element {
6257
this[key].debounceTime = attribute.modifiers.debounce
6358
? parseInt(attribute.modifiers.debounce, 10) || -1
6459
: -1;
65-
} else if (attribute.isDb) {
66-
this.db.name = attribute.value;
67-
} else if (attribute.isPK) {
68-
this.db.pk = attribute.value;
6960
} else if (attribute.isPoll) {
7061
this.poll.method = attribute.value ? attribute.value : "refresh";
7162
this.poll.timing = 2000;
@@ -146,51 +137,6 @@ export class Element {
146137
this.errors.push({ code, message: attribute.value });
147138
}
148139
}
149-
150-
// Look in parent elements if the db.pk or db.name is missing
151-
if (this.isUnicorn && hasValue(this.field)) {
152-
const dbAttrs = ["pk", "name"];
153-
let elToCheck = this;
154-
155-
// Look for `db.pk` and `db.name`
156-
dbAttrs.forEach((attr) => {
157-
elToCheck = this;
158-
159-
while (isEmpty(this.db[attr])) {
160-
if (elToCheck.isUnicorn && hasValue(elToCheck.db[attr])) {
161-
this.db[attr] = elToCheck.db[attr];
162-
}
163-
164-
if (elToCheck.isRoot()) {
165-
break;
166-
}
167-
168-
elToCheck = elToCheck.parent;
169-
}
170-
});
171-
172-
// Look for model.name
173-
elToCheck = this;
174-
175-
while (isEmpty(this.model.name)) {
176-
if (elToCheck.isUnicorn && hasValue(elToCheck.model.name)) {
177-
if (hasValue(this.field) && isEmpty(this.db)) {
178-
// Handle a model + field that is not a db
179-
this.model = this.field; // Make sure to keep all modifiers from the field for the model
180-
this.model.name = `${elToCheck.model.name}.${this.field.name}`;
181-
this.field = {};
182-
} else {
183-
this.model.name = elToCheck.model.name;
184-
}
185-
}
186-
187-
if (elToCheck.isRoot()) {
188-
break;
189-
}
190-
191-
elToCheck = elToCheck.parent;
192-
}
193-
}
194140
}
195141

196142
/**
@@ -214,13 +160,6 @@ export class Element {
214160
this.el.hidden = null;
215161
}
216162

217-
/**
218-
* A key that takes into consideration the db name and pk.
219-
*/
220-
dbKey() {
221-
return generateDbKey(this);
222-
}
223-
224163
/**
225164
* Get the element's next parent that is a unicorn element.
226165
*

0 commit comments

Comments
 (0)