Using the Django scheduler app with your own models - python

Using the https://github.com/llazzaro/django-scheduler I'd like to use my own models in the calendar, they also have a start and end date.
I think there are multiple ways to solve this problem:
Hack the current schedule app to make it interact with my models.
Creating default event models when creating my models, using the save() override.
Use the "relations of events to generic objects" feature of the django-scheduler app.
Extend the default event models to meet my own requirements.
I would like to use the third option but I wouldn't know how to use it since a calendar is linked to a single object.
I'm new to both Python and Django, so could someone give me advice?

To achieve option 3, your generic object would have a foreign key linking to an Event object from that calendar app.

Django Scheduler has a quite hidden setting (not even reported in official docs) which can do the trick:
SCHEDULER_BASE_CLASSES
SCHEDULER_BASE_CLASSES = {
'Event': ['my_app.models.EventAbstract1', 'my_app.models.EventAbstract2']
'Calendar': [my_app.models.CalendarAbstract']
}
So, you can define your own abstract model and make Calendar extend it.
EDIT
As #Jheasly said in his comment, this feature is now documented.

Related

Using Django admin as a base for backend

First sorry for my poor English.
I came from asp.net mvc. Now I use django with nanoboxio
In asp, I can create sections like below.
Create model and add it to dbcontext
Right click controller folder and create new controller with views.
Modify as you wish
For now, I know Django can create admin interface for your model. I try and happy with it. but i want to develop it.
I want to for example;
Create a post model.
Create a admin interface for it.
Copy generated admin interface controller and views to another app
Modify it
How can I do that?
Django's MVC is quite different to ASP.
Django's MVC pattern is less strict so you sort of combine the view and the controller in the views.py. However, if you want to change the Admin, the Django docs are quite nice here: docs.djangoproject.com
If you want to create a custom admin functionality the docs should give you a first idea and if you're planning to create a blog, I would advice you to use an existing plugin such as Zinnia. There, you can find the desired functionalities and modify them instead of building them from scratch.
Also, there are a couple of tutorials on how to build reusable apps and they usually include a detailed guideline how to implement admin functionalities there. Just look it up on google.
I hope that helps you!

Which is the best way to log user actions in flask views?

I would like to log user actions whenever user logs in/out and adds, edits, deletes objects in my site models in flask. Which is the best way to do this? Also I would like to show the old data and the new modified data, which happens using wtfforms. I am using flask and Flask-SQLAlchemy. I want something similar to what Django framework offers in the 'History' hlink for the associated objects.
Use Signals. Take a look at this
http://flask.pocoo.org/docs/signals/
Using signals, you can keep track of any actions such as adds/edits etc. as needed. All you have to do is
from blinker import Namespace
my_signals = Namespace()
def add_user():
# add user code here
user_added = my_signals.signal('user-added')
You can refer to flask-login, also using Signals.

Django: Get notified of all changes in database

I was wandering if there's a way to get notified of any changes to objects in a Django database. Right now I just need an email if anybody adds or changes anything but it would be best if I could hook a function triggered by any change and could decide what to do.
Is there an easy way to do it in Django?
Two ideas come to mind:
Override the predefined model method for saving.
Use a signal like post_save.
Here is a good article that talks about the difference between the two things listed above and when to use them:
Django signals vs. custom save()-method
The article was written near the end of 2007, three days after the release of Django 0.96.1. However, I believe the advice the author gives still applies today.

How to preload model for ReferenceProperty?

I have a models in different files (blog/models.py, forum/models.py, article/models.py). In each of this files I have defined model classes with application prefix (BlobPost, BlogTag, ForumPost, ForumThread, Article, ArticleCategory).
Also I have appliation - comment, for adding comment attached to any model object. For example, I want to comment BlogPost, or add comment referenced to ForumPost. For this I use property with type ReferenceProperty() - without specify type of references. Any model can attached to comment.
What a problem? If I have show all comments in administration section, I see a problem with autoloading models for ReferenceProperty. I don't know, what type of model used for current comment. I need to autoload package with model, if this need.
Yes, exists simple solution - include all models from all applications. But, this is not good solution. I need load only need models. How to do this autoloading?
My idea is based on detect kind of property, and by first part of this name detect application name for load all models in this application. For example, I have comment with Reference to BlogPost model. I get name of application - Blog and load all models from blog.models import *
For implement my idea I need to understand - how to intercept creating property instances. In my case, if I loop over comments, I see that App Engine automatically (thanks, but not in my case) create instances for properties.
How to inject my logic for loading my models before creating property instance?
Thank you!
This isn't possible in the standard db framework, as there's not enough information present to find your models. The only information the framework has to work with is the kind name, which doesn't include the fully qualified package - so it has no way to figure out what package your model definition might be in.
If you're writing an admin interface, though, you probably want to use the low-level google.appengine.api.datastore interface, instead, which operates on dicts instead of model classes, and doesn't require a model definition.

How about having a SingletonModel in Django?

I'm making a very simple website in Django. On one of the pages there is a vertical ticker box. I need to give the client a way to edit the contents of the ticker box as an HTMLField.
The first way that came to mind was to make a model Ticker which will have only one instance. Then I thought, instead of making sure manually that only one instance exists, perhaps there is (or there should be) something like a SingletonModel class in Django, which is like a normal model, except it makes sure no more than one instance gets created?
Or perhaps I should be solving my problem in a different way?
Try django-solo, it works in django 1.5 + for sure, django-singletons doesn't work with 1.5 + because it uses a deprecated feature.
You can use django_singletons. It has a built in admin support.
I think having a "singleton" model is ugly; it's dumb use of the relational database and it's bad UI, because the admin UI is built around working with lists of objects.
Instead I prefer to use a generic solution like django-chunks or django-flatblocks for this.
rewrite your save method so that every time a Ticker object gets saved it overwrites the existing one (if one exists).
A model with only one instance, a singleton, is sometime useful for things like global settings that you want to edit from the admin instead of having them in Django settings.py.
There are several third party applications that helps implementing singleton models and improve the admin interface for instance, django-solo, django-singleton-admin, django-singletons.

Categories

Resources