~nmu-sscheel/gtg/rework-task-editor

« back to all changes in this revision

Viewing changes to GTG/core/task.py

  • Committer: Bertrand Rousseau
  • Date: 2012-05-09 22:33:25 UTC
  • mfrom: (1178 trunk)
  • mto: This revision was merged to the branch mainline in revision 1179.
  • Revision ID: bertrand.rousseau@gmail.com-20120509223325-a53d8nwo0x9g93bc
Merge nimit branch and trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
"""
23
23
from datetime         import datetime
24
24
import cgi
25
 
import gobject
26
25
import re
27
26
import uuid
28
27
import xml.dom.minidom
51
50
        self.set_uuid(uuid.uuid4())
52
51
        self.remote_ids = {}
53
52
        self.content = ""
54
 
        #self.content = \
55
 
        #    "<content>Press Escape or close this task to save it</content>"
56
53
        self.title = _("My new task")
57
54
        #available status are: Active - Done - Dismiss - Note
58
55
        self.status = self.STA_ACTIVE
147
144
        due_date = Date.no_date()
148
145
        defer_date = Date.no_date()
149
146
        if text:
150
 
 
151
147
            # Get tags in the title
152
 
            #NOTE: the ?: tells regexp that the first one is
153
 
            # a non-capturing group, so it must not be returned
154
 
            # to findall. http://www.amk.ca/python/howto/regex/regex.html
155
 
            # ~~~~Invernizzi
156
148
            for match in re.findall(r'(?:^|[\s])(@\w+)', text, re.UNICODE):
157
149
                tags.append(match)
158
 
                # Remove the @
159
 
                #text =text.replace(match,match[1:],1)
160
150
            # Get attributes
161
 
            regexp = r'([\s]*)([\w-]+):([^\s]+)'
 
151
            regexp = r'([\s]*)([\w-]+):\s*([^\s]+)'
162
152
            for spaces, attribute, args in re.findall(regexp, text, re.UNICODE):
163
153
                valid_attribute = True
164
 
                if attribute.lower() in ["tags", "tag"] or \
165
 
                   attribute.lower() in [_("tags"), _("tag")]:
 
154
                if attribute.lower() in ["tags", _("tags"), "tag", _("tag")]:
166
155
                    for tag in args.split(","):
167
156
                        if not tag.startswith("@"):
168
157
                            tag = "@" + tag
169
158
                        tags.append(tag)
170
 
                elif attribute.lower() == "defer" or \
171
 
                     attribute.lower() == _("defer"):
 
159
                elif attribute.lower() in ["defer", _("defer"), "start", _("start")]:
172
160
                    try:
173
161
                        defer_date = Date.parse(args)
174
162
                    except ValueError:
182
170
                else:
183
171
                    # attribute is unknown
184
172
                    valid_attribute = False
 
173
 
185
174
                if valid_attribute:
186
 
                    # if the command is valid we have to remove it
187
 
                    # from the task title
 
175
                    # remove valid attribute from the task title
188
176
                    text = \
189
177
                        text.replace("%s%s:%s" % (spaces, attribute, args), "")
190
 
#            # Create the new task
191
 
#            task = self.req.new_task(tags=[t.get_name() for t in tags], newtask=True)
 
178
 
192
179
            for t in tags:
193
180
                self.add_tag(t)
 
181
 
194
182
            if text != "":
195
183
                self.set_title(text.strip())
196
184
                self.set_to_keep()
 
185
 
197
186
            self.set_due_date(due_date)
198
187
            self.set_start_date(defer_date)
199
188
 
256
245
        self.due_date = Date(fulldate)
257
246
        self.sync()
258
247
 
259
 
    #Due date return the most urgent date of all parents
260
248
    def get_due_date(self):
 
249
        """ Due date return the most urgent date of all parents """
261
250
        zedate = self.due_date
262
251
 
263
252
        for par in self.get_parents():
264
 
            #Here we compare with the parent's due date
 
253
            # compare with the parent's due date
265
254
            pardate = self.req.get_task(par).get_due_date()
266
255
            if pardate and zedate > pardate:
267
256
                zedate = pardate
293
282
        return (closed_date - due_date).days
294
283
 
295
284
    def get_text(self):
296
 
        #defensive programmtion to avoid returning None
 
285
        """ Return the content or empty string in case of None """
297
286
        if self.content:
298
287
            return str(self.content)
299
288
        else:
488
477
                priority = "medium"
489
478
        return priority
490
479
 
491
 
#   the following is not currently needed
492
 
#    def modified(self):
493
 
#        TreeNode.modified(self)
494
 
#        for t in self.get_tags():
495
 
#            gobject.idle_add(t.modified)
496
 
 
497
480
    def _modified_update(self):
498
481
        '''
499
482
        Updates the modified timestamp
531
514
        """
532
515
        Adds a tag. Does not add '@tag' to the contents. See add_tag
533
516
        """
534
 
#        print "tag %s added to task %s" %(tagname,self.get_title())
535
517
        t = tagname.encode("UTF-8")
536
518
        #Do not add the same tag twice
537
519
        if not t in self.tags:
538
520
            self.tags.append(t)
539
 
            #we notify the backends
540
 
            #self.req.tag_was_added_to_task(self, tagname)
541
521
            if self.is_loaded():
542
522
                for child in self.get_subtasks():
543
523
                    if child.can_be_deleted:
551
531
 
552
532
    def add_tag(self, tagname):
553
533
        "Add a tag to the task and insert '@tag' into the task's content"
554
 
#        print "add tag %s to task %s" %(tagname,self.get_title())
555
534
        if self.tag_added(tagname):
556
535
            c = self.content
557
536
 
608
587
        return (text
609
588
                    .replace('<tag>%s</tag>\n\n' % (tagname), newtag) #trail \n
610
589
                    .replace('<tag>%s</tag>, ' % (tagname), newtag) #trail comma
 
590
                    .replace('<tag>%s</tag>,' % (tagname), newtag)
611
591
                    .replace('<tag>%s</tag>' % (tagname), newtag)
612
592
                    #in case XML is missing (bug #504899)
613
593
                    .replace('%s\n\n' % (tagname), newtag)
614
594
                    .replace('%s, ' % (tagname), newtag)
 
595
                    .replace('%s,' % (tagname), newtag)
615
596
                    #don't forget a space a the end
616
597
                    .replace('%s ' % (tagname), newtag)
617
598
               )
646
627
        else:
647
628
            #Well, if we don't filter on tags or notag, it's true, of course
648
629
            toreturn = True
649
 
#        print "task %s has tag %s : %s" %(self.get_id(),tag_list,toreturn)
650
630
        return toreturn
651
631
 
652
 
    #return the color of one tag that have a color defined
653
 
    #Yes, the choosen color is a bit random in case of multiple colored tags
654
 
    def get_color(self):
655
 
        color = None
656
 
        for t in self.get_tags():
657
 
            c = t.get_attribute("color")
658
 
            if c:
659
 
                color = c
660
 
        return color
661
 
 
662
632
    def __str__(self):
663
633
        s = ""
664
634
        s = s + "Task Object\n"