Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

# Copyright 2012-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

"""Database models for `lino.modlib.changes`. 

 

It defines the :class:`Change` model, and the functions 

:func:`watch_changes` and :func:`watch_all_changes`.  It also adds a 

menu entry to the `Explorer` menu. 

 

See also :ref:`lino.tutorial.watch`. 

 

""" 

from past.builtins import basestring 

from builtins import object 

 

import logging 

 

logger = logging.getLogger(__name__) 

 

import datetime 

 

from django.conf import settings 

 

from django.db import models 

from django.utils.translation import ugettext_lazy as _ 

from django.utils import timezone 

 

from lino.api import dd 

from lino.core import fields 

 

from lino.core.roles import SiteStaff 

from lino.core.signals import pre_ui_delete, on_ui_created, on_ui_updated 

from lino.core.signals import pre_merge 

from lino.core.signals import pre_add_child, pre_remove_child 

from lino.core.signals import receiver 

 

from lino.modlib.gfks.fields import GenericForeignKey, GenericForeignKeyIdField 

 

 

class ChangeTypes(dd.ChoiceList): 

    """ 

    The list of possible choices for the `type` field 

    of a :class:`Change`. 

    """ 

    # app_label = 'lino' 

    verbose_name = _("Change Type") 

    verbose_name_plural = _("Change Types") 

 

 

add = ChangeTypes.add_item 

add('C', _("Create"), 'create') 

add('U', _("Update"), 'update') 

add('D', _("Delete"), 'delete') 

add('R', _("Remove child"), 'remove_child') 

add('A', _("Add child"), 'add_child') 

add('M', _("Merge"), 'merge') 

 

 

@dd.python_2_unicode_compatible 

class Change(dd.Model): 

    """A registered change in the database. 

 

    Each database change of a watched object will generate one Change 

    record. 

 

    .. attribute:: master 

 

        The database object which acts as "master". 

     

    .. attribute:: object 

 

        The database object which has been modified. 

     

     

    """ 

 

    class Meta(object): 

        verbose_name = _("Change") 

        verbose_name_plural = _("Changes") 

 

    # allow_cascaded_delete = 'master' 

 

    time = models.DateTimeField() 

    type = ChangeTypes.field() 

    if settings.SITE.user_model: 

        user = dd.ForeignKey(settings.SITE.user_model) 

    else: 

        user = dd.DummyField() 

 

    object_type = models.ForeignKey( 

        'contenttypes.ContentType', blank=True, null=True, 

        verbose_name=_("Object type"), 

        related_name='changes_by_object') 

    object_id = GenericForeignKeyIdField( 

        object_type, blank=True, null=True) 

    object = GenericForeignKey('object_type', 'object_id', _("Object")) 

 

    master_type = models.ForeignKey( 

        'contenttypes.ContentType', blank=True, null=True, 

        verbose_name=_("Master type"), related_name='changes_by_master') 

    master_id = GenericForeignKeyIdField( 

        master_type, blank=True, null=True) 

    master = GenericForeignKey('master_type', 'master_id', _("Master")) 

 

    diff = dd.RichTextField(_("Changes"), format='plain', blank=True) 

 

    def __str__(self): 

        # ~ return "#%s - %s" % (self.id,self.time) 

        return "#%s" % self.id 

 

 

class Changes(dd.Table): 

    """The default table for :class:`Change`. 

    """ 

 

    param_object_type = models.ForeignKey( 

        'contenttypes.ContentType', 

        verbose_name=_("Object type"), blank=True) 

    parameters = { 

        'change_type': ChangeTypes.field(force_selection=False, blank=True), 

        'date': models.DateField(_("Only changes from"), blank=True), 

        'object_type': param_object_type, 

        'object_id': models.PositiveIntegerField("Object ID", blank=True), 

    } 

    if settings.SITE.user_model: 

        parameters['user'] = dd.ForeignKey( 

            settings.SITE.user_model, 

            blank=True) 

 

    required_roles = dd.required(SiteStaff) 

 

    editable = False 

    model = 'changes.Change' 

    order_by = ['-time'] 

 

    detail_layout = """ 

        time user type master object id 

        diff 

    """ 

 

    params_layout = """ 

        date user change_type object_type object_id 

    """ 

 

    @classmethod 

    def get_request_queryset(cls, ar): 

        qs = super(Changes, cls).get_request_queryset(ar) 

        if not isinstance(qs, list): 

            if ar.param_values.change_type: 

                qs = qs.filter(type=ar.param_values.change_type) 

            if ar.param_values.date: 

                qs = qs.filter(time__range=( 

                    ar.param_values.date, 

                    ar.param_values.date+datetime.timedelta(1))) 

            if settings.SITE.user_model and ar.param_values.user: 

                qs = qs.filter(user=ar.param_values.user) 

            if ar.param_values.object_type: 

                qs = qs.filter(object_type=ar.param_values.object_type) 

            if ar.param_values.object_id: 

                qs = qs.filter(object_id=ar.param_values.object_id) 

        return qs 

 

 

class ChangesByObject(Changes): 

    """Slave Table showing the direct changes related to the current 

    object. 

 

    """ 

    required_roles = dd.required(SiteStaff) 

    master_key = 'object' 

    column_names = 'time user type master diff master_type master_id' 

 

 

class ChangesByMaster(Changes): 

    """Slave Table showing the changes related to the current object, 

    including those applied to "child" objects. 

 

    """ 

    required_roles = dd.required() 

    master_key = 'master' 

    # column_names = 'time user type object diff object_type object_id' 

    column_names = 'time user type object diff *' 

 

 

class WatcherSpec(object): 

    def __init__(self, ignored_fields, get_master): 

        self.ignored_fields = ignored_fields 

        self.get_master = get_master 

 

 

def watch_all_changes(ignore=[]): 

 

    """Call this to activate change watching on *all* models. The default 

    behaviour is to watch only models that have been explicitly 

    declared using :func:`watch_changes`. 

 

    This is a fallback method and settings passed to specific model 

    using `watch_changes` call takes precedence. 

 

 

    :param ignore: specify list of model names to ignore 

 

    """ 

    watch_all_changes.allow = True 

    watch_all_changes.ignore.extend(ignore) 

 

watch_all_changes.allow = False 

watch_all_changes.ignore = [] 

 

 

def return_self(obj): 

    return obj 

 

 

def watch_changes(model, ignore=[], master_key=None, **options): 

    """Declare the specified model to be "observed" ("watched") for changes. 

    Each change to an object comprising at least one watched field 

    will lead to an entry to the `Changes` table. 

 

    `ignore` should be a string with a space-separated list of field 

    names to be ignored. 

     

    All calls to watch_changes will be grouped by model. 

 

    """ 

    if isinstance(ignore, basestring): 

        ignore = fields.fields_list(model, ignore) 

    if isinstance(master_key, basestring): 

        fld = model.get_data_elem(master_key) 

        if fld is None: 

            raise Exception("No field %r in %s" % (master_key, model)) 

        master_key = fld 

    if isinstance(master_key, fields.RemoteField): 

        get_master = master_key.func 

    elif master_key is None: 

        get_master = return_self 

    else: 

        def get_master(obj): 

            return getattr(obj, master_key.name) 

    ignore = set(ignore) 

    cs = model.change_watcher_spec 

    if cs is not None: 

        ignore |= cs.ignored_fields 

    for f in model._meta.fields: 

        if not f.editable: 

            ignore.add(f.name) 

    model.change_watcher_spec = WatcherSpec(ignore, get_master) 

 

 

def get_change_watcher_spec(obj): 

    cs = obj.change_watcher_spec 

 

    if cs is None: 

        if not watch_all_changes.allow \ 

           or obj.__class__.__name__ in watch_all_changes.ignore: 

            return None 

 

        cs = WatcherSpec([], return_self) 

        obj.change_watcher_spec = cs 

 

    return cs 

 

 

def get_master(obj): 

    cs = get_change_watcher_spec(obj) 

 

    if cs: 

        return cs.get_master(obj) 

 

 

def log_change(type, request, master, obj, msg=''): 

    Change( 

        type=type, 

        time=timezone.now(), 

        user=request.user, 

        master=master, 

        object=obj, 

        diff=msg).save() 

 

 

@receiver(on_ui_updated) 

def on_update(sender=None, watcher=None, request=None, **kw): 

    """ 

    Log a Change if there is a `change_watcher_spec`. 

    """ 

    master = get_master(watcher.watched) 

    if master is None: 

        # No master, nothing to log 

        return 

 

    cs = watcher.watched.change_watcher_spec 

    changes = [] 

    for k, old, new in watcher.get_updates(cs.ignored_fields): 

        changes.append("%s : %s --> %s" % 

                       (k, dd.obj2str(old), dd.obj2str(new))) 

    if len(changes) == 0: 

        msg = '(no changes)' 

    elif len(changes) == 1: 

        msg = changes[0] 

    else: 

        msg = '- ' + ('\n- '.join(changes)) 

    log_change(ChangeTypes.update, request, master, watcher.watched, msg) 

 

 

@receiver(pre_ui_delete) 

def on_delete(sender=None, request=None, **kw): 

    """Calls :func:`log_change` with `ChangeTypes.delete`. 

 

    Note that you must call this before actually deleting the object, 

    otherwise mysql (not sqlite) says ERROR: (1048, "Column 

    'object_id' cannot be null") 

 

    """ 

    master = get_master(sender) 

    if master is None: 

        return 

    log_change(ChangeTypes.delete, request, master, 

               sender, dd.obj2str(sender, True)) 

 

 

@receiver(on_ui_created) 

def on_create(sender=None, request=None, **kw): 

    """To be called when a new instance has actually been created and 

    saved. 

 

    """ 

    master = get_master(sender) 

    if master is None: 

        return 

    log_change(ChangeTypes.create, request, master, 

               sender, dd.obj2str(sender, True)) 

 

 

@receiver(pre_add_child) 

def on_add_child(sender=None, request=None, child=None, **kw): 

    master = get_master(sender) 

    if master is None: 

        return 

    log_change(ChangeTypes.add_child, request, master, 

               sender, dd.full_model_name(child)) 

 

 

@receiver(pre_remove_child) 

def on_remove_child(sender=None, request=None, child=None, **kw): 

    master = get_master(sender) 

    if master is None: 

        return 

    log_change(ChangeTypes.remove_child, request, 

               master, sender, dd.full_model_name(child)) 

 

 

@receiver(pre_merge) 

def on_merge(sender=None, request=None, **kw): 

    """ 

    """ 

    master = get_master(sender.obj) 

    if master is None: 

        return 

    log_change(ChangeTypes.merge, request, 

               master, sender.obj, sender.logmsg())