Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Creating a Django Package

Creating a Django Package

Step by step yo create your third party Django package

Élysson MR

October 23, 2015
Tweet

More Decks by Élysson MR

Other Decks in Programming

Transcript

  1. Creating a Django Package Step by step to create your

    third party Django package Created by Élysson Mendes Rezende
  2. What is a Django package? Reusable code for a commom

    problem Essentially a Django Package is a Python Package Could be a package or an entire Django Application A package should contain the "__init__.py" file In case of Django application it should follow common Django conventions such as models, tests, urls and views A package should only depends of itself
  3. Creating the app 1. Create a Django app with a

    name of your choice. I.e: notification 2. Create the "templatetags" folder inside the new app 3. Create a template tag file "notify.py" and create a "__init__.py" file. Leave they empty for now 4. We are going to add some content later
  4. Configuring the package Create a file called "setup.py" inside project's

    root directory This file contains some information about the package. Such as: name, version, packages, descripton and etc Write some code so that your file should be like this: # c o d i n g : u t f ­ 8 i m p o r t o s f r o m s e t u p t o o l s i m p o r t s e t u p c u r r e n t _ d i r = o s . p a t h . a b s p a t h ( o s . p a t h . d i r n a m e ( _ _ f i l e _ _ ) ) r e a d m e = o p e n ( o s . p a t h . j o i n ( c u r r e n t _ d i r , ' R E A D M E . m d ' ) ) . r e a d ( ) s e t u p ( n a m e = ' d j a n g o ­ t e m p l a t e t a g s ' , v e r s i o n = ' 1 . 0 ' , p a c k a g e s = [ ' n o t i f i c a t i o n s ' ] , d e s c r i p t i o n = ' C u s t o m t e n p l a t e t a g s f o r n o t i f i c a t i o n ' , l o n g _ d e s c r i p t i o n = r e a d m e , a u t h o r = ' É l y s s o n M R ' , a u t h o r _ e m a i l = ' e l y s s o n m r @ g m a i l . c o m ' , u r l = ' h t t p s : / / g i t h u b . c o m / e l y s s o n m r / d j a n g o _ p k g / ' , l i c e n s e = ' M I T ' , i n s t a l l _ r e q u i r e s = [ ' D j a n g o > = 1 . 8 ' , ] )
  5. Configuring the package Finally we need a manifest file telling

    what files we want to distribute Create a file called "MANIFEST.in" inside project's root directory with those lines: i n c l u d e * . m d r e c u r s i v e ­ i n c l u d e t e m p l a t e t a g s p k g * . p y * . h t m l
  6. Installing the package Now the package is ready to be

    installed To do it just run "python setup.py develop" If everything is installed correctly, this message should be shown: $ p y t h o n s e t u p . p y d e v e l o p . . . I n s t a l l e d / p a t h / t o / p r o j e c t / d j a n g o _ p k g P r o c e s s i n g d e p e n d e n c i e s f o r d j a n g o ­ t e m p l a t e t a g s = = 1 . 0 S e a r c h i n g f o r D j a n g o = = 1 . 8 . 5 B e s t m a t c h : D j a n g o 1 . 8 . 5 A d d i n g D j a n g o 1 . 8 . 5 t o e a s y ­ i n s t a l l . p t h f i l e I n s t a l l i n g d j a n g o ­ a d m i n s c r i p t t o / h o m e / u s e r / . v i r t u a l e n v s / v e n v / b i n U s i n g / h o m e / u s e r / . v i r t u a l e n v s / v e n v / l i b / p y t h o n 2 . 7 / s i t e ­ p a c k a g e s F i n i s h e d p r o c e s s i n g d e p e n d e n c i e s f o r d j a n g o ­ t e m p l a t e t a g s = = 1 . 0
  7. Now What? Now our packages is ready to be distributed

    We just need to add some code! Right? After that we can distribute it for everyone!!!
  8. Coding some template tags First we have to write some

    templatetags in "notification/templatetags/notify.py": # c o d i n g : u t f ­ 8 f r o m d j a n g o i m p o r t t e m p l a t e r e g i s t e r = t e m p l a t e . L i b r a r y ( ) @ r e g i s t e r . i n c l u s i o n _ t a g ( ' n o t i f i c a t i o n / n o t i f i c a t i o n . h t m l ' ) d e f r e n d e r _ n o t i f i c a t i o n ( ) : r e t u r n { } @ r e g i s t e r . f i l t e r d e f f o r m a t _ h e l l o ( v a l u e , n o m e ) : r e t u r n v a l u e % ( n o m e )
  9. Coding some template tags Now we have to create o

    html file in "notification/templates/notification/notification.html": < p > H i I a m a n o t i f i c a t i o n < / p >
  10. Testing template tags There is no need to create a

    full django project to test the code, just create a settings to use with the tests For example: creating a "test_settings.py" inside our project's root we can execute our tests using "$ python django-admin.py test -- settings=test_settings" We just need to add a few lines to our "test_settings.py": I N S T A L L E D _ A P P S = ( ' n o t i f i c a t i o n ' , ) D A T A B A S E S = { ' d e f a u l t ' : { ' E N G I N E ' : ' d j a n g o . d b . b a c k e n d s . s q l i t e 3 ' , ' N A M E ' : ' : m e m o r y : ' , } } S E C R E T _ K E Y = " s e c r e t _ k e y _ f o r _ t e s t i n g "
  11. Testing template tags Now we need to write some tests

    to validate those template tags The test module "notification/test.py" should looks like: # c o d i n g : u t f ­ 8 f r o m d j a n g o . t e s t i m p o r t T e s t C a s e f r o m d j a n g o . t e m p l a t e i m p o r t C o n t e x t f r o m d j a n g o . t e m p l a t e i m p o r t T e m p l a t e c l a s s T e m p l a t e T a g T e s t ( T e s t C a s e ) : d e f s e t U p ( s e l f ) : s e l f . t e m p l a t e = T e m p l a t e ( ' { % l o a d n o t i f y % } { % r e n d e r _ n o t i f i c a t i o n % } ' ) d e f t e s t _ t e m p l a t e t a g _ i n c l u s i o n ( s e l f ) : r e n d e r e d = s e l f . t e m p l a t e . r e n d e r ( C o n t e x t ( ) ) s e l f . a s s e r t I n ( ' a m a n o t i f i c a t i o n ' , r e n d e r e d ) c l a s s F i l t e r T e s t ( T e s t C a s e ) : d e f s e t U p ( s e l f ) : s e l f . t e m p l a t e = T e m p l a t e ( ' { % l o a d n o t i f y % } ' + ' { { " H e l l o % s , H o w a r e y o u ? " | f o r m a t _ h e l l o : " É l y s s o n M R " } } ' ) d e f t e s t _ f i l t e r ( s e l f ) : r e n d e r e d = s e l f . t e m p l a t e . r e n d e r ( C o n t e x t ( ) ) s e l f . a s s e r t I n ( u ' É l y s s o n M R ' , r e n d e r e d )
  12. Distributing Now everything is ready to distribute the package for

    everyone But first we have to register a new After the registration open your console and run "python setup.py register" Select option "1", put your username and password and your package is now registered To finish we need to upload all files to PyPi server, run "python setup.py sdist upload" PyPi account
  13. Now we're done Now everyone can add our package using

    the pip command "pip install django-templatetags" Then just add the app name into INSTALLED_APPS in the settings file To upload an update just run the upload command again, remmember to change your "setup.py" version
  14. Conclusion The process to create a Django package is very

    simple Many times we code to solve the same problems, why not create a package for that? Using packages we can share many solutions of common problems with others developers
  15. References How to create your first Django Package How to

    write reusable apps Python Modules The Python Package Index