I've refactored my models files into a module - this way it's much easier to maintain the code since it has grown quite a bit.
The funny thing is though that it won't work for one of the classes that references another class that references the fist one in it's turn:
UPD: the cycling references are confusing python and this is what the problem is caused by. This is easy to fix when you only reference other models from your model definition. However, Picture has methods that reference paperType class and vice versa - how can this be fixed?
Here's class Picture:
from django.db import models
from django.utils import simplejson
from picviewer.models import Collection, ImageSizeRatio, printSize
class Picture(models.Model):
name = models.TextField(null=False,blank=False,unique=False)
collection = models.ForeignKey(Collection)
popularity = models.IntegerField(default=0,unique=False)
isPurchasable = models.BooleanField(default=False)
allowBuyExclusive = models.BooleanField(default=False)
basePrice = models.DecimalField(decimal_places=2,max_digits=8)
imageSizeRatio = models.ForeignKey(ImageSizeRatio)
imageThumbnail = models.FileField(upload_to='pictures')
imagePreview = models.FileField(upload_to='pictures')
imageSmall = models.FileField(upload_to='pictures')
imageNormal = models.FileField(upload_to='pictures')
imageLarge = models.FileField(upload_to='pictures')
imageHuge = models.FileField(upload_to='pictures')
allowedPrintSize = models.ManyToManyField(printSize)
Here is printSize class that it references - you see it calls Picture functions to do some math around pictures of specified printSize:
from django.db import models
from picviewer.models import paperType
from picviewer.models import Picture
class printSize (models.Model):
name = models.CharField(null=False,blank=False,unique=True,max_length=60)
width = models.IntegerField(null=False,blank=False)
height = models.IntegerField(null=False,blank=False)
allowedPaperType = models.ManyToManyField(paperType)
#isActive = models.NullBooleanField(null=True, default=None)
def json(self, picture_id, base_price):
sizes_selector = printSize.objects.filter(picture__id = picture_id)
sizes = list()
for size in sizes_selector:
papers = list()
for paper in size.allowedPaperType.all():
cost_for_paper = Picture.objects.get(id=picture_id).calculatePrice(paper.id,size.id)
p = dict(id = paper.id,
name = paper.name,
description = paper.description,
price = paper.pricePerSqMeter.__str__(),
cost = "%.2f" % cost_for_paper)
papers.append(p)
s = dict(id = size.id,
name = size.name,
width = size.width,
height = size.height,
allowedPapers = papers)
sizes.append(s)
return sizes
now this is what I get in shell trying to import Picture:
>>> from picviewer.models import Picture
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\~Sasha\eclipse_workspace\zavalen\picviewer\models\Picture.py", line 4, in <module>
from picviewer.models import Collection, ImageSizeRatio, printSize
File "D:\~Sasha\eclipse_workspace\zavalen\picviewer\models\printSize.py", line 4, in <module>
from picviewer.models import Picture
ImportError: cannot import name Picture
>>>
can I cure this? :)
To avoid cyclic imports, specify FK model as a string, e.g
collection = models.ForeignKey('Collection') # Collection is in the same module
or
collection = models.ForeignKey('myapp.Collection') # Collection is in another app
Related
When someone makes an entry, the date and time will be recorded as expected. What I'm curious about is how to do math operations on that date. Like I want to show the date of when the object was created, and also the date 2 weeks in the future.
models.py
from django.db import models
class Checkout(models.Model):
member_id = models.IntegerField(null = True)
title = models.CharField(max_length = 1000)
date_checkout = models.DateField(auto_now = True)
# expected_return = date_checkout * 2
I thought I had stumbled across a useful resource online that mentioned something about this but I can't find it anymore. If you could just point me to some resources online that would be awesome.
you can provide a callable as a default and then store that result (assuming you actually want to store the expected_return (ie for queries) )
class Checkout(models.Model):
member_id = models.IntegerField(null = True)
title = models.CharField(max_length = 1000)
date_checkout = models.DateField(auto_now = True)
expected_return = models.DateField(default=lambda:datetime.datetime.now()+datetime.timedelta(days=14))
Checkout.objects.filter(expeded_return__lt=datetime.datetime.now())
checked_out_item.expected_return
if you want it to be an actual field (ie it can be queried Checkout.objects.filter(expeded_return__lt=datetime.datetime.now())
if you just want an easy way to access then properties on the class can make functions look like attributes
class Checkout(models.Model):
member_id = models.IntegerField(null = True)
title = models.CharField(max_length = 1000)
date_checkout = models.DateField(auto_now = True)
#property
def expected_return(self):
return self.date_checkout + datetime.timedelta(days=14)
print(checked_out.expected_return)
if you go with the second option you will need to make sure you only use it AFTER the checkout date is set
from demo_app.models import Checkout
c = Checkout(member_id=1,title="asd")
c.expected_return # since there is no date_checkout we get error
Traceback (most recent call last):
File "C:\Users\joran\AppData\Local\Programs\Python\Python37-32\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-4687ba4c4a16>", line 1, in <module>
c.expected_return # since there is no date_checkout we get error
File "D:\demo_django\demo_app\models.py", line 11, in expected_return
return self.date_checkout + datetime.timedelta(days=14)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'
c.save()
c.expected_return # since there is now a checkout it works
Out[6]: datetime.date(2019, 12, 14)
import datetime
c = Checkout(member_id=1,title="asd",date_checkout=datetime.datetime.now())
c.expected_return # since we set the date_checkout it will work even though its not saved
Out[10]: datetime.datetime(2019, 12, 14, 13, 36, 21, 270897)
I am creating a universal text field that can be used in many python turtle projects. I am trying to create an instance of it but I get this error:
>>> import TextField
>>> tf = TextField('None', False)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
tf = TextField('None', False)
TypeError: 'module' object is not callable
>>>
What in a module causes this type of error? I completely wrote this module and I'm getting an error creating an instance of it :( ... What do I need in this module to make it 'callable'? I have tried adding a def __call__(self): but that doesn't affect the problem at all, nor create any errors.
Here is the beginning of the script where the problem is most likely happening:
# Created by SUPERMECHM500 # repl.it
# Edited by cdlane # stackoverflow.com
class TextField:
TextFieldBorderColor = '#0019fc'
TextFieldBGColor = '#000000'
TextFieldTextColor = '#ffffff'
ShiftedDigits = {
'1':'!',
'2':'#',
'3':'#',
'4':'$',
'5':'%',
'6':'^',
'7':'&',
'8':'*',
'9':'(',
'0':')'
}
def __init__(self, command, CanBeEmpty): # Ex. textField = TextField('Execute()', True)
self.CmdOnEnter = command
self.turtle = Turtle()
self.CanBeEmpty = CanBeEmpty
self.turtle.speed('fastest')
self.inp = []
self.FullOutput = ""
self.TextSeparation = 7
self.s = self.TextSeparation
self.key_shiftL = False
......
The module is not the class. If your class TextField is in a module called TextField, then it is referred to as TextField.TextField.
Or change your import to
from TextField import TextField
Im trying to understand why I cannot access the methods on an object that is instantiated inside of a class. For example i'm attempting to build a script that utilizes the python-pptx library and I want to wrap the entire slide creation within a class to abstract it and make it a bit more reusable based on my configuration.
class Builder():
def __init__(self, template='template.pptx', output_file='out.pptx'):
self.cust_name = ''
self.author = ''
self.job_title = ''
self.present_date = ''
self.assessment_type = ''
self.template = template
self.agenda = ['Overview','Resources']
self.outfile = output_file
self.prs = Presentation('template.pptx') <--- This is what im referring to.
def addAgendaSlide(self):
agenda_slide = self.prs.add_slide(self.prs.slide_layouts[AGENDA]) <-- When trying to access this
agenda_slide.shapes.title.text = 'Agenda'
agenda_slide.placeholders[10].text = 'A test Agenda slide'
agenda_slide.placeholders[15].top = STANDARD_TOP
agenda_slide.placeholders[15].left = STANDARD_LEFT
agenda_slide.placeholders[15].width = 8229600
agenda_slide.placeholders[15].height = 4572000
for para in self.agenda:
p = agenda_slide.placeholders[15].text_frame.add_paragraph()
p.text = para
Traceback (most recent call last):
File "test.py", line 19, in <module>
test.addAgendaSlide()
File "/dev/pythonpptx/DocMaker/Slides.py", line 89, in addAgendaSlide
agenda_slide = self.prs.add_slide(self.prs.slide_layouts[AGENDA])
AttributeError: 'Presentation' object has no attribute 'add_slide'
If I use the same bits of code outside the class it works fine. I do have other methods in the class that are fine, it seems to be my implementation of the Presentation() bit that is messing me up.
The following works fine:
prs = Presentation('template.pptx')
agenda_slide = prs.slides.add_slide(prs.slide_layouts[AGENDA])
agenda_slide.shapes.title.text = 'Agenda'
agenda_slide.placeholders[15].top = STANDARD_TOP
agenda_slide.placeholders[15].left = STANDARD_LEFT
agenda_slide.placeholders[15].width = 8229600
agenda_slide.placeholders[15].height = 4572000
prs.save('out.pptx')
I think your problem is you are forgetting to add slides as follows:
agenda_slide = self.prs.slides.add_slide(self.prs.slide_layouts[AGENDA])
instead of
agenda_slide = self.prs.add_slide(self.prs.slide_layouts[AGENDA])
import rhinoscriptsyntax as rs
import scriptcontext
import Rhino
if ACTIVE:
for i in range(len(GEO)):
scriptcontext.doc = ghdoc
GEO_id = GEO[i]
doc_object = rs.coercerhinoobject(GEO_id)
attributes = doc_object.Attributes
geometry = doc_object.Geometry
scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc
rhino_obj = scriptcontext.doc.Objects.Add(geometry, attributes)
rs.ObjectColor(rhino_obj, color[i])
mat_id = rs.AddMaterialToObject(rhino_obj)
rs.MaterialColor(mat_id, color[i])
"Runtime error (TypeErrorException): 'Color' object is unsubscriptable
Traceback:
line 23, in script"
I have no idea why the "ObjectColor" is unscriptable, I have matched it with the python API so many times. AM I not seeing something that I should?
A number of people in my organization have different email names from perforce names, so I need to create an IEmailLookup derivation that overrides getAddress to do my evil bidding:
(From my master.cfg)
class MyIEmailLookup:
from buildbot import interfaces
__implements__ = interfaces.IEmailLookup
def getAddresses(user):
address_dict = {"user1", "user_one#our_domain.com"}
try:
address = address_dict[user]
except KeyError:
address = user + "#our_domain.com"
return address
maillookup = MyIEmailLookup()
from buildbot.status import mail
c['status'].append(mail.MailNotifier(....
....
lookup=maillookup
))
I've tried any number of permutations, but I either get:
Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/scripts/runner.py", line 1071, in doCheckConfig
ConfigLoader(configFileName=configFileName)
File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/scripts/checkconfig.py", line 46, in __init__
self.loadConfig(configFile, check_synchronously_only=True)
File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/master.py", line 727, in loadConfig
exec f in localDict
File "/Users/playbuilder/buildbot/master.cfg", line 207, in <module>
lookup=maillookup
File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/status/mail.py", line 293, in __init__
assert interfaces.IEmailLookup.providedBy(lookup)
AssertionError
...or any other number of issues, dependant upon how I try to implement the IEmailLookup interface.
I'm using buildbot 0.8.3p1 and python 2.6.1.
I see precious few examples of how to do this, and every one of them fails in my context. What am I missing here?
I just solved this problem myself.
First you need to add (somewhere at the top of the file)
from zope.interface import implements
and then change
__implements__ = interfaces.IEmailLookup
to
if implements:
implements( interfaces.IEmailLookup )
else:
__implements__ = interfaces.IEmailLookup
If you want to fetch email from perforce user, you can use this class:
# .-----------------------.
# | Perforce Email Lookup |
# `-----------------------'
from twisted.internet import defer, utils
from buildbot.util import ComparableMixin
from buildbot.interfaces import IEmailLookup
from zope.interface import implements
import os
import re
class PerforceEmailLookup(ComparableMixin):
implements(IEmailLookup)
compare_attrs = ["p4port", "p4user", "p4passwd", "p4bin"]
env_vars = ["P4CLIENT", "P4PORT", "P4PASSWD", "P4USER",
"P4CHARSET"]
def __init__(self,
p4port = None,
p4user = None,
p4passwd = None,
p4bin = 'p4'):
self.p4port = p4port
self.p4user = p4user
self.p4passwd = p4passwd
self.p4bin = p4bin
self.email_re = re.compile(r"Email:\s+(?P<email>\S+#\S+)\s*$")
def _get_process_output(self, args):
env = dict([(e, os.environ.get(e)) for e in self.env_vars if os.environ.get(e)])
d = utils.getProcessOutput(self.p4bin, args, env)
return d
#defer.deferredGenerator
def getAddress(self, name):
if '#' in name:
yield name
return
args = []
if self.p4port:
args.extend(['-p', self.p4port])
if self.p4user:
args.extend(['-u', self.p4user])
if self.p4passwd:
args.extend(['-P', self.p4passwd])
args.extend(['user', '-o', name])
wfd = defer.waitForDeferred(self._get_process_output(args))
yield wfd
result = wfd.getResult()
for line in result.split('\n'):
line = line.strip()
if not line: continue
m = self.email_re.match(line)
if m:
yield m.group('email')
return
yield name
usage would look like:
c['status'].append(
MailNotifier(
sendToInterestedUsers = True,
mode = 'failing',
lookup = PerforceEmailLookup(
p4port = "perforce:1666",
p4user = "buildbot",
p4passwd = "buildbot")))
Try this:
from buildbot.interfaces import IEmailLookup
from buildbot.util import ComparableMixin
from zope.interface import implements
class lookup_example_email(ComparableMixin):
implements(IEmailLookup)
def getAddress(self,user):
return "%s#example.com"%(user)
...
mn = MailNotifier(..., lookup=lookup_example_email(), extraRecipients=m)
Here's the piece of code I use which works with buildbot 2.3.1 in python3.6.
from buildbot.interfaces import IEmailLookup
from buildbot.util import ComparableMixin
from zope.interface import implementer
#implementer(IEmailLookup)
class EmailMap(ComparableMixin):
def getAddress(self, name):
return f'{name}#xxxxx'