Learning how to use the deferred library - python

I'm trying code nearly identical to the example from the manual to enable a download counter but I get an exception:
File "/media/Lexar/montao/wwwblob/handler.py", line 117, in FileInfo
download_count = db.IntegerProperty(required=True, count=0) TypeError: init() got an unexpected keyword argument 'count'
Here's the code I try to run:
from google.appengine.ext import deferred
from google.appengine.runtime import DeadlineExceededError
class Mapper(object):
# Subclasses should replace this with a model class (eg, model.Person).
KIND = None
# Subclasses can replace this with a list of (property, value) tuples to filter by.
FILTERS = []
def __init__(self):
self.to_put = []
self.to_delete = []
def map(self, entity):
"""Updates a single entity.
Implementers should return a tuple containing two iterables (to_update, to_delete).
"""
return ([], [])
def finish(self):
"""Called when the mapper has finished, to allow for any final work to be done."""
pass
def get_query(self):
"""Returns a query over the specified kind, with any appropriate filters applied."""
q = self.KIND.all()
for (prop, value) in self.FILTERS:
q.filter('%s =' % prop, value)
q.order('__key__')
return q
def run(self, batch_size=100):
"""Starts the mapper running."""
self._continue(None, batch_size)
def _batch_write(self):
"""Writes updates and deletes entities in a batch."""
if self.to_put:
db.put(self.to_put)
self.to_put = []
if self.to_delete:
db.delete(self.to_delete)
self.to_delete = []
def _continue(self, start_key, batch_size):
q = self.get_query()
# If we're resuming, pick up where we left off last time.
if start_key:
q.filter('__key__ >', start_key)
# Keep updating records until we run out of time.
try:
# Steps over the results, returning each entity and its index.
for (i, entity) in enumerate(q):
(map_updates, map_deletes) = self.map(entity)
self.to_put.extend(map_updates)
self.to_delete.extend(map_deletes)
# Do updates and deletes in batches.
if (i + 1) % batch_size == 0:
self._batch_write()
# Record the last entity we processed.
start_key = entity.key()
self._batch_write()
except DeadlineExceededError:
# Write any unfinished updates to the datastore.
self._batch_write()
# Queue a new task to pick up where we left off.
deferred.defer(self._continue, start_key, batch_size)
return
self.finish()
class FileInfo(db.Model):
blob = blobstore.BlobReferenceProperty(required=True)
download_count = db.IntegerProperty(required=True, count=0)
uploaded_by = db.UserProperty(required=True)
uploaded_at = db.DateTimeProperty(required=True, auto_now_add=True)
class DailyTotal(db.Model):
date = db.DateProperty(required=True, auto_now_add=True)
file_count = db.IntegerProperty(required=True)
download_count = db.IntegerProperty(required=True)
class DownloadCountMapper(Mapper):
KIND = FileInfo
def __init__(self):
self.file_count = 0
self.download_count = 0
def map(self, file):
self.file_count += 1
self.download_count += file.download_count
def finish(self):
total = DailyTotal(file_count=self.file_count,
download_count=self.download_count)
total.put()
Can you tell me what I should do?
Thank you

This line is the culprit:
download_count = db.IntegerProperty(required=True, count=0)
The IntegerProperty constructor doesn't know what to do with count. Maybe you meant this:
download_count = db.IntegerProperty(required=True, default=0)

Related

How do you get value from another class?

I am starting to learn about object-oriented programming and I'm having a rough time with it.
I have some questions about it:
class Record:
"""Represent a record."""
def __init__(self, category, name, value):
self._category = category
self._name = name
self._value = value
# 1. Define the formal parameters so that a Record can be instantiated
# by calling Record('meal', 'breakfast', -50).
# 2. Initialize the attributes from the parameters. The attribute
# names should start with an underscore (e.g. self._amount)
#property
def amount (self):
return self._value
# Define getter methods for each attribute with #property decorator.
# Example usage:
# >>> record = Record('meal', 'breakfast', -50)
# >>> record.amount
# -50
class Records:
"""Maintain a list of all the 'Record's and the initial amount of money."""
def __init__(self):
# 1. Read from 'records.txt' or prompt for initial amount of money.
# 2. Initialize the attributes (self._records and self._initial_money)
# from the file or user input.
self._records =[]
self._money = 0
fh = open(file='records.txt', mode='r')
if ',' not in i:
self._money = int(i.strip())
else:
raw_line = i.strip()
category, activity, value = raw_line.split(',')
x = Record(category, activity, value)
self._records.append(x)
print("Welcome back!")
fh.close()
#property
def view(self):
for i in self._records:
print(Record._category)
#1. Print all the records and report the balance.
x = Records()
Records.view()
I wanted to print out value from my newly acquired list but I don't know how to pass my data from class Records to class Record for processing. How should I approach this?

Read MongoEngine DynamicDocuments

my issue is that I am saving dict objects with MongoEngine:
class MongoRecord(DynamicDocument):
record_id = SequenceField(primary_key = True)
class SimpleMongo(object):
def __init__(self, *args, **kwargs):
"""
Very simple dict-like Mongo interface
"""
if PY_VERSION == 2:
self.iterattr = 'iteritems'
else:
self.iterattr = 'items'
self.debug = DEBUG
self.dict_type = type(dict())
self.dbname = kwargs.get('dbname', 'untitled')
self.collection_name = kwargs.get('collection', 'default')
self.ip = kwargs.get('ip', '127.0.0.1')
self.port = kwargs.get('port', 27017)
self.dbconn = connect(self.dbname, host=self.ip, port=self.port)
drop = kwargs.get('drop', False)
if drop:
self.dbconn.drop_database(self.dbname)
def put(self, data):
"""
Put dict
"""
assert type(data) == self.dict_type
record = MongoRecord()
record.switch_collection(self.collection_name)
generator = getattr(data, self.iterattr)
__res__ = [setattr(record, k, v) for k,v in generator()] # iteritems() for Python 2.x
record.save()
but when trying to access them:
def get(self):
record = MongoRecord()
record.switch_collection(self.collection_name)
return record.objects
getting
mongoengine.queryset.manager.QuerySetManager object, not an iterator.
So, what is the proper way to get my data back from Mongo being saved as DynamicDocument?
The problem isn't that MongoRecordis a DynamicDocument or that it contains a dict. You would get the same result with a regular Document. Your problem is with querying, you should change record.objects to MongoRecord.objects to get a cursor.
Regarding your usage of switch_collection()...
If MongoRecord documents will be saved to a collection with the same name, at most times, you can define this like below, and you don't have to use switch_collection() when a collection with that name is being queried.
class MongoRecord(DynamicDocument):
record_id = SequenceField(primary_key = True)
meta = {'collection': 'records'}
In case you do want to retrieve MongoRecord documents from a collection which isn't called 'records', and you want to define a function for this (which can give an UnboundLocalError), you can do it like this (source):
from mongoengine.queryset import QuerySet
def get(self):
new_group = MongoRecord.switch_collection(MongoRecord(), self.collection_name)
new_objects = QuerySet(MongoRecord, new_group._get_collection())
all = new_objects.all()
# If you would like to filter on an MongoRecord attribute:
filtered = new_objects.filter(record_id=1)
return all

How to Instantiate a Completely Fresh Temporary Object that Contains Lists

I'm currently using Python to parse CAN database files. I ran into a problem with lists during implementation and gave it a quick patch that makes it work, but it's kind of ugly and seems as if there's a more elegant solution.
I have defined an object CAN database and one of it's methods takes the file to be parsed, which contains definitions of messages in the database. I loop through each line in the file and when I come across a line indicating a message description, I create a temporary variable referencing an object I've defined for CAN messages, some of the members of which are lists. I put elements in these lists with a method based on the next handful of lines in the file.
Now when I'm done with this temporary object, I add it to the CAN database object. Since I no longer need the data referenced by this variable, I assign the value None to it and reinstantiate the clean slate variable on the next iteration through that detects a message descriptor. Or that was the plan.
When I go through the next iteration and need to use this variable, I add some values to these lists and find that they're not actually empty. It seems that despite assigning the variable to reference None the values in the lists persisted and were not cleaned up.
Below you can see my solution which was to stack more methods on specifically to get rid of the persisting list elements.
Here's some relevant portions of the file:
Parsing Loop
for line in file:
line = line.rstrip('\n')
line_number += 1 # keep track of the line number for error reporting
if line.startswith("BU_:"):
self._parseTransmittingNodes(line)
elif line.startswith("BO_"):
can_msg = self._parseMessageHeader(line).ResetSignals().ResetAttributes()
building_message = True
elif line.startswith(" SG_") and building_message:
can_msg.AddSignal(self._parseSignalEntry(line))
# can_msg.updateSubscribers()
elif line == "":
if building_message:
building_message = False
self._messages += [can_msg]
can_msg = None
Reset Methods
def ResetSignals(self):
"""
Flushes all the signals from the CANMessage object.
"""
self._signals = []
return self
def ResetAttributes(self):
"""
Flushes all the attributes from the CANMessage object.
"""
self._attributes = []
return self
How can I make this variable a fresh object every time? Should I have a method that clears all of it's internals instead of assigning it None like the IDispose interface in C#?
EDIT: Here's the full source for the CANMessage object:
class CANMessage:
"""
Contains information on a message's ID, length in bytes, transmitting node,
and the signals it contains.
"""
_name = ""
_canID = None
_idType = None
_dlc = 0
_txNode = ""
_comment = ""
_signals = list()
_attributes = list()
_iter_index = 0
_subscribers = list()
def __init__(self, msg_id, msg_name, msg_dlc, msg_tx):
"""
Constructor.
"""
self._canID = msg_id
self._name = msg_name
self._dlc = msg_dlc
self._txNode = msg_tx
def __iter__(self):
"""
Defined to make the object iterable.
"""
self._iter_index = 0
return self
def __next__(self):
"""
Defines the next CANSignal object to be returned in an iteration.
"""
if self._iter_index == len(self._signals):
self._iter_index = 0
raise StopIteration
self._iter_index += 1
return self._signals[self._iter_index-1]
def AddSignal(self, signal):
"""
Takes a CANSignal object and adds it to the list of signals.
"""
self._signals += [signal]
return self
def Signals(self):
"""
Gets the signals in a CANMessage object.
"""
return self._signals
def SetComment(self, comment_str):
"""
Sets the Comment property for the CANMessage.
"""
self._comment = comment_str
return self
def CANID(self):
"""
Gets the message's CAN ID.
"""
return self._canID
def AddValue(self, value_tuple):
"""
Adds a enumerated value mapping to the appropriate signal.
"""
for signal in self:
if signal.Name() == value_tuple[0]:
signal.SetValues(value_tuple[2])
break
return self
def AddAttribute(self, attr_tuple):
"""
Adds an attribute to the message.
"""
self._attributes.append(attr_tuple)
return self
def ResetSignals(self):
"""
Flushes all the signals from the CANMessage object.
"""
self._signals = []
return self
def ResetAttributes(self):
"""
Flushes all the attributes from the CANMessage object.
"""
self._attributes = []
return self
def Name(self):
return self._name
def TransmittingNode(self):
return self._txNode
def DLC(self):
return self._dlc
The problem you're seeing is because you used class attributes instead of instance attributes. If you move the initialization of the attributes you don't pass to __init__ from class scope into __init__, each instance will have its own set of lists.
Here's what that would look like:
class CANMessage:
"""
Contains information on a message's ID, length in bytes, transmitting node,
and the signals it contains.
"""
def __init__(self, msg_id, msg_name, msg_dlc, msg_tx):
"""
Constructor.
"""
self._canID = msg_id
self._name = msg_name
self._dlc = msg_dlc
self._txNode = msg_tx
self._name = ""
self._canID = None
self._idType = None
self._dlc = 0
self._txNode = ""
self._comment = ""
self._signals = list()
self._attributes = list()
self._iter_index = 0
self._subscribers = list()
# the rest of the class is unchanged, and not repeated here...

Can anybody help me with this python TypeError

As a new learner's question,this question may be very simple, but I have tried for many hours and cannot find the answer, sorry.
this is the a problem form edx's python course.
I will paste all of the code, But I have finished most of it and get correct。 The Part 4 is which I am going on.
first, the file's content is:
# subject trigger named t1
t1 SUBJECT world
# title trigger named t2
t2 TITLE Intel
# phrase trigger named t3
t3 PHRASE New York City
# composite trigger named t4
t4 AND t2 t3
# the trigger set contains t1 and t4
ADD t1 t4
,
import feedparser
import string
import time
from project_util import translate_html
from Tkinter import *
#======================
# Code for retrieving and parsing RSS feeds
#======================
def process(url):
"""
Fetches news items from the rss url and parses them.
Returns a list of NewsStory-s.
"""
feed = feedparser.parse(url)
entries = feed.entries
ret = []
for entry in entries:
guid = entry.guid
title = translate_html(entry.title)
link = entry.link
summary = translate_html(entry.summary)
try:
subject = translate_html(entry.tags[0]['term'])
except AttributeError:
subject = ""
newsStory = NewsStory(guid, title, subject, summary, link)
ret.append(newsStory)
return ret
#======================
# Part 1
# Data structure design
#======================
class NewsStory(object):
def __init__(self, guid, title, subject, summary, link):
self.guid = guid
self.title = title
self.subject = subject
self.summary = summary
self.link = link
def getGuid(self):
return self.guid
def getTitle(self):
return self.title
def getSubject(self):
return self.subject
def getSummary(self):
return self.summary
def getLink(self):
return self.link
#======================
# Part 2
# Triggers
#======================
class Trigger(object):
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
raise NotImplementedError
# Whole Word Triggers
class WordTrigger(Trigger):
def __init__(self, word):
self.word = word
def changeText(self, text):
for i in string.punctuation:
text = text.replace(i, ' ')
return text
def isWordIn(self, text):
return self.word.upper in self.changeText(self, text.upper()).split()
class TitleTrigger(WordTrigger):
def evaluate(self, story):
return self.isWordIn(self, story.getTitle())
class SubjectTrigger(WordTrigger):
def evaluate(self, story):
return self.isWordIn(self, story.getSubject())
class SummaryTrigger(WordTrigger):
def evaluate(self, story):
return self.isWordIn(self, story.getSummary())
# Composite Triggers
class NotTrigger(Trigger):
def __init__(self, trigger):
self.trigger = trigger
def evaluate(self,story):
return not self.trigger.evaluate(story)
class AndTrigger(Trigger):
def __init__(self, trigger1, trigger2):
self.trigger1 = trigger1
self.trigger2 = trigger2
def evaluate(self, story):
return self.trigger1.evaluate(story) and self.trigger2.evaluate(story)
class OrTrigger(Trigger):
def __init__(self, trigger1, trigger2):
self.trigger1 = trigger1
self.trigger2 = trigger2
def evaluate(self, story):
return self.trigger1.evaluate(story) or self.trigger2.evaluate(story)
# Phrase Trigger
class PhraseTrigger(Trigger):
def __init__(self, phrase):
self.phrase = phrase
def evaluate(self, story):
return self.phrase in story.getSubject() or self.phrase in story.\
getSummary() or self.phrase in story.getTitle()
#======================
# Part 3
# Filtering
#======================
def filterStories(stories, triggerlist):
"""
Takes in a list of NewsStory instances.
Returns: a list of only the stories for which a trigger in triggerlist fires.
"""
temp = stories[:]
for i in stories:
for j in triggerlist:
if (not j.evaluate(i)) and j == triggerlist[-1]:
temp.remove(i)
elif j.evaluate(i):
break
stories = temp[:]
return stories
This is what I have done and get correct. because the next function needs TitleTrigger,SubjectTrigger,SummaryTrigger,NotTrigger,AndTrigger,PhraseTrigger,OrTrigger, and all the triggers need the NewsStory, so I keep them.
#======================
# Part 4
# User-Specified Triggers
#======================
def makeTrigger(triggerMap, triggerType, params, name):
"""
Takes in a map of names to trigger instance, the type of trigger to make,
and the list of parameters to the constructor, and adds a new trigger
to the trigger map dictionary.
triggerMap: dictionary with names as keys (strings) and triggers as values
triggerType: string indicating the type of trigger to make (ex: "TITLE")
params: list of strings with the inputs to the trigger constructor (ex: ["world"])
name: a string representing the name of the new trigger (ex: "t1")
Modifies triggerMap, adding a new key-value pair for this trigger.
Returns a new instance of a trigger (ex: TitleTrigger, AndTrigger).
"""
if triggerType == 'TITLE':
triggerMap[name] = TitleTrigger
if triggerType == 'SUBJECT':
triggerMap[name] = SubjectTrigger
if triggerType == 'PHRASE':
triggerMap[name] = PhraseTrigger
if triggerType == 'SUMMARY':
triggerMap[name] = SummaryTrigger
if triggerType == 'AND':
triggerMap[name] = AndTrigger
if triggerType == 'OR':
triggerMap[name] = OrTrigger
if triggerType == 'NOT':
triggerMap[name] = NotTrigger
if triggerType == 'AND' or triggerType == 'OR':
tempt = triggerMap[name](triggerMap[params[0]],triggerMap[params[1]])
elif triggerType == 'NOT':
tempt = NotTrigger(triggerMap[params[0]])
else:
params = ' '.join(params)
tempt = triggerMap[name](params)
return tempt
def readTriggerConfig(filename):
triggerfile = open(filename, "r")
all = [ line.rstrip() for line in triggerfile.readlines() ]
lines = []
for line in all:
if len(line) == 0 or line[0] == '#':
continue
lines.append(line)
triggers = []
triggerMap = {}
for line in lines:
linesplit = line.split(" ")
# Making a new trigger
if linesplit[0] != "ADD":
trigger = makeTrigger(triggerMap, linesplit[1],
linesplit[2:], linesplit[0])
# Add the triggers to the list
else:
for name in linesplit[1:]:
triggers.append(triggerMap[name])
return triggers
This is the part I am working on, readTriggerConfig is given by teacher.
import thread
SLEEPTIME = 60 #seconds -- how often we poll
def main_thread(master):
# A sample trigger list - you'll replace
# this with something more configurable in Problem 11
try:
# TODO: Problem 11
# After implementing makeTrigger, uncomment the line below:
triggerlist = readTriggerConfig("triggers.txt")
# **** from here down is about drawing ****
frame = Frame(master)
frame.pack(side=BOTTOM)
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT,fill=Y)
t = "Google & Yahoo Top News"
title = StringVar()
title.set(t)
ttl = Label(master, textvariable=title, font=("Helvetica", 18))
ttl.pack(side=TOP)
cont = Text(master, font=("Helvetica",14), yscrollcommand=scrollbar.set)
cont.pack(side=BOTTOM)
cont.tag_config("title", justify='center')
button = Button(frame, text="Exit", command=root.destroy)
button.pack(side=BOTTOM)
# Gather stories
guidShown = []
def get_cont(newstory):
if newstory.getGuid() not in guidShown:
cont.insert(END, newstory.getTitle()+"\n", "title")
cont.insert(END, "\n---------------------------------------------------------------\n", "title")
cont.insert(END, newstory.getSummary())
cont.insert(END, "\n*********************************************************************\n", "title")
guidShown.append(newstory.getGuid())
while True:
print "Polling . . .",
# Get stories from Google's Top Stories RSS news feed
stories = process("http://news.google.com/?output=rss")
# Get stories from Yahoo's Top Stories RSS news feed
stories.extend(process("http://rss.news.yahoo.com/rss/topstories"))
# Process the stories
stories = filterStories(stories, triggerlist)
map(get_cont, stories)
scrollbar.config(command=cont.yview)
print "Sleeping..."
time.sleep(SLEEPTIME)
except Exception as e:
print e
if __name__ == '__main__':
root = Tk()
root.title("Some RSS parser")
thread.start_new_thread(main_thread, (root,))
root.mainloop()
the last part is given by teacher.thanks
I add a method named getWord in the SubjectTrigger,TitleTrigger,PhraseTrigger,and AndTrigger to get the word, phrase to see whether the tempt is right or not, and I do this before return:
print tempt
print tempt.getWord()
return tempt
And it gives me:
<__main__.SubjectTrigger object at 0x000000000849B898>
world
<__main__.TitleTrigger object at 0x000000000849B8D0>
Intel
<__main__.PhraseTrigger object at 0x000000000849B898>
New York City
<__main__.AndTrigger object at 0x000000000849B8D0>
(<class '__main__.TitleTrigger'>, <class '__main__.PhraseTrigger'>)
Polling . . .
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\python\ProblemSet7\ps7.py", line 292, in main_thread
stories = filterStories(stories, triggerlist)
File "C:\Users\Administrator\Desktop\python\ProblemSet7\ps7.py", line 161, in filterStories
if (not j.evaluate(i)) and j == triggerlist[-1]:
TypeError: unbound method evaluate() must be called with SubjectTrigger instance as first argument (got NewsStory instance instead)
It seems like I have gotten the right tempt, but with some error in the line 161
At least part of the problem is this line of code:
WordTrigger.changeText(self, text.upper()).split()
You've defined changeText as an instance method, but you are calling it on the class itself. Try changing it to this:
self.changeText(text.upper()).split()
You'll also need to make similar transformations to the other subclasses of WordTrigger.
Your first problem is apparently an instance creation with an unexpected number of arguments, but the second problem is that you're discarding the traceback that would tell you precisely where. In your code:
def main_thread(master):
# A sample trigger list - you'll replace
# this with something more configurable in Problem 11
try:
pass # Lots of code here
except Exception as e:
print e
You catch any exception, and print only the exception. Normal behaviour would print a full traceback of where it occurred. Either remove this try-except clause entirely, or use the traceback module to restore the more informative message:
except:
import traceback
traceback.print_exc()
On further reading, it appears your core problems are in makeTrigger, specifically how it uses different types in triggerMap. It attempts to first assign a class into the map, then use that class to create an instance, but the logic is flawed:
if triggerType == 'AND' or triggerType == 'OR':
tempt = triggerMap[name](triggerMap[params[0]],triggerMap[params[1]])
if triggerType == 'NOT':
tempt = NotTrigger(triggerMap[params[0]])
else:
params = ' '.join(params)
tempt = triggerMap[name](params)
Firstly, as you have found, the first if is distinct from the later ones; this block is not three branches but two branching paths in sequence, easily fixed by using elif. Secondly, triggerMap[name] never gets set to the created instances. This breaks the assumption made in the logic triggers, where triggerMap[params[n]] are expected to be trigger instances, not classes. Trying to evaluate a triggerMap entry led to the unbound method call. One way to fix this is simply triggerMap[name] = tempt.
We could also reduce the special cases quite a bit; I would consider replacing the triggerType ifs with a dictionary, and perhaps using subclasses to choose between the parameter types (triggers, words or phrases).

How to avoid date overlaps in Django models?

I have a model:
class Dimension_Item(models.Model):
uq_dimension_item_id = MyCharField(max_length=1024, primary_key=True)
dimension_id = MyCharField(max_length=1024)
dimension_item_id = MyCharField(max_length=100)
name = MyCharField(max_length=255)
description = MyCharField(max_length=512, null = True)
start_date = models.DateField(default=date(1,1,1))
end_date = models.DateField(default=date(9999,12,31))
function for adding information to the model:
def addRows(in_args):
rq = in_args[0]
pk_l=[]
rows = rq['rows']
if type(rows).__name__ == 'dict':
dim = Dimension_Item(
name=rows['name'],
start_date=rows['start_date'],
end_date=rows['end_date']
)
dim.save()
pk_l.append(dim.dimension_id)
elif type(rows).__name__ == 'list':
for i in rows:
dim = Dimension_Item(
name=rows['name'],
start_date=rows['start_date'],
end_date=rows['end_date']
)
dim.save()
pk_l.append(dim.dimension_id)
else:
pass
return getRows(in_args, pk_l)
# return "success add row"
clauses.addMethod(addRows)
and function for modifying the model items:
def modifyRows(in_args):
pk_l=[]
rq = in_args[0]
rows = rq['rows']
if type(rows).__name__ == 'dict':
dim = Dimension_Item.objects.get(pk=rows['pk'])
for attr in rows:
if attr!='pk':
try:
setattr(dim, attr, rows[attr])
except KeyError:
pass
dim.save()
pk_l.append(dim.dimension_id)
elif type(rows).__name__ == 'list':
for i in rows:
dim = Dimension_Item.objects.get(pk=i['pk'])
for attr in i:
if i!='pk':
try:
setattr(dim, attr, i[attr])
except KeyError:
pass
dim.save()
pk_l.append(dim.dimension_id)
else:
pass
return getRows(in_args, pk_l)
# return "success modify"
clauses.addMethod(modifyRows)
I should check if start_date and end_date fields don't overlap other records in the database.
For example, I enter: 2.02.1988-3.07.1989. And if I already have the record with 2.07.1989 - 3.08.1990, I have to throw an exception about date overlapping.
How better can I do it?
I would override the save() method of your Dimension_Item model.
In your custom save() method you can implement your check for overlapping dates. If everything is OK, create the object. If not, just return nothing (or raise and error.)
The Django documentation explains it really good: https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods
Here is some (untested) code to get you started:
class Dimension_Item(models.Model):
start_date = models.DateField(default=date(1,1,1))
end_date = models.DateField(default=date(9999,12,31))
def save(self, *args, **kwargs):
# get number of items that have an overlapping start date
dimension_items_overlapping_start = Dimension_Item.objects.filter(start_date__gte=self.start_date, start_date__lte=self.end_date).count()
# get number of items that have an overlapping end date
dimension_items_overlapping_end = Dimension_Item.objects.filter(end_date__gte=self.start_date, end_date__lte=self.end_date).count()
overlapping_dimension_items_present = dimension_items_overlapping_start > 0 or dimension_items_overlapping_end > 0
if overlapping_dimension_items_present:
return
else:
super(Dimension_Item, self).save(*args, **kwargs) # Call the "real" save() method.
class Dimension_Item(models.Model):
start_date = models.DateField(default=date(1,1,1))
end_date = models.DateField(default=date(9999,12,31))
def save(self, *args, **kwargs):
# check for items that have an overlapping start date
dimension_items_overlapping_start = Dimension_Item.objects.filter(start_date__gte=self.start_date, start_date__lte=self.end_date).exists()
# check for items that have an overlapping end date
dimension_items_overlapping_end = Dimension_Item.objects.filter(end_date__gte=self.start_date, end_date__lte=self.end_date).exists()
# check for items that envelope this item
dimension_items_enveloping = Dimension_Item.objects.filter(start_date__lte=self.start_date, end_date__gte=self.end_date).exists()
overlapping_dimension_items_present = dimension_items_overlapping_start or dimension_items_overlapping_end or dimenstion_items_enveloping
if overlapping_dimension_items_present:
return
else:
super(Dimension_Item, self).save(*args, **kwargs) # Call the "real" save() method.

Categories