Math Operations on DateField Django - python

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)

Related

instantiate object inside of class

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])

Get artist name

I'm trying to get the names of my top 3 artists of last week with pylast (https://github.com/pylast/pylast) but I run into an error or get I get None as a result and I don't see what I'm doing wrong. pylast is a Python interface to Last.fm.
My code:
import pylast
API_KEY = ""
API_SECRET = ""
username = ""
password_hash = pylast.md5("")
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash)
user = network.get_authenticated_user();
weekly_artists = user.get_weekly_artist_charts();
# Keep the first three artists.
del weekly_artists[3:]
# Print the artist name and number of songs(weight).
for weekly_artist in weekly_artists:
artist,weight = weekly_artist
print (artist.get_name())
print (artist.get_correction())
artist.get_name() returns
None
artist.get_correction() returns
Traceback (most recent call last):
File "G:\projects\python\lastfm_weekly\lastfm-weekly.py", line 28, in <module>
print (artist.get_correction())
File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 1585, in get_correction
self._request(self.ws_prefix + ".getCorrection"), "name")
File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 1029, in _request
return _Request(self.network, method_name, params).execute(cacheable)
File "C:\Users\..\Python\Python36-32\lib\site-packages\pylast\__init__.py", line 744, in __init__
network._get_ws_auth()
AttributeError: 'str' object has no attribute '_get_ws_auth'
What am I doing wrong?
Here is a quick and dirty solution, i'm sure someone will provide something better but i just installed the package to test and it works.
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
artists = network.get_top_artists()
del artists[:3]
for i in artists:
artist, weight = i
print('Artist = {}. Weight = {}'.format(artist, weight))
I'm not really familiar with the package, I just installed it to help out with this but I do wonder what "get_name()" and "get_correction()" are as they're not in your provided code.
If they're not functions you created / are defined within your code then I'd look there for the problem.
Also, you're authenticating the user but the documentation explicitly states you don't need to unless you're writing data.

Django create_or_update model attribute command works fine but update fails

I am trying to write a CSV helper that reads the CSV file and updates or creates fields in the model. The create_or_update query is working fine, but it is only creating not updating. On changing the create_or_update to update it throws an error. The code of the CSV helper is :
def insert_books_from_dictionary(data):
category, created = Category.objects.update_or_create(name=data['category'])
sub_category = None
if data['sub_category'].decode('utf-8') != '':
sub_category, created = SubCategory.objects.update_or_create(name=data['sub_category'], category=category)
publisher, created = Publisher.objects.update_or_create(name=data['publishers'])
# convert strings to float
# ToDo : Fix constraints and create a function
# to handle data check and conversion
try:
price = float(data['mrp_price'])
except ValueError:
price = 0.0
pass
try:
pages = float(data['pages'])
except ValueError:
pages = None
pass
isbn_13_str = str(data['isbn_13'])
isbn_10_str = str(data['isbn_10'])
image_url_str = str(data['image_url'])
binding = 1 if data['binding'] == 'Hardboard' else 2
book, created = Book.objects.update(title=data['title'],description=data['description'], pages=pages, binding=binding, price=price, category=category,sub_category=sub_category, edition_and_year=data['edition_and_year'],
isbn_10=isbn_10_str, isbn_13=isbn_13_str,image_url=image_url_str)
book.publishers.add(publisher)
authors = re.split(",", data['authors'].decode('utf-8'))
for author in authors:
author, created = Author.objects.update_or_create(name=author.strip())
book.authors.add(author)
return dict(book_id=book.id)
It throws the following error:
Traceback (most recent call last):
File "common/util/upload.py", line 18, in <module>
uploadbooks = book_upload(old_file, newfile)
File "common/util/upload.py", line 12, in book_upload
insert_seller_books_from_dictionary(req_data)
File "/home/subhajit/textnook/common/util/csv_helper.py", line 132, in insert_seller_books_from_dictionary
book_id = insert_books_from_dictionary(data)
File "/home/subhajit/textnook/common/util/csv_helper.py", line 167, in insert_books_from_dictionary
isbn_10=isbn_10_str, isbn_13=isbn_13_str,image_url=image_url_str)
TypeError: 'long' object is not iterable
On changing update to create the error is no more. How do i resolve this issue?
in
book, created = Book.objects.update(title=data['title'],description=data['description'], pages=pages, binding=binding, price=price, category=category,sub_category=sub_category, edition_and_year=data['edition_and_year'],
isbn_10=isbn_10_str, isbn_13=isbn_13_str,image_url=image_url_str)
you are using update, not update_or_create
Book.objects.update updates only and returns the number of rows affected, which is the 'long' object is not iterable python complains about, since it has to assign its elements to the tuple book, created

Django models - problem importing

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

NoneType has no attribute Append

I'm new to Python. I can't understand why a variable is None at a certain point in my code:
class UsersInRoom(webapp.RequestHandler):
def get(self):
room_id = self.request.get("room_id")
username = self.request.get("username")
UserInRoom_entities = UserInRoom.gql("WHERE room = :1", room_id).get()
if UserInRoom_entities:
for user_in_room in UserInRoom_entities:
if user_in_room.username == username:
user_in_room.put() # last_poll auto updates to now whenenever user_in_room is saved
else:
user_in_room = UserInRoom()
user_in_room.username = username
user_in_room.put()
// error here, on line 160
UserInRoom_entities = []
UserInRoom_entities.append(user_in_room)
# name is `user_at_room` intead of `user_in_room` to avoid confusion
usernames = [user_at_room.username for user_at_room in UserInRoom_entities]
self.response.out.write(json.dumps(usernames))
The error is:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 507, in __call__
handler.get(*groups)
File "path\to\chat.py", line 160, in get
AttributeError: 'NoneType' object has no attribute 'append'
How is this possible? I'm setting UserInRoom_entities = [] immediately before that call. Or is something else the None in question?
UPDATE: This code works:
class UsersInRoom(webapp.RequestHandler):
def get(self):
room_id = self.request.get("room_id")
username = self.request.get("username")
UserInRoom_entities = UserInRoom.gql("WHERE room = :1", room_id).get()
if UserInRoom_entities:
for user_in_room in UserInRoom_entities:
if user_in_room.name == username:
user_in_room.put() # last_modified auto updates to now whenenever user_in_room is saved
else:
user_in_room = UserInRoom(room=Key(room_id), name=username)
user_in_room.put()
UserInRoom_entities = []
UserInRoom_entities.append(user_in_room)
# name is `user_at_room` intead of `user_in_room` to avoid confusion
usernames = [user_at_room.name for user_at_room in UserInRoom_entities]
self.response.out.write(json.dumps(usernames))
class ChatRoom(db.Model):
name = db.StringProperty()
last_modified = db.DateTimeProperty(auto_now=True)
message_contents = db.StringListProperty()
message_users = db.StringListProperty()
class UserInRoom(db.Model):
room = db.ReferenceProperty(ChatRoom)
name = db.StringProperty()
last_modified = db.DateTimeProperty(auto_now=True)
Since it appears that my comment to the question had the answer to this, I'll repeat it as an answer, with the hope of gaining some reputation points:
Is the UserInRoom instance initialized properly? I am not familiar with the GAE data model, but I could imagine that the put() ing the instance would require that the room attribute was set, if there is a relationship between UserInRoom and Room (assuming a Room class exists).
To make sure that you're not the one raising exception, you can do something like:
UserInRoom_entities = []
# raised? then your .append is missing otherwise something else is wrong
UserInRoom_entities.append
UserInRoom_entities.append(user_in_room)

Categories

Resources