File: models.py

package info (click to toggle)
graphite-web 1.1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,592 kB
  • sloc: javascript: 86,823; python: 11,977; sh: 61; makefile: 50
file content (66 lines) | stat: -rw-r--r-- 1,900 bytes parent folder | download | duplicates (3)
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
import os

from django.db import models

# Monkeypatching so that django-tagging can import python_2_unicode_compatible
import django.utils.encoding
import six
django.utils.encoding.python_2_unicode_compatible = six.python_2_unicode_compatible
django.utils.six = six
from tagging.models import Tag  # noqa: E402

from graphite.events.compat import ModelTaggedItemManager  # noqa: E402

if os.environ.get('READTHEDOCS'):
    TagField = lambda *args, **kwargs: None
else:
    from tagging.fields import TagField


class Event(models.Model):
    when = models.DateTimeField()
    what = models.CharField(max_length=255)
    data = models.TextField(blank=True)
    tags = TagField(default="")

    def get_tags(self):
        return Tag.objects.get_for_object(self)

    def __str__(self):
        return "%s: %s" % (self.when, self.what)

    @staticmethod
    def find_events(time_from=None, time_until=None, tags=None, set_operation=None):

        if tags is not None:
            if set_operation == 'union':
                query = Event.tagged.with_any(tags)
            elif set_operation == 'intersection':
                query = Event.tagged.with_intersection(tags)
            else:
                query = Event.tagged.with_all(tags)
        else:
            query = Event.objects.all()

        if time_from is not None:
            query = query.filter(when__gte=time_from)

        if time_until is not None:
            query = query.filter(when__lte=time_until)

        result = list(query.order_by("when"))
        return result

    def as_dict(self):
        return dict(
            when=self.when,
            what=self.what,
            data=self.data,
            tags=self.tags.split(),
            id=self.id,
        )


# We use this rather than tagging.register() so that tags can be exposed
# in the admin UI
ModelTaggedItemManager().contribute_to_class(Event, 'tagged')