Django-cms: save_model and api.create_page - django-cms

I've an external app with a model like this:
class Society(models.Model):
name = models.CharField(max_length=200)
class Office(models.Model):
society = models.ForeignKey(Society)
name = models.CharField(max_length=200)
I need, everytime that an Office is created and saved, autocreate a page with the name of the office.
I was thinking at a def save-model on the Office(models.Model), in whick insert a cms.api create_page,
but I'm not able to make work it. Anyone could help me?

Like this maybe?
import cms
parent_page = Page.objects.get(pk=1) # you will want something more valid than this
approval_user = User.objects.get(pk=1) # again, just selecting the first user,
# do something better
# like maybe the current request.user?
try:
page = create_page(title=office.name,
template=cms.constants.TEMPLATE_INHERITANCE_MAGIC,
language='en',
parent=parent_page,
published=True) # May not want to auto publish?
except Exception, err:
pass
# need some handling if the creation fails

Related

How to make a button refuse giving you a item the second time you click it

So, i was making a roblox game, and i implemented a shop, but i forgot something, the way how to make it give you an item once a time.
My code is this:
local BtoolGiver = script.Parent
local player = game:GetService("Players")
local valid = game.Workspace.SoundGroup.SoundSFX
local invalid = game.Workspace.SoundGroup.Sounding
script.Parent.MouseButton1Click:Connect(function()
valid.Playing = true
local tool = BtoolGiver.F3X:Clone()
tool.Parent = game.Players.LocalPlayer.Backpack
end)
Can you find a way how to fit the code in it?
Thanks!

Woocommerce Order Export CSV Amendment to code

and a huge thank you to everyone who looks at this in advance from me..
I had some custom code made many moons ago which allowed us to create a new field in a CSV export file, generated by the Woocommerce CSV order export plugin. The field was based on a few rules to export a column called "tax".
It had much more complicated code setup surrounding procedures with Brexit and how we import tax data into Sage 50.
HOWEVER, I have amended the code but I don't really know PHP well. Essentially, I can see the code is trying to set a variable called "tax_code".
WHAT I NEED, is that if field 'shipping_country' equals "GB", then variable "tax_code" should be set in the export to "T1" OTHERWISE, "tax_code" should be "T0".
A snippet of the code is below and I'm pretty sure, if not hopeful, I don't need loads of code rewritten for me, I just need this amended correctly as and IF ELSE statement.
Is there anyone out there to help - much appreciated.
Regards Craig
$tax_code = "";
if (!in_array($order->shipping_country = "GB")) {
$tax_code = "T1";
}
else {
$tax_code = "T0";
}
$order_data['tax'] = $tax_code;
From what I can see the statement is doing the opposite to what you want it to do.
The clause !in_array($order->shipping_country = "GB")) means "not in_array" i.e. evaluates to FALSE. So for GB the else will set the tax_code.
You want to set the parameter $tax_code to T1 if that clause evaluates to TRUE.
If you have been getting the wrong outcome, try removing the exclamation mark so that the clause says in_array($order->shipping_country = "GB")) i.e shipping_country is GB, and you should then see the $tax_code parameter set to T1 as you expect.

How to retrieve EPPO Database info via POST?

I can retrieve EPPO DB info from GET requests.
I am looking for help to retrieve the info from POST requests.
Example code and other info in the linked Rmarkdown HTMP output
As suggested, I have gone trough the https://httr.r-lib.org/ site.
Interesting. I followed the links to https://httr.r-lib.org/articles/api-packages.html and then to https://cdn.zapier.com/storage/learn_ebooks/e06a35cfcf092ec6dd22670383d9fd12.pdf.
I suppose that the arguments for the POST() function should be (more or less) as follows, but yet the response is always 404
url = "https://data.eppo.int/api/rest/1.0/"
config = list(authtoken=my_authtoken)
body = list(intext = "Fraxinus anomala|Tobacco ringspot virus|invalide name|Sequoiadendron giganteum")
encode = "json"
#handle = ???
Created on 2021-04-26 by the reprex package (v0.3.0)
How do I find the missing pieces?
It is a little bit tricky:
You need to use correct url with name of the service from https://data.eppo.int/documentation/rest, e.g. to use Search preferred names from EPPOCodes list:
url = "https://data.eppo.int/api/rest/1.0/tools/codes2prefnames"
Authorization should be passed to body:
body = list(authtoken = "yourtoken", intext = "BEMITA")
So, if you want to check names for two eppocodes: XYLEFA and BEMITA the code should look like:
httr::POST(
url = "https://data.eppo.int/api/rest/1.0/tools/codes2prefnames",
body = list(authtoken = "yourtoken", intext = "BEMITA|XYLEFA")
)
Nonetheless, I would also recommend you to just use the pestr package. However, to find eppocodes it uses SQLite database under the hood instead of EPPO REST API. Since the db is not big itself (circa 40MB) this shouldn't be an issue.
I found the easy way following a suggestion in the DataCamp course:
"To find an R client for an API service search 'CRAN '"
I found the 'pestr' package that gives great access to EPPO database.
I still do not know how to use the POST() function myself. Any hint on that side is warmly welcome.
Here is a solution to loop over names to get EPPO-codes. Whit minor adjustments this also works for distribution and other information in the EPPO DB
# vector of species names
host_name <- c("Fraxinus anomala", "Tobacco ringspot virus", "invalide name",
"Sequoiadendron giganteum")
EPPO_key <- "enter personal key"
# EPPO url
path_eppo_code <- "https://data.eppo.int/api/rest/1.0/tools/names2codes"
# epty list
my_list <- list()
for (i in host_name) {
# POST request on eppo database
response <- httr::POST(path_eppo_code, body=list(authtoken=EPPO_key,
intext=i))
# get EPPO code
pest_eppo_code <- strsplit(httr::content(response)[[1]], ";")[[1]][2]
# add to list
my_list[[i]] <- pest_eppo_code
}
# list to data frame
data <- plyr::ldply(my_list)

Automatically create new objects in a Plone Folder, being the ids only sequential numbers

I have the following structure:
/Plone/folder/year/month/day/id
I want to create the last id sequentially in a tool using invokeFactory. I want to have:
/Plone/folder/2011/06/21/1
/Plone/folder/2011/06/21/2
/Plone/folder/2011/06/21/3
/Plone/folder/2011/06/21/4
Instead of:
/Plone/folder/2011/06/21/id-1
/Plone/folder/2011/06/21/id-2
/Plone/folder/2011/06/21/id-3
/Plone/folder/2011/06/21/id-4
...that is automatically done when I try to create an object with the same name in a folder, Plone handles that for me adding a sequential number. I want an efficient way to create objects, but only using a sequential number instead of a name with a sequential number. I can do that getting the total number os items in a folder, but would like to know if there's a better way.
Real life example: http://plone.org/products/collective.captcha/issues/4
If you're creating these objects manually, you can do something like:
brains = context.getFolderContents({'sort_on' : 'id', 'sort_order' : "reverse"})
if len(brains) > 0:
id = str(int(brains[0].id) + 1)
else:
id = '1'
Then you'll have to create the object manually with that id.
If you want this to do done automatically for you when a user creates content, you might want to look into creating a content rule to change the id of the content for you. An example that might help is collective.contentrules.yearmonth

Visual Basic 2010: How do I reference one of many objects through an additional object? (Pointer-like behaviour?)

I am writing an application in Visual Basic 2010 Express.
I have two objects of a class from a driver DLL that is provided to me. They have some of their own subroutines that I'd like to call, and I'd like an easy way to toggle between them.
Instead of writing a whole bunch of code like this:
selected = x
...
If selected = x then
DriverInstanceX.DoSomething()
Else If Selected = y then
DriverInstanceY.DoSomething()
Endif
I would like to do this:
Bob = (some reference to X - NOT a copy of X!)
...
Bob.DoSomething()
Bob.DoSomethingElse()
I'm sure this is really easy - I am just not sure where to look.
Thanks for any help!
' set the object based on what was selected first, here...
Dim selectedDriverInstance = new DriverObject
' now you can run the method without checking for each as selected was already set.
selectedDriverInstance.DoSometng()
Cool?
Of course, DriverObject can be the instance x or instance y depending on what u set it to, do the assignment there and set it to our fixed name object selectedDriverInstance. this way you can do everything using selectedDriverInstance as it is set to either instance x or instance y already, get me?

Resources