Django Admin site - where to put admin.py - django-admin

I inherited a Django application at work and I am attempting to make some improvements. The application is working without issue however there is some maintenance work that is being done directly in the database tables. I would like to setup the Admin site to avoid having to directly edit the database table(s) content.
I have minimal experience with Django and have been thru a lot of the tutorials however this application structure doesn't really match any I have seen before.
The application is built using Django 3.2.9 (from the requirements.txt) The production version is hosted in GCP on App Engine with a MySQL CloudSQL instance. I am currently testing on a local machine via manage.py runserver(using a local MySQL instance.) At some point I will be updating versions of Django.
The code is structured like this
project/
|---manage.py
|---requirements.txt
+---project_app/
|----|----settings.py
|----|----urls.py
+---app1/
|---+---models/
|----|---+---app1/
|----|----|----|----app1.py
|---+---urls/
|----|---+---app1/
|----|----|----|----routing.py
|---+---views/
|----|---+---app1/
|----|----|----|----view.py
+---app2
|---+---similar structure to app1
+---app3
|---+---similar structure to app1
+---app4
|---+---similar structure to app1
My question is, which folder would I create the admin.py inside the app1 application in order to import the models for the Admin site to manage?
The Admin site is loading and shows the Users and Groups tables (along with the Python Social Auth and Auth Tokens tables) in the list.
There is very little documentation I could find about how the Admin site locates admin.py files. I have also tried using Procmon (Windows) to see if it attempts to find admin.py in any folders. Procmon is not seeing any reads on any admin.py file except under the site-packages in the venv.
Thanks for any help

Update: Was able to solve this
TL;DR the admin.py goes into same folder you have the "import admin" line in.
Adding
from django.contrib import admin
into app.py in app1 and having an admin.py in the same folder and making admin configuration does work
project/
|---manage.py
|---requirements.txt
+---project_app/
|----|----settings.py
|----|----urls.py
+---app1/
|----app.py
|----admin.py
|---+---models/
|----|----models_file.py
|---+---urls/
|----|---+---app1/
|----|----|----|----routing.py
|---+---views/
|----|---+---app1/
|----|----|----|----view.py
+---app2
|---+---similar structure to app1
Some additional things
The order of INSTALLED_APPS matters if you want to customize the admin site models
I wanted to remove the social_django models from the admin and that required putting social_django and social_core above app1 in INSTALLED_APPS
This application is using Django REST Framework with the rest_framework_simplejwt package which includes a Tokens model and this shows by default in the admin. Make sure the DRF and DRF jwt is before app1 in the INSTALLED_APPS than it can be unregistered in admin.py
End result is a admin.py file that looks like this
from django.contrib import admin
from django.contrib.auth.models import User, Group
from social_django.models import Association, Nonce, UserSocialAuth
from rest_framework.authtoken.models import TokenProxy
# My app1 models Imports
# Remove Django REST Framework models from admin.
admin.site.unregister(TokenProxy)
# Remove Default user and group models provided by Django
admin.site.unregister(User)
admin.site.unregister(Group)
# Remove Social Auth models from social-auth-app-django package
admin.site.unregister(Association)
admin.site.unregister(Nonce)
admin.site.unregister(UserSocialAuth)
# My app1 models site.unregister

Related

Modifying Hyperledger Composer WebApp

I've completed the Hyperledger Composer Developer tutorial https://hyperledger.github.io/composer/tutorials/developer-guide.html and I've generated the BNA, deployed it to a local Hyperledger Fabric 1.0 instance, generated the REST API, and generated the skeleton webapp using yo.
However, I noticed that the resulting webapp only has a menu for adding assets, and none for adding participants and submitting transactions. I have two questions:
1) Why doesn't the webapp generate menus for adding participants and submitting transactions? (or does it, and how do I get it to do that?)
2) In an attempt to add the ability to add participants and submit transactions through the webapp, I've looked through the app directory for the relevant source code but became confused as I'm relatively new to web/angular dev.
Is there any reference documentation to show which source files need to be edited to add participants and transactions?
As far as I can tell, \src\app folder has the source files but I don't understand the roles that each of these files play:
app.component.html has the actual HTML menus, but what is this [routerLink] referring to?
I noticed for each asset that there is a subfolder with a CSS, HTML, and three .TS files. Are these needed for each participant as well?
Any direction or reference on how to modify the web app to accommodate adding participants and transactions would be much appreciated.
1) Why doesn't the webapp generate menus for adding participants and submitting transactions? (or does it, and how do I get it to do that?)
Because its a very simple skeleton NG2 app scoped to assets, which you as a developer can add to. You can easily add REST API calls (operations) from your Angular app to (for example) submit transactions. This is how you should interact with the runtime Fabric using Composer REST operations.
2) Is there any reference documentation to show which source files need to be edited to add participants and transactions?
This is a straightforward Angular tutorial https://angular.io/tutorial and here https://angular.io/guide/quickstart - and this tutorial will help explain Angular further.
The dataservice in the generated App is where the current 'asset' REST operation is called from and further REST operations can be added here -> /src/app/data.service.ts

How to write a vendor programme/module for ZFT

I would like to write a Zend Framework 2 vendor module, hosted on github, installable via composer and given to the world at large!
Now while I have no problems writing modules, where I am struggling is the mechanics around this.
My initial instinct would be to do something like this:
Set up a zend framework 2 skeleton app
Add my module as a normal module
Navigate to the module folder and create a git repo
Work on the module and update the module to my gitto...
Now logic tells me this is not the right way to go. So I am thinking, maybe I write the module as a stand alone outside of the skeleton app, push to the gito, and then pull from the gito to a working applicaiton...
If you can direct me to a tutorial, please let me know or if you can confirm or deny my proposed thinking that would be great too.
You probably need some app around your module to check if it works, however that shouldn't affect the way you create that module.
When it comes to Zend Framework 2 integrating modules, all you really need is a class that is called SomeNameSpace\Module which must be autoloadable via Composer. Note that there is NO requirement to place this file at a certain location. ZF2 will detect that you are using it with Composer and simply check with class_exists() instead of trying to locate a file, include it and then check for the class.
This class should reveal some configuration info about your module in the way ZF2 expects modules to do this, i.e. add bootstrap event listeners, return configuration data via getConfig() etc. It need not return anything for getAutoloaderConfig(), because that's what Composer is for.
To add the module to the ZF2 application you add the SomeNameSpace name to the file config/application.config.php:
return array(
'modules' => array(
'OtherStuffForTheDemoApp',
'SomeNameSpace' # this enables your module in your demo app
# and anywhere else where it's being used
),
ZF2 will see the module mentioned, try to instantiate SomeNameSpace\Module, ask it about all the configuration you defined, possibly calling the hook functions like onBootstrap() you provided there - and that's about it. Your module is responsible for doing the rest, i.e. provide a service manager configuration, provide controllers etc. All classes are autoloaded by Composer.
I believe the question of how to expose resources like images of a module hasn't been answered by Zend itself - at least I saw these questions being raised, but unanswered in the most current version of the documentation: http://framework.zend.com/manual/current/en/modules/zend.module-manager.intro.html
The organisation of files inside your module is completely up to you, although it seems common practice to place the source in a folder named src, tests probably go into tests.

connecting to a grails app database

I am a python developer by day, but I have some java experience as well (mostly with struts). At work I was handed a grails app to install, which was ok, but the owners of the app didn't supply any credentials, and the app loads to a login page.
I unpacked the war, but I think the DB config has been packaged into a jar somewhere, because I can't seem to find any connection URL. There's an h2 db file that's been created after I deployed the App, so I'm wondering:
how can I connect to the db like you normally would with SQLite or mysql client, browse tables, and create an admin user so I can login?
Go with decompilation. You need not only a user and password, but at least a database JDBC URL. That may include DBMS name, host, port, database name - depending on Java driver specifics. Then connect as you would normally in Python or whatever, or with DBVisualizer.
But if the database is in H2/hsql (URL is like jdbc:h2:mem:XXX), you've got a problem: it is a Java in-process DBMS. In order to gain external access to it, you'll need to change the decompiled code, compile and pack it back with some additions. Hope it's not your case.
Another way in hsql/H2 case is to find and decompile database bootstrap code - as it's an in-process DB, it should be filled at every application startup, so there should be a code creating that superuser.
In Grails, the DB configuration, including authentication settings, are declared in a file called Datasource.groovy which is then compiled into the WAR. You'll need the source code to find the username and password for the H2 DB.
I don't know if Python can connect to a java datasource but it may be a solution.
An other solution may be to provide some REST webservice from Grails side to make it possible for the python program to access the database. This is the way I prefer and I often choose.
By the way, you have the configuration of the databases used by Grails in the conf directory (grails-app/conf/DataSource.groovy) and you'll get the jdbc URL to the database which could be convert to a classical URL. For example : jdbc:mysql://localhost/my_app refers to the database my_app on localhost and default port for a mysql database.
Feel free to ask if you need more information.
Cheers

Symfony Shared Library Upgrade

I have a symfony version 1.0.16 application that I want to upgrade to symfony 1.4. Symfony is currently installed as a shared library on my server. How can I convert from a server wide shared library to a domain specific symfony library?
I work every day on Symfony projects and one of the best practice i've adopted is:
Always embbed the symfony vendor libraries inside the applicacion (its the recommended by symfony when having non related projects hosted on same server).
So, steps to take in order to change from wide to local:
Create a vendor folder on $sf_root/lib/
Download and uncompress a symfony version inside $sf_root/lib/vendor/symfony folder
replace the symfony script on $sf_root/ with the one on ($sf_root/lib/vendor/symfony/data/bin/symfony)
Modify the ProjectConfiguration.class.php on config to start using the lib/vendor/symfony libraries. It should look something like this:
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration{
.....}
Rebuild all classes
Verify your code. Plugins not always work with every symfony versions. Also if you created administrator modules (like the ones generated by propel), check if the configuration.yml it's still consistent.
Also this could be helpful (its a different version but could give ideas if something breaks) Upgrade symfony project

Using Celery with Pylons

I am trying to set up celery and pylons and can't seem to get it working. I downloaded pylons-celery, but how do I point my Pylons environment to where the celeryconfig.py is? I placed the config file in my pylons-app\celery folder. I read this post, but it doesn't mention how to pair celery with pylons.. Any pointers are appreciated - thanks!
The tightest integration with pylons is to build a custom loader into paste commands. This is what celery-pylons does. Check out my fork of celery-pylons which should work with the latest celery and pylons 1.0.
To get the celeryd side working you need to add the correct options in your ini file and then call paster celeryd development.ini
For the webapp side you just need to import celerypylons in environment.py Then you will be able to import and use your tasks from anywhere in your project.
I'm not exactly sure what you're trying to achieve, but I'll explain how I just setup Pylons to work with celery and hopefully you can gain something from it.
My setup is fairly simple. I implemented a REST API with Pylons using a single controller called jobs. When a POST request is received, the controller action would submit a new task to celery, capture the new tasks's uuid, and respond to the original request with the uuid. I originally place my celeryconfig.py file in my controllers directory, but once I deployed the pylons app egg to the apache web server, the celeryconfig.py was no longer automatically found.
To remedy this, I created a celery directory at the same level as the model and controllers directories, and I placed my celeryconfig.py file there, along with an empty init.py.
Here is a snippet of code from jobs controller where I connect to celery and submit a new task.
from celery import Celery
from my_pylons_app.celery import celeryconfig
celery_app = Celery()
celery_app.config_from_object(celeryconfig)
result = celery_app.send_task("process_job", [job_guid], connect_timeout=10)
I was able to get this working with both my internal paste web server and my public live apache2 web server.

Resources