Passed
Push — master ( aeb165...d9ae97 )
by Dean
03:04
created

UserRule

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Test Coverage

Coverage 0%
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
1
from plugin.models.core import db
2
3
from playhouse.apsw_ext import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like playhouse.apsw_ext should generally be avoided.
Loading history...
4
5
6
def migrate(migrator, database):
0 ignored issues
show
Unused Code introduced by
The argument database seems to be unused.
Loading history...
7
    migrator.create_tables(
8
        #
9
        # Plex
10
        #
11
        PlexAccount,
12
13
        PlexBasicCredential,
14
15
        #
16
        # Sync
17
        #
18
        SyncStatus,
19
20
        SyncResult,
21
        SyncResultError,
22
        SyncResultException,
23
24
        #
25
        # Trakt
26
        #
27
        TraktAccount,
28
29
        TraktBasicCredential,
30
        TraktOAuthCredential,
31
32
        #
33
        # Main
34
        #
35
        Account,
36
        Exception,
37
        Message,
38
        Session,
39
40
        ConfigurationOption,
41
42
        ActionHistory,
43
        ActionQueue,
44
45
        Client,
46
        ClientRule,
47
48
        User,
49
        UserRule
50
    )
51
52
53
class Account(Model):
54
    class Meta:
55
        database = db
56
57
    name = CharField(null=True, unique=True)
58
    thumb = TextField(null=True)
59
60
61
class Client(Model):
62
    class Meta:
63
        database = db
64
        db_table = 'session.client'
65
66
    account = ForeignKeyField(Account, 'clients', null=True)
67
68
    # Identification
69
    key = CharField(unique=True)
70
    name = CharField(null=True)
71
72
    # Device
73
    device_class = CharField(null=True)
74
    platform = CharField(null=True)
75
    product = CharField(null=True)
76
    version = CharField(null=True)
77
78
    # Network
79
    host = CharField(null=True)
80
    address = CharField(null=True)
81
    port = IntegerField(null=True)
82
83
    # Protocol
84
    protocol = CharField(null=True)
85
    protocol_capabilities = CharField(null=True)
86
    protocol_version = CharField(null=True)
87
88
89
class ClientRule(Model):
90
    class Meta:
91
        database = db
92
        db_table = 'session.client.rule'
93
94
    account = ForeignKeyField(Account, 'client_rules')
95
96
    key = CharField(null=True)
97
    name = CharField(null=True)
98
    address = CharField(null=True)
99
100
    priority = IntegerField()
101
102
103
class User(Model):
104
    class Meta:
105
        database = db
106
        db_table = 'session.user'
107
108
    account = ForeignKeyField(Account, 'users', null=True)
109
110
    # Identification
111
    key = IntegerField(unique=True)
112
    name = CharField(null=True)
113
114
    thumb = CharField(null=True)
115
116
117
class UserRule(Model):
118
    class Meta:
119
        database = db
120
        db_table = 'session.user.rule'
121
122
    account = ForeignKeyField(Account, 'user_rules')
123
124
    name = CharField(null=True)
125
126
    priority = IntegerField()
127
128
129 View Code Duplication
class Session(Model):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
    class Meta:
131
        database = db
132
133
    account = ForeignKeyField(Account, 'sessions', null=True)
134
    client = ForeignKeyField(Client, 'sessions', to_field='key', null=True)
135
    user = ForeignKeyField(User, 'sessions', to_field='key', null=True)
136
137
    rating_key = IntegerField(null=True)
138
    session_key = TextField(null=True, unique=True)
139
140
    state = CharField(null=True)
141
142
    progress = FloatField(null=True)
143
144
    duration = IntegerField(null=True)
145
    view_offset = IntegerField(null=True)
146
147
148
class ConfigurationOption(Model):
149
    class Meta:
150
        database = db
151
        db_table = 'configuration.option'
152
153
        primary_key = CompositeKey('account', 'key')
154
155
    account = ForeignKeyField(Account, 'sync_configuration')
156
157
    key = CharField(max_length=60)
158
    value = BlobField()
159
160
#
161
# Action
162
#
163
164
class ActionHistory(Model):
165
    class Meta:
166
        database = db
167
        db_table = 'action.history'
168
169
    account = ForeignKeyField(Account, 'action_history')
170
    session = ForeignKeyField(Session, 'action_history', null=True)
171
172
    event = CharField()
173
    performed = CharField(null=True)
174
175
    queued_at = DateTimeField()
176
    sent_at = DateTimeField()
177
178
179
class ActionQueue(Model):
180
    class Meta:
181
        database = db
182
        db_table = 'action.queue'
183
        primary_key = CompositeKey('session', 'event')
184
185
    account = ForeignKeyField(Account, 'action_queue')
186
    session = ForeignKeyField(Session, 'action_queue', null=True)
187
188
    event = CharField()
189
    request = BlobField()
190
191
    queued_at = DateTimeField()
192
193
#
194
# Exception / Message
195
#
196
197 View Code Duplication
class Message(Model):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
198
    class Meta:
199
        database = db
200
        db_table = 'message'
201
202
    code = IntegerField(null=True)
203
    type = IntegerField()
204
205
    last_logged_at = DateTimeField()
206
    last_viewed_at = DateTimeField(null=True)
207
208
    # Tracking data
209
    exception_hash = CharField(null=True, unique=True, max_length=32)
210
    revision = IntegerField(null=True)
211
212
    version_base = CharField(max_length=12)
213
    version_branch = CharField(max_length=42)
214
215
    # User-friendly explanation
216
    summary = CharField(null=True, max_length=160)  # Short single-line summary
217
    description = TextField(null=True)  # Extra related details
218
219
220 View Code Duplication
class Exception(Model):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in Exception.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
221
    class Meta:
222
        database = db
223
        db_table = 'exception'
224
225
    error = ForeignKeyField(Message, 'exceptions', null=True)
226
227
    type = TextField()
228
    message = TextField()
229
    traceback = TextField()
230
231
    hash = CharField(null=True, max_length=32)
232
233
    timestamp = DateTimeField()
234
    version_base = CharField(max_length=12)
235
    version_branch = CharField(max_length=42)
236
237
#
238
# Plex
239
#
240
241
class PlexAccount(Model):
242
    class Meta:
243
        database = db
244
        db_table = 'plex.account'
245
246
    account = ForeignKeyField(Account, 'plex_accounts', unique=True)
247
248
    username = CharField(null=True, unique=True)
249
    thumb = TextField(null=True)
250
251
252
class PlexBasicCredential(Model):
253
    class Meta:
254
        database = db
255
        db_table = 'plex.credential.basic'
256
257
    account = ForeignKeyField(PlexAccount, 'basic_credentials', unique=True)
258
259
    password = CharField(null=True)
260
261
    # Authorization
262
    token = CharField(null=True)
263
264
#
265
# Trakt
266
#
267
268
class TraktAccount(Model):
269
    class Meta:
270
        database = db
271
        db_table = 'trakt.account'
272
273
    account = ForeignKeyField(Account, 'trakt_accounts', unique=True)
274
275
    username = CharField(null=True, unique=True)
276
    thumb = TextField(null=True)
277
278
    cover = TextField(null=True)
279
    timezone = TextField(null=True)
280
281
    refreshed_at = DateTimeField(null=True)
282
283
284
class TraktBasicCredential(Model):
285
    class Meta:
286
        database = db
287
        db_table = 'trakt.credential.basic'
288
289
    account = ForeignKeyField(TraktAccount, 'basic_credentials', unique=True)
290
291
    password = CharField(null=True)
292
293
    # Authorization
294
    token = CharField(null=True)
295
296
297
class TraktOAuthCredential(Model):
298
    class Meta:
299
        database = db
300
        db_table = 'trakt.credential.oauth'
301
302
    account = ForeignKeyField(TraktAccount, 'oauth_credentials', unique=True)
303
304
    code = CharField(null=True)
305
306
    # Authorization
307
    access_token = CharField(null=True)
308
    refresh_token = CharField(null=True)
309
310
    created_at = IntegerField(null=True)
311
    expires_in = IntegerField(null=True)
312
313
    token_type = CharField(null=True)
314
    scope = CharField(null=True)
315
316
#
317
# Sync
318
#
319
320
class SyncStatus(Model):
321
    class Meta:
322
        database = db
323
        db_table = 'sync.status'
324
325
    account = ForeignKeyField(Account, 'sync_status')
326
327
    mode = IntegerField()
328
    section = CharField(null=True, max_length=3)
329
330
331
class SyncResult(Model):
332
    class Meta:
333
        database = db
334
        db_table = 'sync.result'
335
336
    status = ForeignKeyField(SyncStatus, 'history')
337
338
    # Timestamps
339
    started_at = DateTimeField(null=True)
340
    ended_at = DateTimeField(null=True)
341
342
    # Result
343
    success = BooleanField(null=True)
344
345
346
class SyncResultError(Model):
347
    class Meta:
348
        database = db
349
        db_table = 'sync.result.error'
350
351
    result = ForeignKeyField(SyncResult, 'errors')
352
    error = ForeignKeyField(Message, 'results')
353
354
355
class SyncResultException(Model):
356
    class Meta:
357
        database = db
358
        db_table = 'sync.result.exception'
359
360
    result = ForeignKeyField(SyncResult, 'exceptions')
361
    exception = ForeignKeyField(Exception, 'results')
362
363
#
364
# Schema specification (for migration verification)
365
#
366
367
SPEC = {
368
    #
369
    # Account
370
    #
371
372
    'account': {
373
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
374
375
        'name':                     'VARCHAR(255)',
376
        'thumb':                    'TEXT'
377
    },
378
    'plex.account': {
379
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
380
        'account_id':               'INTEGER NOT NULL',
381
382
        'username':                 'VARCHAR(255)',
383
        'thumb':                    'TEXT'
384
    },
385
    'plex.credential.basic': {
386
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
387
        'account_id':               'INTEGER NOT NULL',
388
389
        'password':                 'VARCHAR(255)',
390
391
        'token':                    'VARCHAR(255)'
392
    },
393
394
    'trakt.account': {
395
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
396
        'account_id':               'INTEGER NOT NULL',
397
398
        'username':                 'VARCHAR(255)',
399
        'thumb':                    'TEXT',
400
401
        'cover':                    'TEXT',
402
        'timezone':                 'TEXT',
403
404
        'refreshed_at':             'DATETIME'
405
    },
406
    'trakt.credential.basic': {
407
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
408
        'account_id':               'INTEGER NOT NULL',
409
410
        'password':                 'VARCHAR(255)',
411
412
        'token':                    'VARCHAR(255)'
413
    },
414
    'trakt.credential.oauth': {
415
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
416
        'account_id':               'INTEGER NOT NULL',
417
418
        'code':                     'VARCHAR(255)',
419
420
        'access_token':             'VARCHAR(255)',
421
        'refresh_token':            'VARCHAR(255)',
422
423
        'created_at':               'INTEGER',
424
        'expires_in':               'INTEGER',
425
426
        'token_type':               'VARCHAR(255)',
427
        'scope':                    'VARCHAR(255)'
428
    },
429
430
    #
431
    # Session
432
    #
433
434
    'session': {
435
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
436
        'account_id':               'INTEGER',
437
        'client_id':                'VARCHAR(255)',
438
        'user_id':                  'INTEGER',
439
440
        'rating_key':               'INTEGER',
441
        'session_key':              'TEXT',
442
443
        'state':                    'VARCHAR(255)',
444
445
        'progress':                 'REAL',
446
447
        'duration':                 'INTEGER',
448
        'view_offset':              'INTEGER'
449
    },
450
451
    'session.client': {
452
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
453
        'account_id':               'INTEGER',
454
455
        'key':                      'VARCHAR(255) NOT NULL',
456
        'name':                     'VARCHAR(255)',
457
458
        'device_class':             'VARCHAR(255)',
459
        'platform':                 'VARCHAR(255)',
460
        'product':                  'VARCHAR(255)',
461
        'version':                  'VARCHAR(255)',
462
463
        'host':                     'VARCHAR(255)',
464
        'address':                  'VARCHAR(255)',
465
        'port':                     'INTEGER',
466
467
        'protocol':                 'VARCHAR(255)',
468
        'protocol_capabilities':    'VARCHAR(255)',
469
        'protocol_version':         'VARCHAR(255)'
470
    },
471
    'session.client.rule': {
472
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
473
        'account_id':               'INTEGER NOT NULL',
474
475
        'key':                      'VARCHAR(255)',
476
        'name':                     'VARCHAR(255)',
477
        'address':                  'VARCHAR(255)',
478
479
        'priority':                 'INTEGER NOT NULL'
480
    },
481
482
    'session.user': {
483
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
484
        'account_id':               'INTEGER',
485
486
        'key':                      'INTEGER NOT NULL',
487
        'name':                     'VARCHAR(255)',
488
489
        'thumb':                    'VARCHAR(255)',
490
    },
491
    'session.user.rule': {
492
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
493
        'account_id':               'INTEGER NOT NULL',
494
495
        'name':                     'VARCHAR(255)',
496
497
        'priority':                 'INTEGER NOT NULL'
498
    },
499
500
    #
501
    # Configuration
502
    #
503
504
    'configuration.option': {
505
        'key':                      'VARCHAR(60) PRIMARY KEY NOT NULL',
506
        'account_id':               'INTEGER PRIMARY KEY NOT NULL',
507
508
        'value':                    'BLOB NOT NULL'
509
    },
510
511
    #
512
    # Actions
513
    #
514
515
    'action.history': {
516
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
517
        'account_id':               'INTEGER NOT NULL',
518
        'session_id':               'INTEGER',
519
520
        'event':                    'VARCHAR(255) NOT NULL',
521
        'performed':                'VARCHAR(255)',
522
523
        'queued_at':                'DATETIME NOT NULL',
524
        'sent_at':                  'DATETIME NOT NULL'
525
    },
526
    'action.queue': {
527
        'account_id':               'INTEGER NOT NULL',
528
        'session_id':               'INTEGER PRIMARY KEY',
529
530
        'event':                    'VARCHAR(255) PRIMARY KEY NOT NULL',
531
        'request':                  'BLOB NOT NULL',
532
533
        'queued_at':                'DATETIME NOT NULL',
534
    },
535
536
    #
537
    # Messages/Exceptions
538
    #
539
540
    'message': {
541
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
542
543
        'code':                     'INTEGER',
544
        'type':                     'INTEGER NOT NULL',
545
546
        'last_logged_at':           'DATETIME NOT NULL',
547
        'last_viewed_at':           'DATETIME',
548
549
        'exception_hash':           'VARCHAR(32)',
550
        'revision':                 'INTEGER',
551
552
        'version_base':             'VARCHAR(12) NOT NULL',
553
        'version_branch':           'VARCHAR(42) NOT NULL',
554
555
        'summary':                  'VARCHAR(160)',
556
        'description':              'TEXT'
557
    },
558
    'exception': {
559
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
560
        'error_id':                 'INTEGER',
561
562
        'type':                     'TEXT NOT NULL',
563
        'message':                  'TEXT NOT NULL',
564
        'traceback':                'TEXT NOT NULL',
565
566
        'hash':                     'VARCHAR(32)',
567
568
        'timestamp':                'DATETIME NOT NULL',
569
        'version_base':             'VARCHAR(12) NOT NULL',
570
        'version_branch':           'VARCHAR(42) NOT NULL',
571
    },
572
573
    #
574
    # Syncing
575
    #
576
577
    'sync.status': {
578
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
579
        'account_id':               'INTEGER NOT NULL',
580
581
        'mode':                     'INTEGER NOT NULL',
582
        'section':                  'VARCHAR(3)'
583
    },
584
585
    'sync.result': {
586
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
587
        'status_id':                'INTEGER NOT NULL',
588
589
        'started_at':               'DATETIME',
590
        'ended_at':                 'DATETIME',
591
592
        'success':                  'SMALLINT'
593
    },
594
    'sync.result.error': {
595
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
596
        'result_id':                'INTEGER NOT NULL',
597
        'error_id':                 'INTEGER NOT NULL'
598
    },
599
    'sync.result.exception': {
600
        'id':                       'INTEGER PRIMARY KEY NOT NULL',
601
        'result_id':                'INTEGER NOT NULL',
602
        'exception_id':             'INTEGER NOT NULL'
603
    },
604
}
605