start to restructure the project tree
This commit is contained in:
		
							
								
								
									
										674
									
								
								aircox/LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										674
									
								
								aircox/LICENSE
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,674 @@
 | 
			
		||||
                    GNU GENERAL PUBLIC LICENSE
 | 
			
		||||
                       Version 3, 29 June 2007
 | 
			
		||||
 | 
			
		||||
 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 | 
			
		||||
 Everyone is permitted to copy and distribute verbatim copies
 | 
			
		||||
 of this license document, but changing it is not allowed.
 | 
			
		||||
 | 
			
		||||
                            Preamble
 | 
			
		||||
 | 
			
		||||
  The GNU General Public License is a free, copyleft license for
 | 
			
		||||
software and other kinds of works.
 | 
			
		||||
 | 
			
		||||
  The licenses for most software and other practical works are designed
 | 
			
		||||
to take away your freedom to share and change the works.  By contrast,
 | 
			
		||||
the GNU General Public License is intended to guarantee your freedom to
 | 
			
		||||
share and change all versions of a program--to make sure it remains free
 | 
			
		||||
software for all its users.  We, the Free Software Foundation, use the
 | 
			
		||||
GNU General Public License for most of our software; it applies also to
 | 
			
		||||
any other work released this way by its authors.  You can apply it to
 | 
			
		||||
your programs, too.
 | 
			
		||||
 | 
			
		||||
  When we speak of free software, we are referring to freedom, not
 | 
			
		||||
price.  Our General Public Licenses are designed to make sure that you
 | 
			
		||||
have the freedom to distribute copies of free software (and charge for
 | 
			
		||||
them if you wish), that you receive source code or can get it if you
 | 
			
		||||
want it, that you can change the software or use pieces of it in new
 | 
			
		||||
free programs, and that you know you can do these things.
 | 
			
		||||
 | 
			
		||||
  To protect your rights, we need to prevent others from denying you
 | 
			
		||||
these rights or asking you to surrender the rights.  Therefore, you have
 | 
			
		||||
certain responsibilities if you distribute copies of the software, or if
 | 
			
		||||
you modify it: responsibilities to respect the freedom of others.
 | 
			
		||||
 | 
			
		||||
  For example, if you distribute copies of such a program, whether
 | 
			
		||||
gratis or for a fee, you must pass on to the recipients the same
 | 
			
		||||
freedoms that you received.  You must make sure that they, too, receive
 | 
			
		||||
or can get the source code.  And you must show them these terms so they
 | 
			
		||||
know their rights.
 | 
			
		||||
 | 
			
		||||
  Developers that use the GNU GPL protect your rights with two steps:
 | 
			
		||||
(1) assert copyright on the software, and (2) offer you this License
 | 
			
		||||
giving you legal permission to copy, distribute and/or modify it.
 | 
			
		||||
 | 
			
		||||
  For the developers' and authors' protection, the GPL clearly explains
 | 
			
		||||
that there is no warranty for this free software.  For both users' and
 | 
			
		||||
authors' sake, the GPL requires that modified versions be marked as
 | 
			
		||||
changed, so that their problems will not be attributed erroneously to
 | 
			
		||||
authors of previous versions.
 | 
			
		||||
 | 
			
		||||
  Some devices are designed to deny users access to install or run
 | 
			
		||||
modified versions of the software inside them, although the manufacturer
 | 
			
		||||
can do so.  This is fundamentally incompatible with the aim of
 | 
			
		||||
protecting users' freedom to change the software.  The systematic
 | 
			
		||||
pattern of such abuse occurs in the area of products for individuals to
 | 
			
		||||
use, which is precisely where it is most unacceptable.  Therefore, we
 | 
			
		||||
have designed this version of the GPL to prohibit the practice for those
 | 
			
		||||
products.  If such problems arise substantially in other domains, we
 | 
			
		||||
stand ready to extend this provision to those domains in future versions
 | 
			
		||||
of the GPL, as needed to protect the freedom of users.
 | 
			
		||||
 | 
			
		||||
  Finally, every program is threatened constantly by software patents.
 | 
			
		||||
States should not allow patents to restrict development and use of
 | 
			
		||||
software on general-purpose computers, but in those that do, we wish to
 | 
			
		||||
avoid the special danger that patents applied to a free program could
 | 
			
		||||
make it effectively proprietary.  To prevent this, the GPL assures that
 | 
			
		||||
patents cannot be used to render the program non-free.
 | 
			
		||||
 | 
			
		||||
  The precise terms and conditions for copying, distribution and
 | 
			
		||||
modification follow.
 | 
			
		||||
 | 
			
		||||
                       TERMS AND CONDITIONS
 | 
			
		||||
 | 
			
		||||
  0. Definitions.
 | 
			
		||||
 | 
			
		||||
  "This License" refers to version 3 of the GNU General Public License.
 | 
			
		||||
 | 
			
		||||
  "Copyright" also means copyright-like laws that apply to other kinds of
 | 
			
		||||
works, such as semiconductor masks.
 | 
			
		||||
 | 
			
		||||
  "The Program" refers to any copyrightable work licensed under this
 | 
			
		||||
License.  Each licensee is addressed as "you".  "Licensees" and
 | 
			
		||||
"recipients" may be individuals or organizations.
 | 
			
		||||
 | 
			
		||||
  To "modify" a work means to copy from or adapt all or part of the work
 | 
			
		||||
in a fashion requiring copyright permission, other than the making of an
 | 
			
		||||
exact copy.  The resulting work is called a "modified version" of the
 | 
			
		||||
earlier work or a work "based on" the earlier work.
 | 
			
		||||
 | 
			
		||||
  A "covered work" means either the unmodified Program or a work based
 | 
			
		||||
on the Program.
 | 
			
		||||
 | 
			
		||||
  To "propagate" a work means to do anything with it that, without
 | 
			
		||||
permission, would make you directly or secondarily liable for
 | 
			
		||||
infringement under applicable copyright law, except executing it on a
 | 
			
		||||
computer or modifying a private copy.  Propagation includes copying,
 | 
			
		||||
distribution (with or without modification), making available to the
 | 
			
		||||
public, and in some countries other activities as well.
 | 
			
		||||
 | 
			
		||||
  To "convey" a work means any kind of propagation that enables other
 | 
			
		||||
parties to make or receive copies.  Mere interaction with a user through
 | 
			
		||||
a computer network, with no transfer of a copy, is not conveying.
 | 
			
		||||
 | 
			
		||||
  An interactive user interface displays "Appropriate Legal Notices"
 | 
			
		||||
to the extent that it includes a convenient and prominently visible
 | 
			
		||||
feature that (1) displays an appropriate copyright notice, and (2)
 | 
			
		||||
tells the user that there is no warranty for the work (except to the
 | 
			
		||||
extent that warranties are provided), that licensees may convey the
 | 
			
		||||
work under this License, and how to view a copy of this License.  If
 | 
			
		||||
the interface presents a list of user commands or options, such as a
 | 
			
		||||
menu, a prominent item in the list meets this criterion.
 | 
			
		||||
 | 
			
		||||
  1. Source Code.
 | 
			
		||||
 | 
			
		||||
  The "source code" for a work means the preferred form of the work
 | 
			
		||||
for making modifications to it.  "Object code" means any non-source
 | 
			
		||||
form of a work.
 | 
			
		||||
 | 
			
		||||
  A "Standard Interface" means an interface that either is an official
 | 
			
		||||
standard defined by a recognized standards body, or, in the case of
 | 
			
		||||
interfaces specified for a particular programming language, one that
 | 
			
		||||
is widely used among developers working in that language.
 | 
			
		||||
 | 
			
		||||
  The "System Libraries" of an executable work include anything, other
 | 
			
		||||
than the work as a whole, that (a) is included in the normal form of
 | 
			
		||||
packaging a Major Component, but which is not part of that Major
 | 
			
		||||
Component, and (b) serves only to enable use of the work with that
 | 
			
		||||
Major Component, or to implement a Standard Interface for which an
 | 
			
		||||
implementation is available to the public in source code form.  A
 | 
			
		||||
"Major Component", in this context, means a major essential component
 | 
			
		||||
(kernel, window system, and so on) of the specific operating system
 | 
			
		||||
(if any) on which the executable work runs, or a compiler used to
 | 
			
		||||
produce the work, or an object code interpreter used to run it.
 | 
			
		||||
 | 
			
		||||
  The "Corresponding Source" for a work in object code form means all
 | 
			
		||||
the source code needed to generate, install, and (for an executable
 | 
			
		||||
work) run the object code and to modify the work, including scripts to
 | 
			
		||||
control those activities.  However, it does not include the work's
 | 
			
		||||
System Libraries, or general-purpose tools or generally available free
 | 
			
		||||
programs which are used unmodified in performing those activities but
 | 
			
		||||
which are not part of the work.  For example, Corresponding Source
 | 
			
		||||
includes interface definition files associated with source files for
 | 
			
		||||
the work, and the source code for shared libraries and dynamically
 | 
			
		||||
linked subprograms that the work is specifically designed to require,
 | 
			
		||||
such as by intimate data communication or control flow between those
 | 
			
		||||
subprograms and other parts of the work.
 | 
			
		||||
 | 
			
		||||
  The Corresponding Source need not include anything that users
 | 
			
		||||
can regenerate automatically from other parts of the Corresponding
 | 
			
		||||
Source.
 | 
			
		||||
 | 
			
		||||
  The Corresponding Source for a work in source code form is that
 | 
			
		||||
same work.
 | 
			
		||||
 | 
			
		||||
  2. Basic Permissions.
 | 
			
		||||
 | 
			
		||||
  All rights granted under this License are granted for the term of
 | 
			
		||||
copyright on the Program, and are irrevocable provided the stated
 | 
			
		||||
conditions are met.  This License explicitly affirms your unlimited
 | 
			
		||||
permission to run the unmodified Program.  The output from running a
 | 
			
		||||
covered work is covered by this License only if the output, given its
 | 
			
		||||
content, constitutes a covered work.  This License acknowledges your
 | 
			
		||||
rights of fair use or other equivalent, as provided by copyright law.
 | 
			
		||||
 | 
			
		||||
  You may make, run and propagate covered works that you do not
 | 
			
		||||
convey, without conditions so long as your license otherwise remains
 | 
			
		||||
in force.  You may convey covered works to others for the sole purpose
 | 
			
		||||
of having them make modifications exclusively for you, or provide you
 | 
			
		||||
with facilities for running those works, provided that you comply with
 | 
			
		||||
the terms of this License in conveying all material for which you do
 | 
			
		||||
not control copyright.  Those thus making or running the covered works
 | 
			
		||||
for you must do so exclusively on your behalf, under your direction
 | 
			
		||||
and control, on terms that prohibit them from making any copies of
 | 
			
		||||
your copyrighted material outside their relationship with you.
 | 
			
		||||
 | 
			
		||||
  Conveying under any other circumstances is permitted solely under
 | 
			
		||||
the conditions stated below.  Sublicensing is not allowed; section 10
 | 
			
		||||
makes it unnecessary.
 | 
			
		||||
 | 
			
		||||
  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
 | 
			
		||||
 | 
			
		||||
  No covered work shall be deemed part of an effective technological
 | 
			
		||||
measure under any applicable law fulfilling obligations under article
 | 
			
		||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
 | 
			
		||||
similar laws prohibiting or restricting circumvention of such
 | 
			
		||||
measures.
 | 
			
		||||
 | 
			
		||||
  When you convey a covered work, you waive any legal power to forbid
 | 
			
		||||
circumvention of technological measures to the extent such circumvention
 | 
			
		||||
is effected by exercising rights under this License with respect to
 | 
			
		||||
the covered work, and you disclaim any intention to limit operation or
 | 
			
		||||
modification of the work as a means of enforcing, against the work's
 | 
			
		||||
users, your or third parties' legal rights to forbid circumvention of
 | 
			
		||||
technological measures.
 | 
			
		||||
 | 
			
		||||
  4. Conveying Verbatim Copies.
 | 
			
		||||
 | 
			
		||||
  You may convey verbatim copies of the Program's source code as you
 | 
			
		||||
receive it, in any medium, provided that you conspicuously and
 | 
			
		||||
appropriately publish on each copy an appropriate copyright notice;
 | 
			
		||||
keep intact all notices stating that this License and any
 | 
			
		||||
non-permissive terms added in accord with section 7 apply to the code;
 | 
			
		||||
keep intact all notices of the absence of any warranty; and give all
 | 
			
		||||
recipients a copy of this License along with the Program.
 | 
			
		||||
 | 
			
		||||
  You may charge any price or no price for each copy that you convey,
 | 
			
		||||
and you may offer support or warranty protection for a fee.
 | 
			
		||||
 | 
			
		||||
  5. Conveying Modified Source Versions.
 | 
			
		||||
 | 
			
		||||
  You may convey a work based on the Program, or the modifications to
 | 
			
		||||
produce it from the Program, in the form of source code under the
 | 
			
		||||
terms of section 4, provided that you also meet all of these conditions:
 | 
			
		||||
 | 
			
		||||
    a) The work must carry prominent notices stating that you modified
 | 
			
		||||
    it, and giving a relevant date.
 | 
			
		||||
 | 
			
		||||
    b) The work must carry prominent notices stating that it is
 | 
			
		||||
    released under this License and any conditions added under section
 | 
			
		||||
    7.  This requirement modifies the requirement in section 4 to
 | 
			
		||||
    "keep intact all notices".
 | 
			
		||||
 | 
			
		||||
    c) You must license the entire work, as a whole, under this
 | 
			
		||||
    License to anyone who comes into possession of a copy.  This
 | 
			
		||||
    License will therefore apply, along with any applicable section 7
 | 
			
		||||
    additional terms, to the whole of the work, and all its parts,
 | 
			
		||||
    regardless of how they are packaged.  This License gives no
 | 
			
		||||
    permission to license the work in any other way, but it does not
 | 
			
		||||
    invalidate such permission if you have separately received it.
 | 
			
		||||
 | 
			
		||||
    d) If the work has interactive user interfaces, each must display
 | 
			
		||||
    Appropriate Legal Notices; however, if the Program has interactive
 | 
			
		||||
    interfaces that do not display Appropriate Legal Notices, your
 | 
			
		||||
    work need not make them do so.
 | 
			
		||||
 | 
			
		||||
  A compilation of a covered work with other separate and independent
 | 
			
		||||
works, which are not by their nature extensions of the covered work,
 | 
			
		||||
and which are not combined with it such as to form a larger program,
 | 
			
		||||
in or on a volume of a storage or distribution medium, is called an
 | 
			
		||||
"aggregate" if the compilation and its resulting copyright are not
 | 
			
		||||
used to limit the access or legal rights of the compilation's users
 | 
			
		||||
beyond what the individual works permit.  Inclusion of a covered work
 | 
			
		||||
in an aggregate does not cause this License to apply to the other
 | 
			
		||||
parts of the aggregate.
 | 
			
		||||
 | 
			
		||||
  6. Conveying Non-Source Forms.
 | 
			
		||||
 | 
			
		||||
  You may convey a covered work in object code form under the terms
 | 
			
		||||
of sections 4 and 5, provided that you also convey the
 | 
			
		||||
machine-readable Corresponding Source under the terms of this License,
 | 
			
		||||
in one of these ways:
 | 
			
		||||
 | 
			
		||||
    a) Convey the object code in, or embodied in, a physical product
 | 
			
		||||
    (including a physical distribution medium), accompanied by the
 | 
			
		||||
    Corresponding Source fixed on a durable physical medium
 | 
			
		||||
    customarily used for software interchange.
 | 
			
		||||
 | 
			
		||||
    b) Convey the object code in, or embodied in, a physical product
 | 
			
		||||
    (including a physical distribution medium), accompanied by a
 | 
			
		||||
    written offer, valid for at least three years and valid for as
 | 
			
		||||
    long as you offer spare parts or customer support for that product
 | 
			
		||||
    model, to give anyone who possesses the object code either (1) a
 | 
			
		||||
    copy of the Corresponding Source for all the software in the
 | 
			
		||||
    product that is covered by this License, on a durable physical
 | 
			
		||||
    medium customarily used for software interchange, for a price no
 | 
			
		||||
    more than your reasonable cost of physically performing this
 | 
			
		||||
    conveying of source, or (2) access to copy the
 | 
			
		||||
    Corresponding Source from a network server at no charge.
 | 
			
		||||
 | 
			
		||||
    c) Convey individual copies of the object code with a copy of the
 | 
			
		||||
    written offer to provide the Corresponding Source.  This
 | 
			
		||||
    alternative is allowed only occasionally and noncommercially, and
 | 
			
		||||
    only if you received the object code with such an offer, in accord
 | 
			
		||||
    with subsection 6b.
 | 
			
		||||
 | 
			
		||||
    d) Convey the object code by offering access from a designated
 | 
			
		||||
    place (gratis or for a charge), and offer equivalent access to the
 | 
			
		||||
    Corresponding Source in the same way through the same place at no
 | 
			
		||||
    further charge.  You need not require recipients to copy the
 | 
			
		||||
    Corresponding Source along with the object code.  If the place to
 | 
			
		||||
    copy the object code is a network server, the Corresponding Source
 | 
			
		||||
    may be on a different server (operated by you or a third party)
 | 
			
		||||
    that supports equivalent copying facilities, provided you maintain
 | 
			
		||||
    clear directions next to the object code saying where to find the
 | 
			
		||||
    Corresponding Source.  Regardless of what server hosts the
 | 
			
		||||
    Corresponding Source, you remain obligated to ensure that it is
 | 
			
		||||
    available for as long as needed to satisfy these requirements.
 | 
			
		||||
 | 
			
		||||
    e) Convey the object code using peer-to-peer transmission, provided
 | 
			
		||||
    you inform other peers where the object code and Corresponding
 | 
			
		||||
    Source of the work are being offered to the general public at no
 | 
			
		||||
    charge under subsection 6d.
 | 
			
		||||
 | 
			
		||||
  A separable portion of the object code, whose source code is excluded
 | 
			
		||||
from the Corresponding Source as a System Library, need not be
 | 
			
		||||
included in conveying the object code work.
 | 
			
		||||
 | 
			
		||||
  A "User Product" is either (1) a "consumer product", which means any
 | 
			
		||||
tangible personal property which is normally used for personal, family,
 | 
			
		||||
or household purposes, or (2) anything designed or sold for incorporation
 | 
			
		||||
into a dwelling.  In determining whether a product is a consumer product,
 | 
			
		||||
doubtful cases shall be resolved in favor of coverage.  For a particular
 | 
			
		||||
product received by a particular user, "normally used" refers to a
 | 
			
		||||
typical or common use of that class of product, regardless of the status
 | 
			
		||||
of the particular user or of the way in which the particular user
 | 
			
		||||
actually uses, or expects or is expected to use, the product.  A product
 | 
			
		||||
is a consumer product regardless of whether the product has substantial
 | 
			
		||||
commercial, industrial or non-consumer uses, unless such uses represent
 | 
			
		||||
the only significant mode of use of the product.
 | 
			
		||||
 | 
			
		||||
  "Installation Information" for a User Product means any methods,
 | 
			
		||||
procedures, authorization keys, or other information required to install
 | 
			
		||||
and execute modified versions of a covered work in that User Product from
 | 
			
		||||
a modified version of its Corresponding Source.  The information must
 | 
			
		||||
suffice to ensure that the continued functioning of the modified object
 | 
			
		||||
code is in no case prevented or interfered with solely because
 | 
			
		||||
modification has been made.
 | 
			
		||||
 | 
			
		||||
  If you convey an object code work under this section in, or with, or
 | 
			
		||||
specifically for use in, a User Product, and the conveying occurs as
 | 
			
		||||
part of a transaction in which the right of possession and use of the
 | 
			
		||||
User Product is transferred to the recipient in perpetuity or for a
 | 
			
		||||
fixed term (regardless of how the transaction is characterized), the
 | 
			
		||||
Corresponding Source conveyed under this section must be accompanied
 | 
			
		||||
by the Installation Information.  But this requirement does not apply
 | 
			
		||||
if neither you nor any third party retains the ability to install
 | 
			
		||||
modified object code on the User Product (for example, the work has
 | 
			
		||||
been installed in ROM).
 | 
			
		||||
 | 
			
		||||
  The requirement to provide Installation Information does not include a
 | 
			
		||||
requirement to continue to provide support service, warranty, or updates
 | 
			
		||||
for a work that has been modified or installed by the recipient, or for
 | 
			
		||||
the User Product in which it has been modified or installed.  Access to a
 | 
			
		||||
network may be denied when the modification itself materially and
 | 
			
		||||
adversely affects the operation of the network or violates the rules and
 | 
			
		||||
protocols for communication across the network.
 | 
			
		||||
 | 
			
		||||
  Corresponding Source conveyed, and Installation Information provided,
 | 
			
		||||
in accord with this section must be in a format that is publicly
 | 
			
		||||
documented (and with an implementation available to the public in
 | 
			
		||||
source code form), and must require no special password or key for
 | 
			
		||||
unpacking, reading or copying.
 | 
			
		||||
 | 
			
		||||
  7. Additional Terms.
 | 
			
		||||
 | 
			
		||||
  "Additional permissions" are terms that supplement the terms of this
 | 
			
		||||
License by making exceptions from one or more of its conditions.
 | 
			
		||||
Additional permissions that are applicable to the entire Program shall
 | 
			
		||||
be treated as though they were included in this License, to the extent
 | 
			
		||||
that they are valid under applicable law.  If additional permissions
 | 
			
		||||
apply only to part of the Program, that part may be used separately
 | 
			
		||||
under those permissions, but the entire Program remains governed by
 | 
			
		||||
this License without regard to the additional permissions.
 | 
			
		||||
 | 
			
		||||
  When you convey a copy of a covered work, you may at your option
 | 
			
		||||
remove any additional permissions from that copy, or from any part of
 | 
			
		||||
it.  (Additional permissions may be written to require their own
 | 
			
		||||
removal in certain cases when you modify the work.)  You may place
 | 
			
		||||
additional permissions on material, added by you to a covered work,
 | 
			
		||||
for which you have or can give appropriate copyright permission.
 | 
			
		||||
 | 
			
		||||
  Notwithstanding any other provision of this License, for material you
 | 
			
		||||
add to a covered work, you may (if authorized by the copyright holders of
 | 
			
		||||
that material) supplement the terms of this License with terms:
 | 
			
		||||
 | 
			
		||||
    a) Disclaiming warranty or limiting liability differently from the
 | 
			
		||||
    terms of sections 15 and 16 of this License; or
 | 
			
		||||
 | 
			
		||||
    b) Requiring preservation of specified reasonable legal notices or
 | 
			
		||||
    author attributions in that material or in the Appropriate Legal
 | 
			
		||||
    Notices displayed by works containing it; or
 | 
			
		||||
 | 
			
		||||
    c) Prohibiting misrepresentation of the origin of that material, or
 | 
			
		||||
    requiring that modified versions of such material be marked in
 | 
			
		||||
    reasonable ways as different from the original version; or
 | 
			
		||||
 | 
			
		||||
    d) Limiting the use for publicity purposes of names of licensors or
 | 
			
		||||
    authors of the material; or
 | 
			
		||||
 | 
			
		||||
    e) Declining to grant rights under trademark law for use of some
 | 
			
		||||
    trade names, trademarks, or service marks; or
 | 
			
		||||
 | 
			
		||||
    f) Requiring indemnification of licensors and authors of that
 | 
			
		||||
    material by anyone who conveys the material (or modified versions of
 | 
			
		||||
    it) with contractual assumptions of liability to the recipient, for
 | 
			
		||||
    any liability that these contractual assumptions directly impose on
 | 
			
		||||
    those licensors and authors.
 | 
			
		||||
 | 
			
		||||
  All other non-permissive additional terms are considered "further
 | 
			
		||||
restrictions" within the meaning of section 10.  If the Program as you
 | 
			
		||||
received it, or any part of it, contains a notice stating that it is
 | 
			
		||||
governed by this License along with a term that is a further
 | 
			
		||||
restriction, you may remove that term.  If a license document contains
 | 
			
		||||
a further restriction but permits relicensing or conveying under this
 | 
			
		||||
License, you may add to a covered work material governed by the terms
 | 
			
		||||
of that license document, provided that the further restriction does
 | 
			
		||||
not survive such relicensing or conveying.
 | 
			
		||||
 | 
			
		||||
  If you add terms to a covered work in accord with this section, you
 | 
			
		||||
must place, in the relevant source files, a statement of the
 | 
			
		||||
additional terms that apply to those files, or a notice indicating
 | 
			
		||||
where to find the applicable terms.
 | 
			
		||||
 | 
			
		||||
  Additional terms, permissive or non-permissive, may be stated in the
 | 
			
		||||
form of a separately written license, or stated as exceptions;
 | 
			
		||||
the above requirements apply either way.
 | 
			
		||||
 | 
			
		||||
  8. Termination.
 | 
			
		||||
 | 
			
		||||
  You may not propagate or modify a covered work except as expressly
 | 
			
		||||
provided under this License.  Any attempt otherwise to propagate or
 | 
			
		||||
modify it is void, and will automatically terminate your rights under
 | 
			
		||||
this License (including any patent licenses granted under the third
 | 
			
		||||
paragraph of section 11).
 | 
			
		||||
 | 
			
		||||
  However, if you cease all violation of this License, then your
 | 
			
		||||
license from a particular copyright holder is reinstated (a)
 | 
			
		||||
provisionally, unless and until the copyright holder explicitly and
 | 
			
		||||
finally terminates your license, and (b) permanently, if the copyright
 | 
			
		||||
holder fails to notify you of the violation by some reasonable means
 | 
			
		||||
prior to 60 days after the cessation.
 | 
			
		||||
 | 
			
		||||
  Moreover, your license from a particular copyright holder is
 | 
			
		||||
reinstated permanently if the copyright holder notifies you of the
 | 
			
		||||
violation by some reasonable means, this is the first time you have
 | 
			
		||||
received notice of violation of this License (for any work) from that
 | 
			
		||||
copyright holder, and you cure the violation prior to 30 days after
 | 
			
		||||
your receipt of the notice.
 | 
			
		||||
 | 
			
		||||
  Termination of your rights under this section does not terminate the
 | 
			
		||||
licenses of parties who have received copies or rights from you under
 | 
			
		||||
this License.  If your rights have been terminated and not permanently
 | 
			
		||||
reinstated, you do not qualify to receive new licenses for the same
 | 
			
		||||
material under section 10.
 | 
			
		||||
 | 
			
		||||
  9. Acceptance Not Required for Having Copies.
 | 
			
		||||
 | 
			
		||||
  You are not required to accept this License in order to receive or
 | 
			
		||||
run a copy of the Program.  Ancillary propagation of a covered work
 | 
			
		||||
occurring solely as a consequence of using peer-to-peer transmission
 | 
			
		||||
to receive a copy likewise does not require acceptance.  However,
 | 
			
		||||
nothing other than this License grants you permission to propagate or
 | 
			
		||||
modify any covered work.  These actions infringe copyright if you do
 | 
			
		||||
not accept this License.  Therefore, by modifying or propagating a
 | 
			
		||||
covered work, you indicate your acceptance of this License to do so.
 | 
			
		||||
 | 
			
		||||
  10. Automatic Licensing of Downstream Recipients.
 | 
			
		||||
 | 
			
		||||
  Each time you convey a covered work, the recipient automatically
 | 
			
		||||
receives a license from the original licensors, to run, modify and
 | 
			
		||||
propagate that work, subject to this License.  You are not responsible
 | 
			
		||||
for enforcing compliance by third parties with this License.
 | 
			
		||||
 | 
			
		||||
  An "entity transaction" is a transaction transferring control of an
 | 
			
		||||
organization, or substantially all assets of one, or subdividing an
 | 
			
		||||
organization, or merging organizations.  If propagation of a covered
 | 
			
		||||
work results from an entity transaction, each party to that
 | 
			
		||||
transaction who receives a copy of the work also receives whatever
 | 
			
		||||
licenses to the work the party's predecessor in interest had or could
 | 
			
		||||
give under the previous paragraph, plus a right to possession of the
 | 
			
		||||
Corresponding Source of the work from the predecessor in interest, if
 | 
			
		||||
the predecessor has it or can get it with reasonable efforts.
 | 
			
		||||
 | 
			
		||||
  You may not impose any further restrictions on the exercise of the
 | 
			
		||||
rights granted or affirmed under this License.  For example, you may
 | 
			
		||||
not impose a license fee, royalty, or other charge for exercise of
 | 
			
		||||
rights granted under this License, and you may not initiate litigation
 | 
			
		||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
 | 
			
		||||
any patent claim is infringed by making, using, selling, offering for
 | 
			
		||||
sale, or importing the Program or any portion of it.
 | 
			
		||||
 | 
			
		||||
  11. Patents.
 | 
			
		||||
 | 
			
		||||
  A "contributor" is a copyright holder who authorizes use under this
 | 
			
		||||
License of the Program or a work on which the Program is based.  The
 | 
			
		||||
work thus licensed is called the contributor's "contributor version".
 | 
			
		||||
 | 
			
		||||
  A contributor's "essential patent claims" are all patent claims
 | 
			
		||||
owned or controlled by the contributor, whether already acquired or
 | 
			
		||||
hereafter acquired, that would be infringed by some manner, permitted
 | 
			
		||||
by this License, of making, using, or selling its contributor version,
 | 
			
		||||
but do not include claims that would be infringed only as a
 | 
			
		||||
consequence of further modification of the contributor version.  For
 | 
			
		||||
purposes of this definition, "control" includes the right to grant
 | 
			
		||||
patent sublicenses in a manner consistent with the requirements of
 | 
			
		||||
this License.
 | 
			
		||||
 | 
			
		||||
  Each contributor grants you a non-exclusive, worldwide, royalty-free
 | 
			
		||||
patent license under the contributor's essential patent claims, to
 | 
			
		||||
make, use, sell, offer for sale, import and otherwise run, modify and
 | 
			
		||||
propagate the contents of its contributor version.
 | 
			
		||||
 | 
			
		||||
  In the following three paragraphs, a "patent license" is any express
 | 
			
		||||
agreement or commitment, however denominated, not to enforce a patent
 | 
			
		||||
(such as an express permission to practice a patent or covenant not to
 | 
			
		||||
sue for patent infringement).  To "grant" such a patent license to a
 | 
			
		||||
party means to make such an agreement or commitment not to enforce a
 | 
			
		||||
patent against the party.
 | 
			
		||||
 | 
			
		||||
  If you convey a covered work, knowingly relying on a patent license,
 | 
			
		||||
and the Corresponding Source of the work is not available for anyone
 | 
			
		||||
to copy, free of charge and under the terms of this License, through a
 | 
			
		||||
publicly available network server or other readily accessible means,
 | 
			
		||||
then you must either (1) cause the Corresponding Source to be so
 | 
			
		||||
available, or (2) arrange to deprive yourself of the benefit of the
 | 
			
		||||
patent license for this particular work, or (3) arrange, in a manner
 | 
			
		||||
consistent with the requirements of this License, to extend the patent
 | 
			
		||||
license to downstream recipients.  "Knowingly relying" means you have
 | 
			
		||||
actual knowledge that, but for the patent license, your conveying the
 | 
			
		||||
covered work in a country, or your recipient's use of the covered work
 | 
			
		||||
in a country, would infringe one or more identifiable patents in that
 | 
			
		||||
country that you have reason to believe are valid.
 | 
			
		||||
 | 
			
		||||
  If, pursuant to or in connection with a single transaction or
 | 
			
		||||
arrangement, you convey, or propagate by procuring conveyance of, a
 | 
			
		||||
covered work, and grant a patent license to some of the parties
 | 
			
		||||
receiving the covered work authorizing them to use, propagate, modify
 | 
			
		||||
or convey a specific copy of the covered work, then the patent license
 | 
			
		||||
you grant is automatically extended to all recipients of the covered
 | 
			
		||||
work and works based on it.
 | 
			
		||||
 | 
			
		||||
  A patent license is "discriminatory" if it does not include within
 | 
			
		||||
the scope of its coverage, prohibits the exercise of, or is
 | 
			
		||||
conditioned on the non-exercise of one or more of the rights that are
 | 
			
		||||
specifically granted under this License.  You may not convey a covered
 | 
			
		||||
work if you are a party to an arrangement with a third party that is
 | 
			
		||||
in the business of distributing software, under which you make payment
 | 
			
		||||
to the third party based on the extent of your activity of conveying
 | 
			
		||||
the work, and under which the third party grants, to any of the
 | 
			
		||||
parties who would receive the covered work from you, a discriminatory
 | 
			
		||||
patent license (a) in connection with copies of the covered work
 | 
			
		||||
conveyed by you (or copies made from those copies), or (b) primarily
 | 
			
		||||
for and in connection with specific products or compilations that
 | 
			
		||||
contain the covered work, unless you entered into that arrangement,
 | 
			
		||||
or that patent license was granted, prior to 28 March 2007.
 | 
			
		||||
 | 
			
		||||
  Nothing in this License shall be construed as excluding or limiting
 | 
			
		||||
any implied license or other defenses to infringement that may
 | 
			
		||||
otherwise be available to you under applicable patent law.
 | 
			
		||||
 | 
			
		||||
  12. No Surrender of Others' Freedom.
 | 
			
		||||
 | 
			
		||||
  If conditions are imposed on you (whether by court order, agreement or
 | 
			
		||||
otherwise) that contradict the conditions of this License, they do not
 | 
			
		||||
excuse you from the conditions of this License.  If you cannot convey a
 | 
			
		||||
covered work so as to satisfy simultaneously your obligations under this
 | 
			
		||||
License and any other pertinent obligations, then as a consequence you may
 | 
			
		||||
not convey it at all.  For example, if you agree to terms that obligate you
 | 
			
		||||
to collect a royalty for further conveying from those to whom you convey
 | 
			
		||||
the Program, the only way you could satisfy both those terms and this
 | 
			
		||||
License would be to refrain entirely from conveying the Program.
 | 
			
		||||
 | 
			
		||||
  13. Use with the GNU Affero General Public License.
 | 
			
		||||
 | 
			
		||||
  Notwithstanding any other provision of this License, you have
 | 
			
		||||
permission to link or combine any covered work with a work licensed
 | 
			
		||||
under version 3 of the GNU Affero General Public License into a single
 | 
			
		||||
combined work, and to convey the resulting work.  The terms of this
 | 
			
		||||
License will continue to apply to the part which is the covered work,
 | 
			
		||||
but the special requirements of the GNU Affero General Public License,
 | 
			
		||||
section 13, concerning interaction through a network will apply to the
 | 
			
		||||
combination as such.
 | 
			
		||||
 | 
			
		||||
  14. Revised Versions of this License.
 | 
			
		||||
 | 
			
		||||
  The Free Software Foundation may publish revised and/or new versions of
 | 
			
		||||
the GNU General Public License from time to time.  Such new versions will
 | 
			
		||||
be similar in spirit to the present version, but may differ in detail to
 | 
			
		||||
address new problems or concerns.
 | 
			
		||||
 | 
			
		||||
  Each version is given a distinguishing version number.  If the
 | 
			
		||||
Program specifies that a certain numbered version of the GNU General
 | 
			
		||||
Public License "or any later version" applies to it, you have the
 | 
			
		||||
option of following the terms and conditions either of that numbered
 | 
			
		||||
version or of any later version published by the Free Software
 | 
			
		||||
Foundation.  If the Program does not specify a version number of the
 | 
			
		||||
GNU General Public License, you may choose any version ever published
 | 
			
		||||
by the Free Software Foundation.
 | 
			
		||||
 | 
			
		||||
  If the Program specifies that a proxy can decide which future
 | 
			
		||||
versions of the GNU General Public License can be used, that proxy's
 | 
			
		||||
public statement of acceptance of a version permanently authorizes you
 | 
			
		||||
to choose that version for the Program.
 | 
			
		||||
 | 
			
		||||
  Later license versions may give you additional or different
 | 
			
		||||
permissions.  However, no additional obligations are imposed on any
 | 
			
		||||
author or copyright holder as a result of your choosing to follow a
 | 
			
		||||
later version.
 | 
			
		||||
 | 
			
		||||
  15. Disclaimer of Warranty.
 | 
			
		||||
 | 
			
		||||
  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
 | 
			
		||||
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
 | 
			
		||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
 | 
			
		||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
 | 
			
		||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 | 
			
		||||
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
 | 
			
		||||
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
 | 
			
		||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 | 
			
		||||
 | 
			
		||||
  16. Limitation of Liability.
 | 
			
		||||
 | 
			
		||||
  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 | 
			
		||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
 | 
			
		||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
 | 
			
		||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
 | 
			
		||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
 | 
			
		||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
 | 
			
		||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
 | 
			
		||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
 | 
			
		||||
SUCH DAMAGES.
 | 
			
		||||
 | 
			
		||||
  17. Interpretation of Sections 15 and 16.
 | 
			
		||||
 | 
			
		||||
  If the disclaimer of warranty and limitation of liability provided
 | 
			
		||||
above cannot be given local legal effect according to their terms,
 | 
			
		||||
reviewing courts shall apply local law that most closely approximates
 | 
			
		||||
an absolute waiver of all civil liability in connection with the
 | 
			
		||||
Program, unless a warranty or assumption of liability accompanies a
 | 
			
		||||
copy of the Program in return for a fee.
 | 
			
		||||
 | 
			
		||||
                     END OF TERMS AND CONDITIONS
 | 
			
		||||
 | 
			
		||||
            How to Apply These Terms to Your New Programs
 | 
			
		||||
 | 
			
		||||
  If you develop a new program, and you want it to be of the greatest
 | 
			
		||||
possible use to the public, the best way to achieve this is to make it
 | 
			
		||||
free software which everyone can redistribute and change under these terms.
 | 
			
		||||
 | 
			
		||||
  To do so, attach the following notices to the program.  It is safest
 | 
			
		||||
to attach them to the start of each source file to most effectively
 | 
			
		||||
state the exclusion of warranty; and each file should have at least
 | 
			
		||||
the "copyright" line and a pointer to where the full notice is found.
 | 
			
		||||
 | 
			
		||||
    <one line to give the program's name and a brief idea of what it does.>
 | 
			
		||||
    Copyright (C) <year>  <name of author>
 | 
			
		||||
 | 
			
		||||
    This program is free software: you can redistribute it and/or modify
 | 
			
		||||
    it under the terms of the GNU General Public License as published by
 | 
			
		||||
    the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
    (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
    This program is distributed in the hope that it will be useful,
 | 
			
		||||
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
    GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
    You should have received a copy of the GNU General Public License
 | 
			
		||||
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 | 
			
		||||
Also add information on how to contact you by electronic and paper mail.
 | 
			
		||||
 | 
			
		||||
  If the program does terminal interaction, make it output a short
 | 
			
		||||
notice like this when it starts in an interactive mode:
 | 
			
		||||
 | 
			
		||||
    <program>  Copyright (C) <year>  <name of author>
 | 
			
		||||
    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
 | 
			
		||||
    This is free software, and you are welcome to redistribute it
 | 
			
		||||
    under certain conditions; type `show c' for details.
 | 
			
		||||
 | 
			
		||||
The hypothetical commands `show w' and `show c' should show the appropriate
 | 
			
		||||
parts of the General Public License.  Of course, your program's commands
 | 
			
		||||
might be different; for a GUI interface, you would use an "about box".
 | 
			
		||||
 | 
			
		||||
  You should also get your employer (if you work as a programmer) or school,
 | 
			
		||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
 | 
			
		||||
For more information on this, and how to apply and follow the GNU GPL, see
 | 
			
		||||
<http://www.gnu.org/licenses/>.
 | 
			
		||||
 | 
			
		||||
  The GNU General Public License does not permit incorporating your program
 | 
			
		||||
into proprietary programs.  If your program is a subroutine library, you
 | 
			
		||||
may consider it more useful to permit linking proprietary applications with
 | 
			
		||||
the library.  If this is what you want to do, use the GNU Lesser General
 | 
			
		||||
Public License instead of this License.  But first, please read
 | 
			
		||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
 | 
			
		||||
							
								
								
									
										14
									
								
								aircox/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								aircox/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
# aircox
 | 
			
		||||
Platform to manage radio programs, schedules, cms, etc. -- main test repo
 | 
			
		||||
 | 
			
		||||
# Applications
 | 
			
		||||
* **programs**: programs, episodes, schedules, sounds and tracks;
 | 
			
		||||
* **cms**: cms renderer
 | 
			
		||||
* **website**: the website using the cms and the programs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Code and names conventions and uses
 | 
			
		||||
* absolute dates: datetime fields, named "begin" "end" for ranges and "date" otherwise
 | 
			
		||||
* time range: timefield name "duration"
 | 
			
		||||
* parents: when only one parent, named "parent", otherwise model/reference's name
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								aircox/cms/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								aircox/cms/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										3
									
								
								aircox/cms/admin.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								aircox/cms/admin.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
from django.contrib import admin
 | 
			
		||||
 | 
			
		||||
# Register your models here.
 | 
			
		||||
							
								
								
									
										236
									
								
								aircox/cms/models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										236
									
								
								aircox/cms/models.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,236 @@
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django.contrib.auth.models import User
 | 
			
		||||
from django.contrib.contenttypes.fields import GenericForeignKey
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
from django.utils.text import slugify
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.core.urlresolvers import reverse
 | 
			
		||||
 | 
			
		||||
from django.db.models.signals import post_init, post_save, post_delete
 | 
			
		||||
from django.dispatch import receiver
 | 
			
		||||
 | 
			
		||||
from taggit.managers import TaggableManager
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Post (models.Model):
 | 
			
		||||
    thread_type = models.ForeignKey(
 | 
			
		||||
        ContentType,
 | 
			
		||||
        on_delete=models.SET_NULL,
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
    thread_pk = models.PositiveIntegerField(
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
    thread = GenericForeignKey('thread_type', 'thread_pk')
 | 
			
		||||
 | 
			
		||||
    author = models.ForeignKey(
 | 
			
		||||
        User,
 | 
			
		||||
        verbose_name = _('author'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField(
 | 
			
		||||
        _('date'),
 | 
			
		||||
        default = timezone.datetime.now
 | 
			
		||||
    )
 | 
			
		||||
    published = models.BooleanField(
 | 
			
		||||
        verbose_name = _('public'),
 | 
			
		||||
        default = True
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    title = models.CharField (
 | 
			
		||||
        _('title'),
 | 
			
		||||
        max_length = 128,
 | 
			
		||||
    )
 | 
			
		||||
    content = models.TextField (
 | 
			
		||||
        _('description'),
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
    image = models.ImageField(
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
    tags = TaggableManager(
 | 
			
		||||
        _('tags'),
 | 
			
		||||
        blank = True,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def detail_url (self):
 | 
			
		||||
        return reverse(self._meta.verbose_name.lower() + '_detail',
 | 
			
		||||
                       kwargs = { 'pk': self.pk,
 | 
			
		||||
                                  'slug': slugify(self.title) })
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        abstract = True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Article (Post):
 | 
			
		||||
    static_page = models.BooleanField(
 | 
			
		||||
        _('static page'),
 | 
			
		||||
        default = False,
 | 
			
		||||
    )
 | 
			
		||||
    focus = models.BooleanField(
 | 
			
		||||
        _('article is focus'),
 | 
			
		||||
        default = False,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Article')
 | 
			
		||||
        verbose_name_plural = _('Articles')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class RelatedPostBase (models.base.ModelBase):
 | 
			
		||||
    """
 | 
			
		||||
    Metaclass for RelatedPost children.
 | 
			
		||||
    """
 | 
			
		||||
    registry = {}
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def register (cl, key, model):
 | 
			
		||||
        """
 | 
			
		||||
        Register a model and return the key under which it is registered.
 | 
			
		||||
        Raise a ValueError if another model is yet associated under this key.
 | 
			
		||||
        """
 | 
			
		||||
        if key in cl.registry and cl.registry[key] is not model:
 | 
			
		||||
            raise ValueError('A model has yet been registered with "{}"'
 | 
			
		||||
                             .format(key))
 | 
			
		||||
        cl.registry[key] = model
 | 
			
		||||
        return key
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def check_thread_mapping (cl, relation, model, field):
 | 
			
		||||
        """
 | 
			
		||||
        Add information related to the mapping 'thread' info.
 | 
			
		||||
        """
 | 
			
		||||
        if not field:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        parent_model = model._meta.get_field(field).rel.to
 | 
			
		||||
        thread_model = cl.registry.get(parent_model)
 | 
			
		||||
 | 
			
		||||
        if not thread_model:
 | 
			
		||||
            raise ValueError('no registered RelatedPost for the model {}'
 | 
			
		||||
                             .format(model.__name__))
 | 
			
		||||
        relation.thread_model = thread_model
 | 
			
		||||
 | 
			
		||||
    def __new__ (cl, name, bases, attrs):
 | 
			
		||||
        rel = attrs.get('Relation')
 | 
			
		||||
        rel = (rel and rel.__dict__) or {}
 | 
			
		||||
 | 
			
		||||
        related_model = rel.get('model')
 | 
			
		||||
        if related_model:
 | 
			
		||||
            attrs['related'] = models.ForeignKey(related_model)
 | 
			
		||||
 | 
			
		||||
        if not '__str__' in attrs:
 | 
			
		||||
            attrs['__str__'] = lambda self: str(self.related)
 | 
			
		||||
 | 
			
		||||
        if name is not 'RelatedPost':
 | 
			
		||||
            _relation = RelatedPost.Relation()
 | 
			
		||||
            _relation.__dict__.update(rel)
 | 
			
		||||
            mapping = rel.get('mapping')
 | 
			
		||||
            cl.check_thread_mapping(
 | 
			
		||||
                _relation,
 | 
			
		||||
                related_model,
 | 
			
		||||
                mapping and mapping.get('thread')
 | 
			
		||||
            )
 | 
			
		||||
            attrs['_relation'] = _relation
 | 
			
		||||
 | 
			
		||||
        model = super().__new__(cl, name, bases, attrs)
 | 
			
		||||
        cl.register(related_model, model)
 | 
			
		||||
        return model
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class RelatedPost (Post, metaclass = RelatedPostBase):
 | 
			
		||||
    """
 | 
			
		||||
    Use this post to generate Posts that are related to an external model. An
 | 
			
		||||
    extra field "related" will be generated, and some bindings are possible to
 | 
			
		||||
    update te related object on save if desired;
 | 
			
		||||
 | 
			
		||||
    This is done through a class name Relation inside the declaration of the new
 | 
			
		||||
    model.
 | 
			
		||||
    """
 | 
			
		||||
    related = None
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        abstract = True
 | 
			
		||||
 | 
			
		||||
    class Relation:
 | 
			
		||||
        """
 | 
			
		||||
        Relation descriptor used to generate and manage the related object.
 | 
			
		||||
 | 
			
		||||
        * model: model of the related object
 | 
			
		||||
        * mapping: values that are bound between the post and the related
 | 
			
		||||
            object. When the post is saved, these fields are updated on it.
 | 
			
		||||
            It is a dict of { post_attr: rel_attr }
 | 
			
		||||
 | 
			
		||||
            If there is a post_attr "thread", the corresponding rel_attr is used
 | 
			
		||||
            to update the post thread to the correct Post model (in order to
 | 
			
		||||
            establish a parent-child relation between two models)
 | 
			
		||||
        * thread_model: generated by the metaclass that point to the
 | 
			
		||||
            RelatedModel class related to the model that is the parent of
 | 
			
		||||
            the current related one.
 | 
			
		||||
        """
 | 
			
		||||
        model = None
 | 
			
		||||
        mapping = None          # values to map { post_attr: rel_attr }
 | 
			
		||||
        thread = None
 | 
			
		||||
        thread_model = None
 | 
			
		||||
 | 
			
		||||
    def get_attribute (self, attr):
 | 
			
		||||
        attr = self._relation.mappings.get(attr)
 | 
			
		||||
        return self.related.__dict__[attr] if attr else None
 | 
			
		||||
 | 
			
		||||
    def update_thread_mapping (self, save = True):
 | 
			
		||||
        """
 | 
			
		||||
        Update the parent object designed by Relation.mapping.thread if the
 | 
			
		||||
        type matches to the one related of the current instance's thread.
 | 
			
		||||
 | 
			
		||||
        If there is no thread assigned to self, set it to the parent of the
 | 
			
		||||
        related object.
 | 
			
		||||
        """
 | 
			
		||||
        relation = self._relation
 | 
			
		||||
        thread_model = relation.thread_model
 | 
			
		||||
        if not thread_model:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        # self.related.parent -> self.thread
 | 
			
		||||
        rel_parent = relation.mapping.get('thread')
 | 
			
		||||
        if not self.thread:
 | 
			
		||||
            rel_parent = getattr(self.related, rel_parent)
 | 
			
		||||
            thread = thread_model.objects.filter(related = rel_parent)
 | 
			
		||||
            if thread.count():
 | 
			
		||||
                self.thread = thread[0]
 | 
			
		||||
                if save:
 | 
			
		||||
                    self.save()
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        # self.thread -> self.related.parent
 | 
			
		||||
        if thread_model is not self.thread_type.model_class():
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        setattr(self.related, rel_parent, self.thread.related)
 | 
			
		||||
        if save:
 | 
			
		||||
            self.save()
 | 
			
		||||
 | 
			
		||||
    def update_mapping (self):
 | 
			
		||||
        relation = self._relation
 | 
			
		||||
        mapping = relation.mapping
 | 
			
		||||
        if not mapping:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        related = self.related
 | 
			
		||||
        related.__dict__.update({
 | 
			
		||||
            rel_attr: self.__dict__[attr]
 | 
			
		||||
            for attr, rel_attr in mapping.items()
 | 
			
		||||
            if attr is not 'thread' and attr in self.__dict__
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        self.update_thread_mapping(save = False)
 | 
			
		||||
        related.save()
 | 
			
		||||
 | 
			
		||||
    def save (self, *args, **kwargs):
 | 
			
		||||
        if not self.title and self.related:
 | 
			
		||||
            self.title = self.get_attribute('title')
 | 
			
		||||
 | 
			
		||||
        self.update_mapping()
 | 
			
		||||
        super().save(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										173
									
								
								aircox/cms/routes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								aircox/cms/routes.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,173 @@
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
 | 
			
		||||
from website.models import *
 | 
			
		||||
from website.views import *
 | 
			
		||||
 | 
			
		||||
class Router:
 | 
			
		||||
    registry = []
 | 
			
		||||
 | 
			
		||||
    def register (self, route):
 | 
			
		||||
        if not route in self.registry:
 | 
			
		||||
            self.registry.append(route)
 | 
			
		||||
 | 
			
		||||
    def register_set (self, view_set):
 | 
			
		||||
        for url in view_set.urls:
 | 
			
		||||
            self.register(url)
 | 
			
		||||
 | 
			
		||||
    def unregister (self, route):
 | 
			
		||||
        self.registry.remove(route)
 | 
			
		||||
 | 
			
		||||
    def get_urlpatterns (self):
 | 
			
		||||
        return [ url for url in self.registry ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Route:
 | 
			
		||||
    """
 | 
			
		||||
    Base class for routing. Given a model, we generate url specific for each
 | 
			
		||||
    route type. The generated url takes this form:
 | 
			
		||||
        model_name + '/' + route_name + '/' + '/'.join(route_url_args)
 | 
			
		||||
 | 
			
		||||
    Where model_name by default is the given model's verbose_name (uses plural if
 | 
			
		||||
    Route is for a list).
 | 
			
		||||
 | 
			
		||||
    The given view is considered as a django class view, and has view_
 | 
			
		||||
    """
 | 
			
		||||
    name = None         # route name
 | 
			
		||||
    url_args = []       # arguments passed from the url [ (name : regex),... ]
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_queryset (cl, website, model, request, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Called by the view to get the queryset when it is needed
 | 
			
		||||
        """
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_object (cl, website, model, request, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Called by the view to get the object when it is needed
 | 
			
		||||
        """
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_title (cl, model, request, **kwargs):
 | 
			
		||||
        return ''
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_view_name (cl, name):
 | 
			
		||||
        return name + '_' + cl.name
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def as_url (cl, name, model, view, view_kwargs = None):
 | 
			
		||||
        pattern = '^{}/{}'.format(name, cl.name)
 | 
			
		||||
        if cl.url_args:
 | 
			
		||||
            url_args = '/'.join([
 | 
			
		||||
                '(?P<{}>{}){}'.format(
 | 
			
		||||
                    arg, expr,
 | 
			
		||||
                    (optional and optional[0] and '?') or ''
 | 
			
		||||
                )
 | 
			
		||||
                for arg, expr, *optional in cl.url_args
 | 
			
		||||
            ])
 | 
			
		||||
            pattern += '/' + url_args
 | 
			
		||||
        pattern += '/?$'
 | 
			
		||||
 | 
			
		||||
        kwargs = {
 | 
			
		||||
            'route': cl,
 | 
			
		||||
        }
 | 
			
		||||
        if view_kwargs:
 | 
			
		||||
            kwargs.update(view_kwargs)
 | 
			
		||||
 | 
			
		||||
        return url(pattern, view, kwargs = kwargs,
 | 
			
		||||
                   name = cl.get_view_name(name))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DetailRoute (Route):
 | 
			
		||||
    name = 'detail'
 | 
			
		||||
    url_args = [
 | 
			
		||||
        ('pk', '[0-9]+'),
 | 
			
		||||
        ('slug', '(\w|-|_)+', True),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_object (cl, website, model, request, pk, **kwargs):
 | 
			
		||||
        return model.objects.get(pk = int(pk))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AllRoute (Route):
 | 
			
		||||
    name = 'all'
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_queryset (cl, website, model, request, **kwargs):
 | 
			
		||||
        return model.objects.all()
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_title (cl, model, request, **kwargs):
 | 
			
		||||
        return _('All %(model)s') % {
 | 
			
		||||
            'model': model._meta.verbose_name_plural
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ThreadRoute (Route):
 | 
			
		||||
    """
 | 
			
		||||
    Select posts using by their assigned thread.
 | 
			
		||||
 | 
			
		||||
    - "thread_model" can be a string with the name of a registered item on
 | 
			
		||||
    website or a model.
 | 
			
		||||
    - "pk" is the pk of the thread item.
 | 
			
		||||
    """
 | 
			
		||||
    name = 'thread'
 | 
			
		||||
    url_args = [
 | 
			
		||||
        ('thread_model', '(\w|_|-)+'),
 | 
			
		||||
        ('pk', '[0-9]+'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_queryset (cl, website, model, request, thread_model, pk, **kwargs):
 | 
			
		||||
        if type(thread_model) is str:
 | 
			
		||||
            thread_model = website.registry.get(thread_model)
 | 
			
		||||
 | 
			
		||||
        if not thread_model:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        thread_model = ContentType.objects.get_for_model(thread_model)
 | 
			
		||||
        return model.objects.filter(
 | 
			
		||||
            thread_type = thread_model,
 | 
			
		||||
            thread_pk = int(pk)
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DateRoute (Route):
 | 
			
		||||
    name = 'date'
 | 
			
		||||
    url_args = [
 | 
			
		||||
        ('year', '[0-9]{4}'),
 | 
			
		||||
        ('month', '[0-9]{2}'),
 | 
			
		||||
        ('day', '[0-9]{1,2}'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_queryset (cl, website, model, request, year, month, day, **kwargs):
 | 
			
		||||
        return model.objects.filter(
 | 
			
		||||
            date__year = int(year),
 | 
			
		||||
            date__month = int(month),
 | 
			
		||||
            date__day = int(day),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SearchRoute (Route):
 | 
			
		||||
    name = 'search'
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_queryset (cl, website, model, request, q, **kwargs):
 | 
			
		||||
        qs = model.objects
 | 
			
		||||
        for search_field in model.search_fields or []:
 | 
			
		||||
            r = model.objects.filter(**{ search_field + '__icontains': q })
 | 
			
		||||
            if qs: qs = qs | r
 | 
			
		||||
            else: qs = r
 | 
			
		||||
 | 
			
		||||
        qs.distinct()
 | 
			
		||||
        return qs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										50
									
								
								aircox/cms/static/aircox_cms/styles.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								aircox/cms/static/aircox_cms/styles.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
body {
 | 
			
		||||
    padding: 0;
 | 
			
		||||
    margin: 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.page {
 | 
			
		||||
    display: flex;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    .page .menu {
 | 
			
		||||
        width: 20em;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .page .menu_left { margin-right: 0.5em; }
 | 
			
		||||
    .page .menu_right { margin-left: 0.5em; }
 | 
			
		||||
 | 
			
		||||
    .page main {
 | 
			
		||||
        flex-grow: 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.section {
 | 
			
		||||
    vertical-align: top;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main .section {
 | 
			
		||||
    width: calc(50% - 2em);
 | 
			
		||||
    display: inline-block;
 | 
			
		||||
    padding: 0.5em;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    main .section .section_content {
 | 
			
		||||
        font-size: 0.95em;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    main .section h1 {
 | 
			
		||||
        font-size: 1.2em;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    main .section * {
 | 
			
		||||
        max-width: 100%;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.post_item {
 | 
			
		||||
    display: block;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8
									
								
								aircox/cms/templates/aircox_cms/base_content.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								aircox/cms/templates/aircox_cms/base_content.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
{% block pre_title %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
{% block title %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										9
									
								
								aircox/cms/templates/aircox_cms/base_section.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								aircox/cms/templates/aircox_cms/base_section.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
 | 
			
		||||
<{{ tag }} class="section {{ classes }}"
 | 
			
		||||
    {% for key, value in attrs.items %}{{ key }} = "{{ value|addslashes }}"
 | 
			
		||||
    {% endfor %} >
 | 
			
		||||
{% block content %}
 | 
			
		||||
{{ content|safe }}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
</{{ tag }}>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										71
									
								
								aircox/cms/templates/aircox_cms/base_site.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								aircox/cms/templates/aircox_cms/base_site.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
			
		||||
{% load staticfiles %}
 | 
			
		||||
 | 
			
		||||
<html>
 | 
			
		||||
    <head>
 | 
			
		||||
        {# FIXME: page tags #}
 | 
			
		||||
        <meta charset="utf-8">
 | 
			
		||||
        <meta name="application-name" content="aircox-cms">
 | 
			
		||||
        <meta name="description" content="{{ website.description }}">
 | 
			
		||||
        <meta name="keywords" content="{{ website.tags }}">
 | 
			
		||||
 | 
			
		||||
        <link rel="stylesheet" href="{% static "aircox_cms/styles.css" %}" type="text/css">
 | 
			
		||||
        {% if website.styles %}
 | 
			
		||||
        <link rel="stylesheet" href="{% static website.styles %}" type="text/css">
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        <title>{{ website.name }} {% if title %}- {{ title }} {% endif %}</title>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
        {% block header %}
 | 
			
		||||
            {% if menus.header %}
 | 
			
		||||
            {{ menus.header|safe }}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
        {% endblock %}
 | 
			
		||||
 | 
			
		||||
        <div class="page-container">
 | 
			
		||||
            {% if menus.top %}
 | 
			
		||||
                {{ menus.top|safe }}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
 | 
			
		||||
            <div class="page">
 | 
			
		||||
                {% if menus.left %}
 | 
			
		||||
                    {{ menus.left|safe }}
 | 
			
		||||
                {% endif %}
 | 
			
		||||
 | 
			
		||||
                <main>
 | 
			
		||||
                    {% block pre_title %}
 | 
			
		||||
                    {% endblock %}
 | 
			
		||||
                    <h1>
 | 
			
		||||
                        {% block title %}
 | 
			
		||||
                        {{ title }}
 | 
			
		||||
                        {% endblock %}
 | 
			
		||||
                    </h1>
 | 
			
		||||
                    {% block post_title %}
 | 
			
		||||
                    {% endblock %}
 | 
			
		||||
                    <div class="content">
 | 
			
		||||
                        {% block content %}
 | 
			
		||||
                        {% endblock %}
 | 
			
		||||
                    </div>
 | 
			
		||||
                </main>
 | 
			
		||||
 | 
			
		||||
                {% if menus.right %}
 | 
			
		||||
                    {{ menus.right|safe }}
 | 
			
		||||
                {% endif %}
 | 
			
		||||
            </div>
 | 
			
		||||
 | 
			
		||||
            {% if menus.page_bottom %}
 | 
			
		||||
                {{ menus.page_bottom|safe }}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
        </div>
 | 
			
		||||
 | 
			
		||||
        {% block footer %}
 | 
			
		||||
            {% if menus.footer %}
 | 
			
		||||
            {{ menus.footer|safe }}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
        {% endblock %}
 | 
			
		||||
 | 
			
		||||
        {% if menus.bottom %}
 | 
			
		||||
            {{ menus.bottom|safe }}
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    </body>
 | 
			
		||||
</html>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								aircox/cms/templates/aircox_cms/detail.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								aircox/cms/templates/aircox_cms/detail.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
{% extends embed|yesno:"aircox_cms/base_content.html,aircox_cms/base_site.html" %}
 | 
			
		||||
 | 
			
		||||
{% block title %}
 | 
			
		||||
{{ object.title }}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block pre_title %}
 | 
			
		||||
<time datetime="{{ object.date }}">
 | 
			
		||||
    {{ object.date|date:'l d F Y' }},
 | 
			
		||||
    {{ object.date|time:'H\hi' }}
 | 
			
		||||
</time>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% for section in sections %}
 | 
			
		||||
{{ section|safe }}
 | 
			
		||||
{% endfor %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								aircox/cms/templates/aircox_cms/embed.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								aircox/cms/templates/aircox_cms/embed.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
{# Used for embedded content #}
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										65
									
								
								aircox/cms/templates/aircox_cms/list.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								aircox/cms/templates/aircox_cms/list.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
			
		||||
{% extends embed|yesno:"aircox_cms/base_content.html,aircox_cms/base_site.html" %}
 | 
			
		||||
 | 
			
		||||
{% load i18n %}
 | 
			
		||||
{% load thumbnail %}
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
<div class="post_list {{ classes }} {% if embed %}embed{% endif %}">
 | 
			
		||||
{% for post in object_list %}
 | 
			
		||||
    <a class="post_item"
 | 
			
		||||
       href="{{ post.detail_url }}">
 | 
			
		||||
        {% if 'date' in view.fields or 'time' in view.fields %}
 | 
			
		||||
        <time datetime="{{ post.date }}" class="post_datetime">
 | 
			
		||||
            {% if 'date' in view.fields %}
 | 
			
		||||
            <span class="post_date">
 | 
			
		||||
                {{ post.date|date:'D. d F' }}
 | 
			
		||||
            </span>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
            {% if 'time' in view.fields %}
 | 
			
		||||
            <span class="post_time">
 | 
			
		||||
                {{ post.date|date:'H:i' }}
 | 
			
		||||
            </span>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
        </time>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'image' in view.fields %}
 | 
			
		||||
        <img src="{% thumbnail post.image view.icon_size crop %}" class="post_image">
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'title' in view.fields %}
 | 
			
		||||
        <h3 class="post_title">{{ post.title }}</h3>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {% if 'content' in view.fields %}
 | 
			
		||||
        <div class="post_content">
 | 
			
		||||
        {{ post.content|safe|striptags|truncatechars:"64" }}
 | 
			
		||||
        </div>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    </a>
 | 
			
		||||
{% endfor %}
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<nav>
 | 
			
		||||
    {% if embed %}
 | 
			
		||||
        {% with view.get_url as list_url %}
 | 
			
		||||
        {% if list_url %}
 | 
			
		||||
        <a href="{{list_url}}" title="More elements">⇲</a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        {% endwith %}
 | 
			
		||||
    {% else %}
 | 
			
		||||
        {% if page_obj.has_previous %}
 | 
			
		||||
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        <span class="current">
 | 
			
		||||
            {{ page_obj.number }} / {{ page_obj.paginator.num_pages }}
 | 
			
		||||
        </span>
 | 
			
		||||
 | 
			
		||||
        {% if page_obj.has_next %}
 | 
			
		||||
            <a href="?page={{ page_obj.next_page_number }}">next</a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
</nav>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								aircox/cms/templates/aircox_cms/menu.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								aircox/cms/templates/aircox_cms/menu.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
 | 
			
		||||
<{{ tag }} class="menu menu_{{ position }} {{ classes }}" {% if name %}
 | 
			
		||||
        name="{{ name }}"
 | 
			
		||||
        id="{{ name }}"
 | 
			
		||||
    {% endif %}>
 | 
			
		||||
    {% for section in sections %}
 | 
			
		||||
    {{ section|safe }}
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
</{{ tag }}>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										34
									
								
								aircox/cms/templates/aircox_cms/section.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								aircox/cms/templates/aircox_cms/section.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
			
		||||
{% extends "aircox_cms/base_section.html" %}
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% if title %}
 | 
			
		||||
<h1>
 | 
			
		||||
    {% block section_title %}
 | 
			
		||||
    {{ title }}
 | 
			
		||||
    {% endblock %}
 | 
			
		||||
</h1>
 | 
			
		||||
{% endif %}
 | 
			
		||||
 | 
			
		||||
{% if header %}
 | 
			
		||||
<header class="section_header">
 | 
			
		||||
    {% block section_header %}
 | 
			
		||||
    {{ header }}
 | 
			
		||||
    {% endblock %}
 | 
			
		||||
</header>
 | 
			
		||||
{% endif %}
 | 
			
		||||
 | 
			
		||||
<div class="section_content">
 | 
			
		||||
    {% block section_content %}
 | 
			
		||||
    {{ content|safe }}
 | 
			
		||||
    {% endblock %}
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
{% if footer %}
 | 
			
		||||
<footer class="section_footer">
 | 
			
		||||
    {% block section_footer %}
 | 
			
		||||
    {{ footer }}
 | 
			
		||||
    {% endblock %}
 | 
			
		||||
</footer>
 | 
			
		||||
{% endif %}
 | 
			
		||||
 | 
			
		||||
{% endblock %}
 | 
			
		||||
							
								
								
									
										28
									
								
								aircox/cms/templates/aircox_cms/section_list.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								aircox/cms/templates/aircox_cms/section_list.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
			
		||||
{% extends "aircox_cms/section.html" %}
 | 
			
		||||
 | 
			
		||||
{% load thumbnail %}
 | 
			
		||||
 | 
			
		||||
{% block section_content %}
 | 
			
		||||
<ul style="padding:0; margin:0">
 | 
			
		||||
    {% for item in object_list %}
 | 
			
		||||
    <li>
 | 
			
		||||
        {% if item.url %}
 | 
			
		||||
        <a href="{{item.url}}">
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        {% if use_icons and item.icon %}
 | 
			
		||||
        <img src="{% thumbnail item.icon icon_size crop %}" class="icon">
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
        {{ item.title }}
 | 
			
		||||
 | 
			
		||||
        {% if item.text %}
 | 
			
		||||
            <small>{{ item.text }}</small>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        {% if item.url %}
 | 
			
		||||
        </a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
    </li>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
</ul>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										6
									
								
								aircox/cms/templates/aircox_cms/tags
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								aircox/cms/templates/aircox_cms/tags
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
!_TAG_FILE_FORMAT	2	/extended format; --format=1 will not append ;" to lines/
 | 
			
		||||
!_TAG_FILE_SORTED	1	/0=unsorted, 1=sorted, 2=foldcase/
 | 
			
		||||
!_TAG_PROGRAM_AUTHOR	Darren Hiebert	/dhiebert@users.sourceforge.net/
 | 
			
		||||
!_TAG_PROGRAM_NAME	Exuberant Ctags	//
 | 
			
		||||
!_TAG_PROGRAM_URL	http://ctags.sourceforge.net	/official site/
 | 
			
		||||
!_TAG_PROGRAM_VERSION	5.8	//
 | 
			
		||||
							
								
								
									
										3
									
								
								aircox/cms/tests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								aircox/cms/tests.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
 | 
			
		||||
# Create your tests here.
 | 
			
		||||
							
								
								
									
										21
									
								
								aircox/cms/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								aircox/cms/utils.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
from django.core.urlresolvers import reverse
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_url (website, route, model, kwargs):
 | 
			
		||||
    name = website.name_of_model(model)
 | 
			
		||||
    if not name:
 | 
			
		||||
        return
 | 
			
		||||
    name = route.get_view_name(name)
 | 
			
		||||
    return reverse(name, kwargs = kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def filter_thread (qs, object):
 | 
			
		||||
    model_type = ContentType.objects.get_for_model(object.__class__)
 | 
			
		||||
    return qs.filter(
 | 
			
		||||
        thread_pk = object.pk,
 | 
			
		||||
        thread_type__pk = model_type.pk
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										401
									
								
								aircox/cms/views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										401
									
								
								aircox/cms/views.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,401 @@
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
from django.templatetags.static import static
 | 
			
		||||
from django.shortcuts import render
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
from django.views.generic import ListView, DetailView
 | 
			
		||||
from django.views.generic.base import View, TemplateResponseMixin
 | 
			
		||||
from django.core.paginator import Paginator
 | 
			
		||||
from django.core import serializers
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.utils.html import escape
 | 
			
		||||
 | 
			
		||||
import aircox.cms.routes as routes
 | 
			
		||||
import aircox.cms.utils as utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostBaseView:
 | 
			
		||||
    website = None  # corresponding website
 | 
			
		||||
    title = ''      # title of the page
 | 
			
		||||
    embed = False   # page is embed (if True, only post content is printed
 | 
			
		||||
    classes = ''    # extra classes for the content
 | 
			
		||||
 | 
			
		||||
    def get_base_context (self, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Return a context with all attributes of this classe plus 'view' set
 | 
			
		||||
        to self.
 | 
			
		||||
        """
 | 
			
		||||
        context = {
 | 
			
		||||
            k: getattr(self, k)
 | 
			
		||||
            for k, v in PostBaseView.__dict__.items()
 | 
			
		||||
                if not k.startswith('__')
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if not self.embed:
 | 
			
		||||
            context['menus'] = {
 | 
			
		||||
                k: v.get(self.request, website = self.website, **kwargs)
 | 
			
		||||
                for k, v in {
 | 
			
		||||
                    k: self.website.get_menu(k)
 | 
			
		||||
                    for k in self.website.menu_layouts
 | 
			
		||||
                }.items() if v
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        context['view'] = self
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostListView (PostBaseView, ListView):
 | 
			
		||||
    """
 | 
			
		||||
    List view for posts and children
 | 
			
		||||
    """
 | 
			
		||||
    class Query:
 | 
			
		||||
        """
 | 
			
		||||
        Request availables parameters
 | 
			
		||||
        """
 | 
			
		||||
        embed = False   # view is embedded (only the list is shown)
 | 
			
		||||
        exclude = None  # exclude item of this id
 | 
			
		||||
        order = 'desc'  # order of the list when query
 | 
			
		||||
        fields = None   # fields to show
 | 
			
		||||
        page = 1        # page number
 | 
			
		||||
        q = None        # query search
 | 
			
		||||
 | 
			
		||||
        def __init__ (self, query):
 | 
			
		||||
            if query:
 | 
			
		||||
                self.update(query)
 | 
			
		||||
 | 
			
		||||
        def update (self, query):
 | 
			
		||||
            my_class = self.__class__
 | 
			
		||||
            if type(query) is my_class:
 | 
			
		||||
                self.__dict__.update(query.__dict__)
 | 
			
		||||
                return
 | 
			
		||||
            self.__dict__.update(query)
 | 
			
		||||
 | 
			
		||||
    template_name = 'aircox_cms/list.html'
 | 
			
		||||
    allow_empty = True
 | 
			
		||||
    paginate_by = 50
 | 
			
		||||
    model = None
 | 
			
		||||
 | 
			
		||||
    route = None
 | 
			
		||||
    query = None
 | 
			
		||||
    fields = [ 'date', 'time', 'image', 'title', 'content' ]
 | 
			
		||||
    icon_size = '64x64'
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.query = PostListView.Query(self.query)
 | 
			
		||||
 | 
			
		||||
    def dispatch (self, request, *args, **kwargs):
 | 
			
		||||
        self.route = self.kwargs.get('route') or self.route
 | 
			
		||||
        return super().dispatch(request, *args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self):
 | 
			
		||||
        if self.route:
 | 
			
		||||
            qs = self.route.get_queryset(self.website, self.model, self.request,
 | 
			
		||||
                                         **self.kwargs)
 | 
			
		||||
        else:
 | 
			
		||||
            qs = self.queryset or self.model.objects.all()
 | 
			
		||||
        query = self.query
 | 
			
		||||
 | 
			
		||||
        query.update(self.request.GET)
 | 
			
		||||
        if query.exclude:
 | 
			
		||||
            qs = qs.exclude(id = int(exclude))
 | 
			
		||||
 | 
			
		||||
        if query.embed:
 | 
			
		||||
            self.embed = True
 | 
			
		||||
 | 
			
		||||
        if query.order == 'asc':
 | 
			
		||||
            qs.order_by('date', 'id')
 | 
			
		||||
        else:
 | 
			
		||||
            qs.order_by('-date', '-id')
 | 
			
		||||
 | 
			
		||||
        if query.fields:
 | 
			
		||||
            self.fields = [
 | 
			
		||||
                field for field in query.fields
 | 
			
		||||
                if field in self.__class__.fields
 | 
			
		||||
            ]
 | 
			
		||||
 | 
			
		||||
        return qs
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context.update(self.get_base_context(**kwargs))
 | 
			
		||||
        context.update({
 | 
			
		||||
            'title': self.get_title(),
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
    def get_title (self):
 | 
			
		||||
        if self.title:
 | 
			
		||||
            return self.title
 | 
			
		||||
 | 
			
		||||
        title = self.route and self.route.get_title(self.model, self.request,
 | 
			
		||||
                                                    **self.kwargs)
 | 
			
		||||
        return title
 | 
			
		||||
 | 
			
		||||
    def get_url (self):
 | 
			
		||||
        if self.route:
 | 
			
		||||
            return utils.get_urls(self.website, self.route,
 | 
			
		||||
                                  self.model, self.kwargs)
 | 
			
		||||
        return ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PostDetailView (DetailView, PostBaseView):
 | 
			
		||||
    """
 | 
			
		||||
    Detail view for posts and children
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'aircox_cms/detail.html'
 | 
			
		||||
 | 
			
		||||
    sections = []
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, sections = None, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.sections = sections or []
 | 
			
		||||
 | 
			
		||||
    def get_queryset (self):
 | 
			
		||||
        if self.request.GET.get('embed'):
 | 
			
		||||
            self.embed = True
 | 
			
		||||
 | 
			
		||||
        if self.model:
 | 
			
		||||
            return super().get_queryset().filter(published = True)
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
    def get_object (self, **kwargs):
 | 
			
		||||
        if self.model:
 | 
			
		||||
            object = super().get_object(**kwargs)
 | 
			
		||||
            if object.published:
 | 
			
		||||
                return object
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context.update(self.get_base_context())
 | 
			
		||||
        context.update({
 | 
			
		||||
            'sections': [
 | 
			
		||||
                section.get(self.request, website = self.website, **kwargs)
 | 
			
		||||
                    for section in self.sections
 | 
			
		||||
            ]
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Menu (View):
 | 
			
		||||
    template_name = 'aircox_cms/menu.html'
 | 
			
		||||
 | 
			
		||||
    name = ''
 | 
			
		||||
    tag = 'nav'
 | 
			
		||||
    enabled = True
 | 
			
		||||
    classes = ''
 | 
			
		||||
    position = ''   # top, left, bottom, right, header, footer, page_top, page_bottom
 | 
			
		||||
    sections = None
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.name = self.name or ('menu_' + self.position)
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        return {
 | 
			
		||||
            'name': self.name,
 | 
			
		||||
            'tag': self.tag,
 | 
			
		||||
            'classes': self.classes,
 | 
			
		||||
            'position': self.position,
 | 
			
		||||
            'sections': [
 | 
			
		||||
                section.get(self.request, website = self.website,
 | 
			
		||||
                            object = None, **kwargs)
 | 
			
		||||
                    for section in self.sections
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def get (self, request, website, **kwargs):
 | 
			
		||||
        self.request = request
 | 
			
		||||
        self.website = website
 | 
			
		||||
        context = self.get_context_data(**kwargs)
 | 
			
		||||
        return render_to_string(self.template_name, context)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BaseSection (View):
 | 
			
		||||
    """
 | 
			
		||||
    Base class for sections. Sections are view that can be used in detail view
 | 
			
		||||
    in order to have extra content about a post, or in menus.
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'aircox_cms/base_section.html'
 | 
			
		||||
    kwargs = None   # kwargs argument passed to get
 | 
			
		||||
    tag = 'div'     # container tags
 | 
			
		||||
    classes = ''    # container classes
 | 
			
		||||
    attrs = ''      # container extra attributes
 | 
			
		||||
    content = ''    # content
 | 
			
		||||
    visible = True  # if false renders an empty string
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self):
 | 
			
		||||
        return {
 | 
			
		||||
            'view': self,
 | 
			
		||||
            'tag': self.tag,
 | 
			
		||||
            'classes': self.classes,
 | 
			
		||||
            'attrs': self.attrs,
 | 
			
		||||
            'visible': self.visible,
 | 
			
		||||
            'content': self.content,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def get (self, request, website, **kwargs):
 | 
			
		||||
        self.request = request
 | 
			
		||||
        self.website = website
 | 
			
		||||
        self.kwargs = kwargs
 | 
			
		||||
 | 
			
		||||
        context = self.get_context_data()
 | 
			
		||||
        # get_context_data may call extra function that can change visibility
 | 
			
		||||
        if self.visible:
 | 
			
		||||
            return render_to_string(self.template_name, context)
 | 
			
		||||
        return ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Section (BaseSection):
 | 
			
		||||
    """
 | 
			
		||||
    A Section that can be related to an object.
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'aircox_cms/section.html'
 | 
			
		||||
    object = None
 | 
			
		||||
    object_required = False
 | 
			
		||||
    title = ''
 | 
			
		||||
    header = ''
 | 
			
		||||
    footer = ''
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self):
 | 
			
		||||
        context = super().get_context_data()
 | 
			
		||||
        context.update({
 | 
			
		||||
            'title': self.title,
 | 
			
		||||
            'header': self.header,
 | 
			
		||||
            'footer': self.footer,
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
    def get (self, request, object = None, **kwargs):
 | 
			
		||||
        self.object = object or self.object
 | 
			
		||||
        if self.object_required and not self.object:
 | 
			
		||||
            raise ValueError('object is required by this Section but not given')
 | 
			
		||||
        return super().get(request, **kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Sections:
 | 
			
		||||
    class Image (BaseSection):
 | 
			
		||||
        """
 | 
			
		||||
        Render an image with the given relative url.
 | 
			
		||||
        """
 | 
			
		||||
        url = None
 | 
			
		||||
 | 
			
		||||
        @property
 | 
			
		||||
        def content (self):
 | 
			
		||||
            return '<img src="{}">'.format(
 | 
			
		||||
                        static(self.url),
 | 
			
		||||
                    )
 | 
			
		||||
 | 
			
		||||
    class PostContent (Section):
 | 
			
		||||
        """
 | 
			
		||||
        Render the content of the Post (format the text a bit and escape HTML
 | 
			
		||||
        tags).
 | 
			
		||||
        """
 | 
			
		||||
        @property
 | 
			
		||||
        def content (self):
 | 
			
		||||
            content = escape(self.object.content)
 | 
			
		||||
            content = re.sub(r'(^|\n\n)((\n?[^\n])+)', r'<p>\2</p>', content)
 | 
			
		||||
            content = re.sub(r'\n', r'<br>', content)
 | 
			
		||||
            return content
 | 
			
		||||
 | 
			
		||||
    class PostImage (Section):
 | 
			
		||||
        """
 | 
			
		||||
        Render the image of the Post
 | 
			
		||||
        """
 | 
			
		||||
        @property
 | 
			
		||||
        def content (self):
 | 
			
		||||
            return '<img src="{}" class="post_image">'.format(
 | 
			
		||||
                        self.object.image.url
 | 
			
		||||
                    )
 | 
			
		||||
 | 
			
		||||
    class List (Section):
 | 
			
		||||
        """
 | 
			
		||||
        Section to render list. The context item 'object_list' is used as list of
 | 
			
		||||
        items to render.
 | 
			
		||||
        """
 | 
			
		||||
        class Item:
 | 
			
		||||
            icon = None
 | 
			
		||||
            title = None
 | 
			
		||||
            text = None
 | 
			
		||||
            url = None
 | 
			
		||||
 | 
			
		||||
            def __init__ (self, icon, title = None, text = None, url = None):
 | 
			
		||||
                self.icon = icon
 | 
			
		||||
                self.title = title
 | 
			
		||||
                self.text = text
 | 
			
		||||
 | 
			
		||||
        hide_empty = False      # hides the section if the list is empty
 | 
			
		||||
        use_icons = True        # print icons
 | 
			
		||||
        paginate_by = 0         # number of items
 | 
			
		||||
        icon_size = '32x32'     # icons size
 | 
			
		||||
        template_name = 'aircox_cms/section_list.html'
 | 
			
		||||
 | 
			
		||||
        def get_object_list (self):
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
        def get_context_data (self, **kwargs):
 | 
			
		||||
            object_list = self.get_object_list()
 | 
			
		||||
            self.visibility = True
 | 
			
		||||
            if not object_list and hide_empty:
 | 
			
		||||
                self.visibility = False
 | 
			
		||||
 | 
			
		||||
            context = super().get_context_data(**kwargs)
 | 
			
		||||
            context.update({
 | 
			
		||||
                'classes': context.get('classes') + ' section_list',
 | 
			
		||||
                'icon_size': self.icon_size,
 | 
			
		||||
                'object_list': object_list,
 | 
			
		||||
                'paginate_by': self.paginate_by,
 | 
			
		||||
            })
 | 
			
		||||
            return context
 | 
			
		||||
 | 
			
		||||
    class Urls (List):
 | 
			
		||||
        """
 | 
			
		||||
        Render a list of urls of targets that are Posts
 | 
			
		||||
        """
 | 
			
		||||
        classes = 'section_urls'
 | 
			
		||||
        targets = None
 | 
			
		||||
 | 
			
		||||
        def get_object_list (self):
 | 
			
		||||
            return [
 | 
			
		||||
                List.Item(
 | 
			
		||||
                    target.image or None,
 | 
			
		||||
                    target.title,
 | 
			
		||||
                    url = target.detail_url(),
 | 
			
		||||
                )
 | 
			
		||||
                for target in self.targets
 | 
			
		||||
            ]
 | 
			
		||||
 | 
			
		||||
    class Posts (PostBaseView, Section):
 | 
			
		||||
        """
 | 
			
		||||
        Render a list using PostListView's template.
 | 
			
		||||
        """
 | 
			
		||||
        embed = True
 | 
			
		||||
        paginate_by = 5
 | 
			
		||||
        icon_size = '64x64'
 | 
			
		||||
        fields = [ 'date', 'time', 'image', 'title', 'content' ]
 | 
			
		||||
 | 
			
		||||
        def get_url (self):
 | 
			
		||||
            return ''
 | 
			
		||||
 | 
			
		||||
        def get_object_list (self):
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
        def render_list (self):
 | 
			
		||||
            self.embed = True
 | 
			
		||||
            context = self.get_base_context(**self.kwargs)
 | 
			
		||||
            context.update({
 | 
			
		||||
                'object_list': self.get_object_list(),
 | 
			
		||||
                'embed': True,
 | 
			
		||||
                'paginate_by': self.paginate_by,
 | 
			
		||||
            })
 | 
			
		||||
            return render_to_string(PostListView.template_name, context)
 | 
			
		||||
 | 
			
		||||
        def get_context_data (self, **kwargs):
 | 
			
		||||
            context = super().get_context_data(**kwargs)
 | 
			
		||||
            context['content'] = self.render_list()
 | 
			
		||||
            return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										101
									
								
								aircox/cms/website.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								aircox/cms/website.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,101 @@
 | 
			
		||||
import aircox.cms.routes as routes
 | 
			
		||||
import aircox.cms.views as views
 | 
			
		||||
 | 
			
		||||
class Website:
 | 
			
		||||
    name = ''
 | 
			
		||||
    domain = ''
 | 
			
		||||
    description = 'An aircox website'   # public description (used in meta info)
 | 
			
		||||
    tags = 'aircox,radio,music'         # public keywords (used in meta info)
 | 
			
		||||
 | 
			
		||||
    styles = ''                         # relative url to stylesheet file
 | 
			
		||||
    menus = None                        # list of menus
 | 
			
		||||
    menu_layouts = ['top', 'left',      # available positions
 | 
			
		||||
                    'right', 'bottom',
 | 
			
		||||
                    'header', 'footer']
 | 
			
		||||
    router = None
 | 
			
		||||
    urls = []
 | 
			
		||||
    registry = {}
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, **kwargs):
 | 
			
		||||
        self.registry = {}
 | 
			
		||||
        self.urls = []
 | 
			
		||||
        self.__dict__.update(kwargs)
 | 
			
		||||
 | 
			
		||||
    def name_of_model (self, model):
 | 
			
		||||
        for name, _model in self.registry.items():
 | 
			
		||||
            if model is _model:
 | 
			
		||||
                return name
 | 
			
		||||
 | 
			
		||||
    def register_model (self, name, model):
 | 
			
		||||
        """
 | 
			
		||||
        Register a model and return the name under which it is registered.
 | 
			
		||||
        Raise a ValueError if another model is yet associated under this name.
 | 
			
		||||
        """
 | 
			
		||||
        if name in self.registry and self.registry[name] is not model:
 | 
			
		||||
            raise ValueError('A model has yet been registered under "{}"'
 | 
			
		||||
                             .format(name))
 | 
			
		||||
        self.registry[name] = model
 | 
			
		||||
        return name
 | 
			
		||||
 | 
			
		||||
    def register_detail (self, name, model, view = views.PostDetailView,
 | 
			
		||||
                         **view_kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Register a model and the detail view
 | 
			
		||||
        """
 | 
			
		||||
        name = self.register_model(name, model)
 | 
			
		||||
        view = view.as_view(
 | 
			
		||||
            website = self,
 | 
			
		||||
            model = model,
 | 
			
		||||
            **view_kwargs,
 | 
			
		||||
        )
 | 
			
		||||
        self.urls.append(routes.DetailRoute.as_url(name, model, view))
 | 
			
		||||
        self.registry[name] = model
 | 
			
		||||
 | 
			
		||||
    def register_list (self, name, model, view = views.PostListView,
 | 
			
		||||
                       routes = [], **view_kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Register a model and the given list view using the given routes
 | 
			
		||||
        """
 | 
			
		||||
        name = self.register_model(name, model)
 | 
			
		||||
        view = view.as_view(
 | 
			
		||||
            website = self,
 | 
			
		||||
            model = model,
 | 
			
		||||
            **view_kwargs
 | 
			
		||||
        )
 | 
			
		||||
        self.urls += [ route.as_url(name, model, view) for route in routes ]
 | 
			
		||||
        self.registry[name] = model
 | 
			
		||||
 | 
			
		||||
    def register (self, name, model, sections = None, routes = None,
 | 
			
		||||
                  list_kwargs = {}, detail_kwargs = {}):
 | 
			
		||||
        if sections:
 | 
			
		||||
            self.register_detail(
 | 
			
		||||
                name, model,
 | 
			
		||||
                sections = sections,
 | 
			
		||||
                **detail_kwargs
 | 
			
		||||
            )
 | 
			
		||||
        if routes:
 | 
			
		||||
            self.register_list(
 | 
			
		||||
                name, model,
 | 
			
		||||
                routes = routes,
 | 
			
		||||
                **list_kwargs
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    def get_menu (self, position):
 | 
			
		||||
        """
 | 
			
		||||
        Get an enabled menu by its position
 | 
			
		||||
        """
 | 
			
		||||
        for menu in self.menus:
 | 
			
		||||
            if menu.enabled and menu.position == position:
 | 
			
		||||
                self.check_menu_tag(menu)
 | 
			
		||||
                return menu
 | 
			
		||||
 | 
			
		||||
    def check_menu_tag (self, menu):
 | 
			
		||||
        """
 | 
			
		||||
        Update menu tag if it is a footer or a header
 | 
			
		||||
        """
 | 
			
		||||
        if menu.position in ('footer','header'):
 | 
			
		||||
            menu.tag = menu.position
 | 
			
		||||
        if menu.position in ('left', 'right'):
 | 
			
		||||
            menu.tag = 'side'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										14
									
								
								aircox/liquidsoap/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								aircox/liquidsoap/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
# Aircox LiquidSoap
 | 
			
		||||
This application makes the bridge between Aircox and LiquidSoap. It can monitor scheduled and streamed programs and offer some controls on LiquidSoap.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## manage.py's commands
 | 
			
		||||
** liquidsoap **: monitor LiquidSoap, logs what is playing on the different sources, and plays scheduled diffusions;
 | 
			
		||||
** liquidsoap_files**: generates playlists and LiquidSoap config based on Programs' parameters;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Requirements
 | 
			
		||||
* Liquidsoap
 | 
			
		||||
* requirements.txt for python's dependencies
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								aircox/liquidsoap/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								aircox/liquidsoap/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										3
									
								
								aircox/liquidsoap/admin.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								aircox/liquidsoap/admin.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
from django.contrib import admin
 | 
			
		||||
 | 
			
		||||
# Register your models here.
 | 
			
		||||
							
								
								
									
										55
									
								
								aircox/liquidsoap/management/commands/liquidsoap.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								aircox/liquidsoap/management/commands/liquidsoap.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,55 @@
 | 
			
		||||
"""
 | 
			
		||||
Monitor Liquidsoap's sources, logs, and even print what's on air.
 | 
			
		||||
"""
 | 
			
		||||
import time
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
 | 
			
		||||
import aircox.liquidsoap.settings as settings
 | 
			
		||||
import aircox.liquidsoap.utils as utils
 | 
			
		||||
import aircox.programs.models as models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help= __doc__
 | 
			
		||||
    output_dir = settings.AIRCOX_LIQUIDSOAP_MEDIA
 | 
			
		||||
 | 
			
		||||
    def add_arguments (self, parser):
 | 
			
		||||
        parser.formatter_class=RawTextHelpFormatter
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-o', '--on_air', action='store_true',
 | 
			
		||||
            help='Print what is on air'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-m', '--monitor', action='store_true',
 | 
			
		||||
            help='Runs in monitor mode'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-d', '--delay', type=int,
 | 
			
		||||
            default=1000,
 | 
			
		||||
            help='Time to sleep in milliseconds before update on monitor'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        connector = utils.Connector()
 | 
			
		||||
        self.monitor = utils.Monitor(connector)
 | 
			
		||||
        self.monitor.update()
 | 
			
		||||
 | 
			
		||||
        if options.get('on_air'):
 | 
			
		||||
            for id, controller in self.monitor.controller.items():
 | 
			
		||||
                print(id, controller.on_air)
 | 
			
		||||
 | 
			
		||||
        if options.get('monitor'):
 | 
			
		||||
            delay = options.get('delay') / 1000
 | 
			
		||||
            while True:
 | 
			
		||||
                for controller in self.monitor.controllers.values():
 | 
			
		||||
                    try:
 | 
			
		||||
                        controller.monitor()
 | 
			
		||||
                    except Exception, e:
 | 
			
		||||
                        print(e)
 | 
			
		||||
                time.sleep(delay)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										119
									
								
								aircox/liquidsoap/management/commands/liquidsoap_files.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								aircox/liquidsoap/management/commands/liquidsoap_files.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,119 @@
 | 
			
		||||
"""
 | 
			
		||||
Generate configuration files and playlists for liquidsoap using settings, streams and
 | 
			
		||||
so on
 | 
			
		||||
"""
 | 
			
		||||
import os
 | 
			
		||||
import re
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
from django.views.generic.base import View
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
 | 
			
		||||
import aircox.liquidsoap.settings as settings
 | 
			
		||||
import aircox.liquidsoap.utils as utils
 | 
			
		||||
import aircox.programs.settings as programs_settings
 | 
			
		||||
import aircox.programs.models as models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help= __doc__
 | 
			
		||||
    output_dir = settings.AIRCOX_LIQUIDSOAP_MEDIA
 | 
			
		||||
 | 
			
		||||
    def add_arguments (self, parser):
 | 
			
		||||
        parser.formatter_class=RawTextHelpFormatter
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            'output', metavar='PATH', type=str, nargs='?',
 | 
			
		||||
            help='force output to file (- to stdout) for single actions; to a '
 | 
			
		||||
                 'given dir when using --all')
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-c', '--config', action='store_true',
 | 
			
		||||
            help='Generate liquidsoap config file'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-d', '--diffusion', action='store_true',
 | 
			
		||||
            help='Generate the playlist for the current scheduled diffusion'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-s', '--stream', type=int,
 | 
			
		||||
            help='Generate the playlist of a stream with the given id'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-S', '--streams', action='store_true',
 | 
			
		||||
            help='Generate all stream playlists'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-a', '--all', action='store_true',
 | 
			
		||||
            help='Generate all playlists (stream and scheduled diffusion) '
 | 
			
		||||
                 'and config file'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        output = options.get('output') or None
 | 
			
		||||
        if options.get('config'):
 | 
			
		||||
            data = self.get_config(output = output)
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if options.get('stream'):
 | 
			
		||||
            stream = options['stream']
 | 
			
		||||
            if type(stream) is int:
 | 
			
		||||
                stream = models.Stream.objects.get(id = stream,
 | 
			
		||||
                                                   program__active = True)
 | 
			
		||||
 | 
			
		||||
            data = self.get_playlist(stream, output = output)
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if options.get('all') or options.get('streams'):
 | 
			
		||||
            if output:
 | 
			
		||||
                if not os.path.isdir(output):
 | 
			
		||||
                    raise CommandError('given output is not a directory')
 | 
			
		||||
                self.output_dir = output
 | 
			
		||||
 | 
			
		||||
            if options.get('all'):
 | 
			
		||||
                self.handle(config = True)
 | 
			
		||||
 | 
			
		||||
            for stream in models.Stream.objects.filter(program__active = True):
 | 
			
		||||
                self.handle(stream = stream)
 | 
			
		||||
            self.output_dir = settings.AIRCOX_LIQUIDSOAP_MEDIA
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        raise CommandError('nothing to do')
 | 
			
		||||
 | 
			
		||||
    def print (self, data, path, default):
 | 
			
		||||
        if path and path == '-':
 | 
			
		||||
            print(data)
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if not path:
 | 
			
		||||
            path = os.path.join(self.output_dir, default)
 | 
			
		||||
        with open(path, 'w+') as file:
 | 
			
		||||
            file.write(data)
 | 
			
		||||
 | 
			
		||||
    def get_config (self, output = None):
 | 
			
		||||
        context = {
 | 
			
		||||
            'monitor': utils.Monitor(),
 | 
			
		||||
            'settings': settings,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        data = render_to_string('aircox_liquidsoap/config.liq', context)
 | 
			
		||||
        data = re.sub(r'\s*\\\n', r'#\\n#', data)
 | 
			
		||||
        data = data.replace('\n', '')
 | 
			
		||||
        data = re.sub(r'#\\n#', '\n', data)
 | 
			
		||||
        self.print(data, output, 'aircox.liq')
 | 
			
		||||
 | 
			
		||||
    def get_playlist (self, stream = None, output = None):
 | 
			
		||||
        program = stream.program
 | 
			
		||||
        source = utils.Source(program = program)
 | 
			
		||||
 | 
			
		||||
        sounds = models.Sound.objects.filter(
 | 
			
		||||
            # good_quality = True,
 | 
			
		||||
            type = models.Sound.Type['archive'],
 | 
			
		||||
            path__startswith = os.path.join(
 | 
			
		||||
                programs_settings.AIRCOX_SOUND_ARCHIVES_SUBDIR,
 | 
			
		||||
                program.path
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        data = '\n'.join(sound.path for sound in sounds)
 | 
			
		||||
        self.print(data, output, utils.Source(program = program).path)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								aircox/liquidsoap/models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								aircox/liquidsoap/models.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
from django.db import models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										24
									
								
								aircox/liquidsoap/settings.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								aircox/liquidsoap/settings.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
def ensure (key, default):
 | 
			
		||||
    globals()[key] = getattr(settings, key, default)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ensure('AIRCOX_LIQUIDSOAP_SOCKET', '/tmp/liquidsoap.sock')
 | 
			
		||||
 | 
			
		||||
# dict of values to set (do not forget to escape chars)
 | 
			
		||||
ensure('AIRCOX_LIQUIDSOAP_SET', {
 | 
			
		||||
    'log.file.path': '"/tmp/liquidsoap.log"',
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
# security source: used when no source are available
 | 
			
		||||
ensure('AIRCOX_LIQUIDSOAP_SECURITY_SOURCE', '/media/data/musique/creation/Mega Combi/MegaCombi241-PT134-24062015_Comme_des_lyca_ens.mp3')
 | 
			
		||||
 | 
			
		||||
# start the server on monitor if not present
 | 
			
		||||
ensure('AIRCOX_LIQUIDSOAP_AUTOSTART', True)
 | 
			
		||||
 | 
			
		||||
# output directory for the generated files
 | 
			
		||||
ensure('AIRCOX_LIQUIDSOAP_MEDIA', '/tmp')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,14 @@
 | 
			
		||||
{% with metadata=source.metadata %}
 | 
			
		||||
<div class="source {% if metadata.initial_uri == controller.master.metadata.initial_uri %}on_air{% endif %}">
 | 
			
		||||
    <h2>{{ source.name }}</h2>
 | 
			
		||||
    <time>{{ metadata.on_air }}</time>
 | 
			
		||||
    <span class="path">{{ metadata.initial_uri }}</span>
 | 
			
		||||
    <span class="status" status="{{ metadata.status }}">{{ metadata.status }}</span>
 | 
			
		||||
 | 
			
		||||
    <button onclick="liquid_action('{{controller.id}}','{{source.id}}','skip');">
 | 
			
		||||
        skip
 | 
			
		||||
    </button>
 | 
			
		||||
</div>
 | 
			
		||||
{% endwith %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										26
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/config.liq
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/config.liq
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
{# Utilities #}
 | 
			
		||||
def interactive_source (id, s, ) = \
 | 
			
		||||
    s = store_metadata(id=id, size=1, s) \
 | 
			
		||||
    add_skip_command(s) \
 | 
			
		||||
    s \
 | 
			
		||||
end \
 | 
			
		||||
\
 | 
			
		||||
def stream (id, file) = \
 | 
			
		||||
    s = playlist(id = '#{id}_playlist', mode = "random", file) \
 | 
			
		||||
    interactive_source(id, s) \
 | 
			
		||||
end \
 | 
			
		||||
\
 | 
			
		||||
{# Config #}
 | 
			
		||||
set("server.socket", true) \
 | 
			
		||||
set("server.socket.path", "{{ settings.AIRCOX_LIQUIDSOAP_SOCKET }}") \
 | 
			
		||||
{% for key, value in settings.AIRCOX_LIQUIDSOAP_SET.items %}
 | 
			
		||||
set("{{ key|safe }}", {{ value|safe }}) \
 | 
			
		||||
{% endfor %}
 | 
			
		||||
\
 | 
			
		||||
\
 | 
			
		||||
{% for controller in monitor.controllers.values %}
 | 
			
		||||
{% include 'aircox_liquidsoap/station.liq' %} \
 | 
			
		||||
{{ controller.id }} = make_station_{{ controller.id }}() \
 | 
			
		||||
output.alsa({{ controller.id }}) \
 | 
			
		||||
{% endfor %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										134
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/controller.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/controller.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,134 @@
 | 
			
		||||
{% if not embed %}
 | 
			
		||||
<html>
 | 
			
		||||
    <head>
 | 
			
		||||
        <style>
 | 
			
		||||
        .station {
 | 
			
		||||
            margin: 2em;
 | 
			
		||||
            border: 1px grey solid;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            .sources {
 | 
			
		||||
                padding: 0.5em;
 | 
			
		||||
                box-shadow: inset 0.1em 0.1em 0.5em rgba(0, 0, 0, 0.5);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        .station h1 {
 | 
			
		||||
            font-size: 1.2em;
 | 
			
		||||
            margin: 0.2em;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .source {
 | 
			
		||||
            border-left: 0.5em solid grey;
 | 
			
		||||
            font-size: 0.9em;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            .on_air {
 | 
			
		||||
                display: block;
 | 
			
		||||
                border-left: 0.5em solid #f00;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            .source h2 {
 | 
			
		||||
                display: inline-block;
 | 
			
		||||
                min-width: 10em;
 | 
			
		||||
                font-size: 1em;
 | 
			
		||||
                margin: 0.2em;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            .source time {
 | 
			
		||||
                display: inline-block;
 | 
			
		||||
                margin-right: 2em;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            .source span {
 | 
			
		||||
                font-size: 1em;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        .error {
 | 
			
		||||
            padding: 0.2em;
 | 
			
		||||
            color: red;
 | 
			
		||||
            font-weight: bold;
 | 
			
		||||
        }
 | 
			
		||||
        </style>
 | 
			
		||||
 | 
			
		||||
        <script>
 | 
			
		||||
 | 
			
		||||
            function get_token() {
 | 
			
		||||
                return document.cookie.replace(/.*csrftoken=([^;]+)(;.*|$)/, '$1');
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            function liquid_action (controller, source, action) {
 | 
			
		||||
                params = 'controller=' + controller + '&&source=' + source +
 | 
			
		||||
                         '&&action=' + action;
 | 
			
		||||
 | 
			
		||||
                req = new XMLHttpRequest()
 | 
			
		||||
                req.open('POST', '{% url 'liquid-controller' %}', false);
 | 
			
		||||
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 | 
			
		||||
                req.setRequestHeader("Content-length", params.length);
 | 
			
		||||
                req.setRequestHeader("Connection", "close");
 | 
			
		||||
                req.setRequestHeader("X-CSRFToken", get_token());
 | 
			
		||||
 | 
			
		||||
                req.send(params);
 | 
			
		||||
                liquid_update()
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            function liquid_update (update) {
 | 
			
		||||
                req = new XMLHttpRequest()
 | 
			
		||||
                req.open('GET', '{% url 'liquid-controller' %}?embed', true);
 | 
			
		||||
 | 
			
		||||
                req.onreadystatechange = function() {
 | 
			
		||||
                    if(req.readyState != 4 || (req.status != 200 && req.status != 0))
 | 
			
		||||
                        return;
 | 
			
		||||
                    document.getElementById('liquid-stations').innerHTML =
 | 
			
		||||
                        req.responseText;
 | 
			
		||||
 | 
			
		||||
                    if(update)
 | 
			
		||||
                        window.setTimeout(function() { liquid_update(update);}, 5000);
 | 
			
		||||
                };
 | 
			
		||||
                req.send();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            liquid_update(true);
 | 
			
		||||
        </script>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
        <main id="liquid-stations">
 | 
			
		||||
{% endif %}
 | 
			
		||||
            {% for c_id, controller in monitor.controllers.items %}
 | 
			
		||||
            {% with on_air=controller.on_air %}
 | 
			
		||||
            <div id="{{ c_id }}" class="station">
 | 
			
		||||
                <header>
 | 
			
		||||
                    {% if not controller.connector.available %}
 | 
			
		||||
                    <span class="error" style="float:right;">disconnected</span>
 | 
			
		||||
                    {% endif %}
 | 
			
		||||
                    <h1>
 | 
			
		||||
                        {{ controller.station.name }}
 | 
			
		||||
                    </h1>
 | 
			
		||||
                </header>
 | 
			
		||||
                <div class="sources">
 | 
			
		||||
                    {% with source=controller.master %}
 | 
			
		||||
                    {% include 'aircox_liquidsoap/base_source.html' %}
 | 
			
		||||
                    {% endwith %}
 | 
			
		||||
 | 
			
		||||
                    {% with source=controller.dealer %}
 | 
			
		||||
                    {% include 'aircox_liquidsoap/base_source.html' %}
 | 
			
		||||
                    {% endwith %}
 | 
			
		||||
 | 
			
		||||
                    {% for source in controller.streams.values %}
 | 
			
		||||
                    {% include 'aircox_liquidsoap/base_source.html' %}
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="next">
 | 
			
		||||
                    {% for diffusion in controller.next_diffusions %}
 | 
			
		||||
                    {{ diffusion }}
 | 
			
		||||
                    {% endfor %}
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
            {% endwith %}
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
 | 
			
		||||
{% if not embed %}
 | 
			
		||||
        </main>
 | 
			
		||||
    </body>
 | 
			
		||||
</html>
 | 
			
		||||
{% endif %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										54
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/station.liq
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								aircox/liquidsoap/templates/aircox_liquidsoap/station.liq
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
			
		||||
{% comment %}
 | 
			
		||||
A station has multiple sources:
 | 
			
		||||
- dealer: a controlled playlist playing once each track, that reloads on file
 | 
			
		||||
    change. This is used for scheduled sounds.
 | 
			
		||||
- streams: a rotate source with all playlists
 | 
			
		||||
- single: security song
 | 
			
		||||
{% endcomment %}
 | 
			
		||||
def make_station_{{ controller.id }} () = \
 | 
			
		||||
    {# dealer #}
 | 
			
		||||
    {% with source=controller.dealer %}
 | 
			
		||||
    {% if source %}
 | 
			
		||||
    dealer = interactive_source('{{ source.id }}', playlist.once( \
 | 
			
		||||
        reload_mode='watch', \
 | 
			
		||||
        "{{ source.path }}", \
 | 
			
		||||
    )) \
 | 
			
		||||
    \
 | 
			
		||||
    dealer_on = interactive.bool("{{ source.id }}_on", false) \
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% endwith %}
 | 
			
		||||
    \
 | 
			
		||||
    {# streams #}
 | 
			
		||||
    streams = interactive_source("streams", rotate([ \
 | 
			
		||||
    {% for source in controller.streams.values %}
 | 
			
		||||
    {% with info=source.stream_info %}
 | 
			
		||||
    {% if info.delay %}
 | 
			
		||||
        delay({{ info.delay }}., stream("{{ source.id }}", "{{ source.path }}")), \
 | 
			
		||||
    {% elif info.begin and info.end %}
 | 
			
		||||
        at({ {{info.begin}}-{{info.end}} }, stream("{{ source.id }}", "{{ source.path }}")), \
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% endwith %}
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
 | 
			
		||||
    {% for source in controller.streams.values %}
 | 
			
		||||
    {% if not source.stream_info %}
 | 
			
		||||
        stream("{{ source.id }}", "{{ source.path }}"), \
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
    ])) \
 | 
			
		||||
    \
 | 
			
		||||
    {# station #}
 | 
			
		||||
    interactive_source ( \
 | 
			
		||||
        "{{ controller.id }}", \
 | 
			
		||||
        fallback(track_sensitive = false, [ \
 | 
			
		||||
            at(dealer_on, dealer), \
 | 
			
		||||
            streams, \
 | 
			
		||||
        {% if controller.station.fallback %}
 | 
			
		||||
            single("{{ controller.station.fallback }}"), \
 | 
			
		||||
        {% else %}
 | 
			
		||||
            blank(), \
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        ]) \
 | 
			
		||||
    ) \
 | 
			
		||||
end \
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								aircox/liquidsoap/tests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								aircox/liquidsoap/tests.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
 | 
			
		||||
# Create your tests here.
 | 
			
		||||
							
								
								
									
										9
									
								
								aircox/liquidsoap/urls.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								aircox/liquidsoap/urls.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
from django.conf.urls import url
 | 
			
		||||
 | 
			
		||||
import aircox.liquidsoap.views as views
 | 
			
		||||
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
    url('^controller/', views.LiquidControl.as_view(),  name = 'liquid-controller'),
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										441
									
								
								aircox/liquidsoap/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										441
									
								
								aircox/liquidsoap/utils.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,441 @@
 | 
			
		||||
import os
 | 
			
		||||
import socket
 | 
			
		||||
import re
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
 | 
			
		||||
from aircox.programs.utils import to_timedelta
 | 
			
		||||
import aircox.programs.models as models
 | 
			
		||||
import aircox.liquidsoap.settings as settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Connector:
 | 
			
		||||
    """
 | 
			
		||||
    Telnet connector utility.
 | 
			
		||||
 | 
			
		||||
    address: a string to the unix domain socket file, or a tuple
 | 
			
		||||
        (host, port) for TCP/IP connection
 | 
			
		||||
    """
 | 
			
		||||
    __socket = None
 | 
			
		||||
    __available = False
 | 
			
		||||
    address = settings.AIRCOX_LIQUIDSOAP_SOCKET
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def available (self):
 | 
			
		||||
        return self.__available
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, address = None):
 | 
			
		||||
        if address:
 | 
			
		||||
            self.address = address
 | 
			
		||||
 | 
			
		||||
    def open (self):
 | 
			
		||||
        if self.__available:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            family = socket.AF_INET if type(self.address) in (tuple, list) else \
 | 
			
		||||
                     socket.AF_UNIX
 | 
			
		||||
            self.__socket = socket.socket(family, socket.SOCK_STREAM)
 | 
			
		||||
            self.__socket.connect(self.address)
 | 
			
		||||
            self.__available = True
 | 
			
		||||
        except:
 | 
			
		||||
            # print('can not connect to liquidsoap socket {}'.format(self.address))
 | 
			
		||||
            self.__available = False
 | 
			
		||||
            return -1
 | 
			
		||||
 | 
			
		||||
    def send (self, *data, try_count = 1, parse = False, parse_json = False):
 | 
			
		||||
        if self.open():
 | 
			
		||||
            return ''
 | 
			
		||||
        data = bytes(''.join([str(d) for d in data]) + '\n', encoding='utf-8')
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            reg = re.compile(r'(.*)\s+END\s*$')
 | 
			
		||||
            self.__socket.sendall(data)
 | 
			
		||||
            data = ''
 | 
			
		||||
            while not reg.search(data):
 | 
			
		||||
                data += self.__socket.recv(1024).decode('unicode_escape')
 | 
			
		||||
 | 
			
		||||
            if data:
 | 
			
		||||
                data = reg.sub(r'\1', data)
 | 
			
		||||
                data = data.strip()
 | 
			
		||||
 | 
			
		||||
                if parse:
 | 
			
		||||
                    data = self.parse(data)
 | 
			
		||||
                elif parse_json:
 | 
			
		||||
                    data = self.parse_json(data)
 | 
			
		||||
            return data
 | 
			
		||||
        except:
 | 
			
		||||
            self.__available = False
 | 
			
		||||
            if try_count > 0:
 | 
			
		||||
                return self.send(data, try_count - 1)
 | 
			
		||||
 | 
			
		||||
    def parse (self, string):
 | 
			
		||||
        string = string.split('\n')
 | 
			
		||||
        data = {}
 | 
			
		||||
        for line in string:
 | 
			
		||||
            line = re.search(r'(?P<key>[^=]+)="?(?P<value>([^"]|\\")+)"?', line)
 | 
			
		||||
            if not line:
 | 
			
		||||
                continue
 | 
			
		||||
            line = line.groupdict()
 | 
			
		||||
            data[line['key']] = line['value']
 | 
			
		||||
        return data
 | 
			
		||||
 | 
			
		||||
    def parse_json (self, string):
 | 
			
		||||
        try:
 | 
			
		||||
            if string[0] == '"' and string[-1] == '"':
 | 
			
		||||
                string = string[1:-1]
 | 
			
		||||
            return json.loads(string) if string else None
 | 
			
		||||
        except:
 | 
			
		||||
            return None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Source:
 | 
			
		||||
    """
 | 
			
		||||
    A structure that holds informations about a LiquidSoap source.
 | 
			
		||||
    """
 | 
			
		||||
    controller = None
 | 
			
		||||
    program = None
 | 
			
		||||
    metadata = None
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, controller = None, program = None):
 | 
			
		||||
        self.controller = controller
 | 
			
		||||
        self.program = program
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def station (self):
 | 
			
		||||
        """
 | 
			
		||||
        Proxy to self.(program|controller).station
 | 
			
		||||
        """
 | 
			
		||||
        return self.program.station if self.program else \
 | 
			
		||||
                self.controller.station
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def connector (self):
 | 
			
		||||
        """
 | 
			
		||||
        Proxy to self.controller.connector
 | 
			
		||||
        """
 | 
			
		||||
        return self.controller.connector
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def id (self):
 | 
			
		||||
        """
 | 
			
		||||
        Identifier for the source, scoped in the station's one
 | 
			
		||||
        """
 | 
			
		||||
        postfix = ('_stream_' + str(self.program.id)) if self.program else ''
 | 
			
		||||
        return self.station.slug + postfix
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def name (self):
 | 
			
		||||
        """
 | 
			
		||||
        Name of the related object (program or station)
 | 
			
		||||
        """
 | 
			
		||||
        if self.program:
 | 
			
		||||
            return self.program.name
 | 
			
		||||
        return self.station.name
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def path (self):
 | 
			
		||||
        """
 | 
			
		||||
        Path to the playlist
 | 
			
		||||
        """
 | 
			
		||||
        return os.path.join(
 | 
			
		||||
            settings.AIRCOX_LIQUIDSOAP_MEDIA,
 | 
			
		||||
            self.id + '.m3u'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def playlist (self):
 | 
			
		||||
        """
 | 
			
		||||
        Get or set the playlist as an array, and update it into
 | 
			
		||||
        the corresponding file.
 | 
			
		||||
        """
 | 
			
		||||
        try:
 | 
			
		||||
            with open(self.path, 'r') as file:
 | 
			
		||||
                return file.readlines()
 | 
			
		||||
        except:
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
    @playlist.setter
 | 
			
		||||
    def playlist (self, sounds):
 | 
			
		||||
        with open(self.path, 'w') as file:
 | 
			
		||||
            file.write('\n'.join(sounds))
 | 
			
		||||
            self.connector.send(self.name, '_playlist.reload')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def current_sound (self):
 | 
			
		||||
        self.update()
 | 
			
		||||
        return self.metadata.get('initial_uri') if self.metadata else {}
 | 
			
		||||
 | 
			
		||||
    def stream_info (self):
 | 
			
		||||
        """
 | 
			
		||||
        Return a dict with info related to the program's stream
 | 
			
		||||
        """
 | 
			
		||||
        if not self.program:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        stream = models.Stream.objects.get(program = self.program)
 | 
			
		||||
        if not stream.begin and not stream.delay:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        def to_seconds (time):
 | 
			
		||||
            return 3600 * time.hour + 60 * time.minute + time.second
 | 
			
		||||
 | 
			
		||||
        return {
 | 
			
		||||
            'begin': stream.begin.strftime('%Hh%M') if stream.begin else None,
 | 
			
		||||
            'end': stream.end.strftime('%Hh%M') if stream.end else None,
 | 
			
		||||
            'delay': to_seconds(stream.delay) if stream.delay else None
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def skip (self):
 | 
			
		||||
        """
 | 
			
		||||
        Skip a given source. If no source, use master.
 | 
			
		||||
        """
 | 
			
		||||
        self.connector.send(self.id, '.skip')
 | 
			
		||||
 | 
			
		||||
    def update (self, metadata = None):
 | 
			
		||||
        """
 | 
			
		||||
        Update metadata with the given metadata dict or request them to
 | 
			
		||||
        liquidsoap if nothing is given.
 | 
			
		||||
 | 
			
		||||
        Return -1 in case no update happened
 | 
			
		||||
        """
 | 
			
		||||
        if metadata is not None:
 | 
			
		||||
            source = metadata.get('source') or ''
 | 
			
		||||
            if self.program and not source.startswith(self.id):
 | 
			
		||||
                return -1
 | 
			
		||||
            self.metadata = metadata
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        # r = self.connector.send('var.get ', self.id + '_meta', parse_json=True)
 | 
			
		||||
        r = self.connector.send(self.id, '.get', parse=True)
 | 
			
		||||
        return self.update(metadata = r or {})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Master (Source):
 | 
			
		||||
    """
 | 
			
		||||
    A master Source
 | 
			
		||||
    """
 | 
			
		||||
    def update (self, metadata = None):
 | 
			
		||||
        if metadata is not None:
 | 
			
		||||
            return super().update(metadata)
 | 
			
		||||
 | 
			
		||||
        r = self.connector.send('request.on_air')
 | 
			
		||||
        r = self.connector.send('request.metadata ', r, parse = True)
 | 
			
		||||
        return self.update(metadata = r or {})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Dealer (Source):
 | 
			
		||||
    """
 | 
			
		||||
    The Dealer source is a source that is used for scheduled diffusions and
 | 
			
		||||
    manual sound diffusion.
 | 
			
		||||
    """
 | 
			
		||||
    name = _('Dealer')
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def id (self):
 | 
			
		||||
        return self.station.slug + '_dealer'
 | 
			
		||||
 | 
			
		||||
    def stream_info (self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def on (self):
 | 
			
		||||
        r = self.connector.send('var.get ', self.id, '_on')
 | 
			
		||||
        return (r == 'true')
 | 
			
		||||
 | 
			
		||||
    @on.setter
 | 
			
		||||
    def on (self, value):
 | 
			
		||||
        return self.connector.send('var.set ', self.id, '_on',
 | 
			
		||||
                                    '=', 'true' if value else 'false')
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def playlist (self):
 | 
			
		||||
        try:
 | 
			
		||||
            with open(self.path, 'r') as file:
 | 
			
		||||
                return file.readlines()
 | 
			
		||||
        except:
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
    @playlist.setter
 | 
			
		||||
    def playlist (self, sounds):
 | 
			
		||||
        with open(self.path, 'w') as file:
 | 
			
		||||
            file.write('\n'.join(sounds))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def __get_next (self, date, on_air):
 | 
			
		||||
        """
 | 
			
		||||
        Return which diffusion should be played now and not playing
 | 
			
		||||
        """
 | 
			
		||||
        r = [ models.Diffusion.get_prev(self.station, date),
 | 
			
		||||
              models.Diffusion.get_next(self.station, date) ]
 | 
			
		||||
        r = [ diffusion.prefetch_related('sounds')[0]
 | 
			
		||||
                for diffusion in r if diffusion.count() ]
 | 
			
		||||
 | 
			
		||||
        for diffusion in r:
 | 
			
		||||
            duration = to_timedelta(diffusion.archives_duration())
 | 
			
		||||
            end_at = diffusion.date + duration
 | 
			
		||||
            if end_at < date:
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            diffusion.playlist = [ sound.path
 | 
			
		||||
                                    for sound in diffusion.get_archives() ]
 | 
			
		||||
            if diffusion.playlist and on_air not in diffusion.playlist:
 | 
			
		||||
                return diffusion
 | 
			
		||||
 | 
			
		||||
    def monitor (self):
 | 
			
		||||
        """
 | 
			
		||||
        Monitor playlist (if it is time to load) and if it time to trigger
 | 
			
		||||
        the button to start a diffusion.
 | 
			
		||||
        """
 | 
			
		||||
        playlist = self.playlist
 | 
			
		||||
        on_air = self.current_sound
 | 
			
		||||
        now = tz.make_aware(tz.datetime.now())
 | 
			
		||||
 | 
			
		||||
        diff = self.__get_next(now, on_air)
 | 
			
		||||
        if not diff:
 | 
			
		||||
            return # there is nothing we can do
 | 
			
		||||
 | 
			
		||||
        # playlist reload
 | 
			
		||||
        if self.playlist != diff.playlist:
 | 
			
		||||
            if not playlist or on_air == playlist[-1] or \
 | 
			
		||||
                on_air not in playlist:
 | 
			
		||||
                self.on = False
 | 
			
		||||
                self.playlist = diff.playlist
 | 
			
		||||
 | 
			
		||||
        # run the diff
 | 
			
		||||
        if self.playlist == diff.playlist and diff.date <= now:
 | 
			
		||||
            self.on = True
 | 
			
		||||
            for source in self.controller.streams.values():
 | 
			
		||||
                source.skip()
 | 
			
		||||
            self.controller.log(
 | 
			
		||||
                source = self.id,
 | 
			
		||||
                date = now,
 | 
			
		||||
                comment = 'trigger the scheduled diffusion to liquidsoap; '
 | 
			
		||||
                          'skip all other streams',
 | 
			
		||||
                related_object = diff,
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Controller:
 | 
			
		||||
    connector = None
 | 
			
		||||
    station = None      # the related station
 | 
			
		||||
    master = None       # master source (station's source)
 | 
			
		||||
    dealer = None       # dealer source
 | 
			
		||||
    streams = None      # streams streams
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def on_air (self):
 | 
			
		||||
        return self.master
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def id (self):
 | 
			
		||||
        return self.master and self.master.id
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def name (self):
 | 
			
		||||
        return self.master and self.master.name
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, station, connector = None):
 | 
			
		||||
        """
 | 
			
		||||
        use_connector: avoids the creation of a Connector, in case it is not needed
 | 
			
		||||
        """
 | 
			
		||||
        self.connector = connector
 | 
			
		||||
        self.station = station
 | 
			
		||||
        self.station.controller = self
 | 
			
		||||
 | 
			
		||||
        self.master = Master(self)
 | 
			
		||||
        self.dealer = Dealer(self)
 | 
			
		||||
        self.streams = {
 | 
			
		||||
            source.id : source
 | 
			
		||||
            for source in [
 | 
			
		||||
                Source(self, program)
 | 
			
		||||
                for program in models.Program.objects.filter(station = station,
 | 
			
		||||
                                                             active = True)
 | 
			
		||||
                if program.stream_set.count()
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def get (self, source_id):
 | 
			
		||||
        """
 | 
			
		||||
        Get a source by its id
 | 
			
		||||
        """
 | 
			
		||||
        if source_id == self.master.id:
 | 
			
		||||
            return self.master
 | 
			
		||||
        if source_id == self.dealer.id:
 | 
			
		||||
            return self.dealer
 | 
			
		||||
        return self.streams.get(source_id)
 | 
			
		||||
 | 
			
		||||
    def log (self, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Create a log using **kwargs, and print info
 | 
			
		||||
        """
 | 
			
		||||
        log = models.Log(**kwargs)
 | 
			
		||||
        log.save()
 | 
			
		||||
        log.print()
 | 
			
		||||
 | 
			
		||||
    def update_all (self):
 | 
			
		||||
        """
 | 
			
		||||
        Fetch and update all streams metadata.
 | 
			
		||||
        """
 | 
			
		||||
        self.master.update()
 | 
			
		||||
        self.dealer.update()
 | 
			
		||||
        for source in self.streams.values():
 | 
			
		||||
            source.update()
 | 
			
		||||
 | 
			
		||||
    def __change_log (self, source):
 | 
			
		||||
        last_log = models.Log.objects.filter(
 | 
			
		||||
            source = source.id,
 | 
			
		||||
        ).prefetch_related('sound').order_by('-date')
 | 
			
		||||
 | 
			
		||||
        on_air = source.current_sound
 | 
			
		||||
        if not on_air:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if last_log:
 | 
			
		||||
            last_log = last_log[0]
 | 
			
		||||
            if last_log.sound and on_air == last_log.sound.path:
 | 
			
		||||
                return
 | 
			
		||||
 | 
			
		||||
        self.log(
 | 
			
		||||
            source = source.id,
 | 
			
		||||
            date = tz.make_aware(tz.datetime.now()),
 | 
			
		||||
            comment = 'sound has changed',
 | 
			
		||||
            related_object = models.Sound.objects.get(path = on_air),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def monitor (self):
 | 
			
		||||
        """
 | 
			
		||||
        Log changes in the streams, and call dealer.monitor.
 | 
			
		||||
        """
 | 
			
		||||
        if not self.connector.available and self.connector.open():
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        self.dealer.monitor()
 | 
			
		||||
        self.__change_log(self.dealer)
 | 
			
		||||
        for source in self.streams.values():
 | 
			
		||||
            self.__change_log(source)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Monitor:
 | 
			
		||||
    """
 | 
			
		||||
    Monitor multiple controllers.
 | 
			
		||||
    """
 | 
			
		||||
    controllers = None
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, connector = None):
 | 
			
		||||
        self.controllers = {
 | 
			
		||||
            controller.id : controller
 | 
			
		||||
            for controller in [
 | 
			
		||||
                Controller(station, connector)
 | 
			
		||||
                for station in models.Station.objects.filter(active = True)
 | 
			
		||||
            ]
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def update (self):
 | 
			
		||||
        for controller in self.controllers.values():
 | 
			
		||||
            controller.update_all()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										61
									
								
								aircox/liquidsoap/views.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								aircox/liquidsoap/views.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
from django.views.generic.base import View, TemplateResponseMixin
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
from django.shortcuts import render
 | 
			
		||||
from django.http import HttpResponse
 | 
			
		||||
 | 
			
		||||
import aircox.liquidsoap.settings as settings
 | 
			
		||||
import aircox.liquidsoap.utils as utils
 | 
			
		||||
import aircox.programs.models as models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
view_monitor = utils.Monitor(
 | 
			
		||||
    utils.Connector(address = settings.AIRCOX_LIQUIDSOAP_SOCKET)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
class Actions:
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def exec (cl, monitor, controller, source, action):
 | 
			
		||||
        controller = monitor.controllers.get(controller)
 | 
			
		||||
        source = controller and controller.get(source)
 | 
			
		||||
 | 
			
		||||
        if not controller or not source or \
 | 
			
		||||
                action.startswith('__') or \
 | 
			
		||||
                action not in cl.__dict__:
 | 
			
		||||
            return -1
 | 
			
		||||
 | 
			
		||||
        action = getattr(Actions, action)
 | 
			
		||||
        return action(monitor, controller, source)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def skip (cl, monitor, controller, source):
 | 
			
		||||
        source.skip()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LiquidControl (View):
 | 
			
		||||
    template_name = 'aircox_liquidsoap/controller.html'
 | 
			
		||||
 | 
			
		||||
    def get_context_data (self, **kwargs):
 | 
			
		||||
        view_monitor.update()
 | 
			
		||||
        return {
 | 
			
		||||
            'request': self.request,
 | 
			
		||||
            'monitor': view_monitor,
 | 
			
		||||
            'embed': 'embed' in self.request.GET,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def post (self, request = None, **kwargs):
 | 
			
		||||
        if 'action' in request.POST:
 | 
			
		||||
            POST = request.POST
 | 
			
		||||
            controller = POST.get('controller')
 | 
			
		||||
            source = POST.get('source')
 | 
			
		||||
            action = POST.get('action')
 | 
			
		||||
            Actions.exec(view_monitor, controller, source, action)
 | 
			
		||||
        return HttpResponse('')
 | 
			
		||||
 | 
			
		||||
    def get (self, request = None, **kwargs):
 | 
			
		||||
        self.request = request
 | 
			
		||||
        context = self.get_context_data(**kwargs)
 | 
			
		||||
        return render(request, self.template_name, context)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										32
									
								
								aircox/programs/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								aircox/programs/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
# Aircox Programs
 | 
			
		||||
 | 
			
		||||
This application defines all base models and basic control of them. We have:
 | 
			
		||||
* **Nameable**: generic class used in any class needing to be named. Includes some utility functions;
 | 
			
		||||
* **Program**: the program itself;
 | 
			
		||||
* **Diffusion**: occurrence of a program planified in the timetable. For rerun, informations are bound to the initial diffusion;
 | 
			
		||||
* **Schedule**: describes diffusions frequencies for each program;
 | 
			
		||||
* **Track**: track informations in a playlist of a diffusion;
 | 
			
		||||
* **Sound**: information about a sound that can be used for podcast or rerun;
 | 
			
		||||
* **Log**: logs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Architecture
 | 
			
		||||
A Station is basically an object that represent a radio station. On each station, we use the Program object, that is declined in two different type:
 | 
			
		||||
* **Scheduled**: the diffusion is based on a timetable and planified through one Schedule or more; Diffusion object represent the occurrence of these programs;
 | 
			
		||||
* **Streamed**: the diffusion is based on random playlist, used to fill gaps between the programs;
 | 
			
		||||
 | 
			
		||||
Each program has a directory in **AIRCOX_PROGRAMS_DIR**; For each, subdir:
 | 
			
		||||
* **archives**: complete episode record, can be used for diffusions or as a podcast
 | 
			
		||||
* **excerpts**: excerpt of an episode, or other elements, can be used as a podcast
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## manage.py's commands
 | 
			
		||||
* **diffusions_monitor**: update/create, check and clean diffusions; When a diffusion is created, its type is unconfirmed, and requires a manual approval to be on the timetable.
 | 
			
		||||
* **sound_monitor**: check for existing and missing sounds files in programs directories and synchronize the database. Can also check for the quality of file and synchronize the database according to them.
 | 
			
		||||
* **sound_quality_check**: check for the quality of the file (don't update database)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Requirements
 | 
			
		||||
* Sox (and soxi): sound file monitor and quality check
 | 
			
		||||
* requirements.txt for python's dependecies
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								aircox/programs/__init__.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										0
									
								
								aircox/programs/__init__.py
									
									
									
									
									
										Executable file
									
								
							
							
								
								
									
										123
									
								
								aircox/programs/admin.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										123
									
								
								aircox/programs/admin.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,123 @@
 | 
			
		||||
import copy
 | 
			
		||||
 | 
			
		||||
from django import forms
 | 
			
		||||
from django.contrib import admin
 | 
			
		||||
from django.db import models
 | 
			
		||||
 | 
			
		||||
from aircox.programs.models import *
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#
 | 
			
		||||
# Inlines
 | 
			
		||||
#
 | 
			
		||||
class SoundInline (admin.TabularInline):
 | 
			
		||||
    model = Sound
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ScheduleInline (admin.TabularInline):
 | 
			
		||||
    model = Schedule
 | 
			
		||||
    extra = 1
 | 
			
		||||
 | 
			
		||||
class StreamInline (admin.TabularInline):
 | 
			
		||||
    fields = ['delay', 'begin', 'end']
 | 
			
		||||
    model = Stream
 | 
			
		||||
    extra = 1
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# from suit.admin import SortableTabularInline, SortableModelAdmin
 | 
			
		||||
#class TrackInline (SortableTabularInline):
 | 
			
		||||
#    fields = ['artist', 'name', 'tags', 'position']
 | 
			
		||||
#    form = TrackForm
 | 
			
		||||
#    model = Track
 | 
			
		||||
#    sortable = 'position'
 | 
			
		||||
#    extra = 10
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class NameableAdmin (admin.ModelAdmin):
 | 
			
		||||
    fields = [ 'name' ]
 | 
			
		||||
 | 
			
		||||
    list_display = ['id', 'name']
 | 
			
		||||
    list_filter = []
 | 
			
		||||
    search_fields = ['name',]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Sound)
 | 
			
		||||
class SoundAdmin (NameableAdmin):
 | 
			
		||||
    fields = None
 | 
			
		||||
    list_display = ['id', 'name', 'duration', 'type', 'mtime', 'good_quality', 'removed', 'public']
 | 
			
		||||
    fieldsets = [
 | 
			
		||||
        (None, { 'fields': NameableAdmin.fields + ['path', 'type'] } ),
 | 
			
		||||
        (None, { 'fields': ['embed', 'duration', 'mtime'] }),
 | 
			
		||||
        (None, { 'fields': ['removed', 'good_quality', 'public' ] } )
 | 
			
		||||
    ]
 | 
			
		||||
    readonly_fields = ('path', 'duration',)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Stream)
 | 
			
		||||
class StreamAdmin (admin.ModelAdmin):
 | 
			
		||||
    list_display = ('id', 'program', 'delay', 'begin', 'end')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Station)
 | 
			
		||||
class StationAdmin (NameableAdmin):
 | 
			
		||||
    fields = NameableAdmin.fields + [ 'active', 'public', 'fallback' ]
 | 
			
		||||
 | 
			
		||||
@admin.register(Program)
 | 
			
		||||
class ProgramAdmin (NameableAdmin):
 | 
			
		||||
    fields = NameableAdmin.fields + [ 'station', 'active' ]
 | 
			
		||||
    # TODO list_display
 | 
			
		||||
    inlines = [ ScheduleInline, StreamInline ]
 | 
			
		||||
 | 
			
		||||
    # SO#8074161
 | 
			
		||||
    #def get_form (self, request, obj=None, **kwargs):
 | 
			
		||||
        #if obj:
 | 
			
		||||
        #    if Schedule.objects.filter(program = obj).count():
 | 
			
		||||
        #        self.inlines.remove(StreamInline)
 | 
			
		||||
        #    elif Stream.objects.filter(program = obj).count():
 | 
			
		||||
        #        self.inlines.remove(ScheduleInline)
 | 
			
		||||
        #return super().get_form(request, obj, **kwargs)
 | 
			
		||||
 | 
			
		||||
@admin.register(Diffusion)
 | 
			
		||||
class DiffusionAdmin (admin.ModelAdmin):
 | 
			
		||||
    def archives (self, obj):
 | 
			
		||||
        sounds = [ str(s) for s in obj.get_archives()]
 | 
			
		||||
        return ', '.join(sounds) if sounds else ''
 | 
			
		||||
 | 
			
		||||
    def conflicts (self, obj):
 | 
			
		||||
        if obj.type == Diffusion.Type['unconfirmed']:
 | 
			
		||||
            return ', '.join([ str(d) for d in obj.get_conflicts()])
 | 
			
		||||
        return ''
 | 
			
		||||
 | 
			
		||||
    list_display = ('id', 'type', 'date', 'program', 'initial', 'archives', 'conflicts')
 | 
			
		||||
    list_filter = ('type', 'date', 'program')
 | 
			
		||||
    list_editable = ('type', 'date')
 | 
			
		||||
 | 
			
		||||
    fields = ['type', 'date', 'initial', 'program', 'sounds']
 | 
			
		||||
 | 
			
		||||
    def get_form(self, request, obj=None, **kwargs):
 | 
			
		||||
        if request.user.has_perm('aircox_program.programming'):
 | 
			
		||||
            self.readonly_fields = []
 | 
			
		||||
        else:
 | 
			
		||||
            self.readonly_fields = ['program', 'date', 'duration']
 | 
			
		||||
 | 
			
		||||
        if obj.initial:
 | 
			
		||||
            self.readonly_fields += ['program', 'sounds']
 | 
			
		||||
        return super().get_form(request, obj, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get_queryset(self, request):
 | 
			
		||||
        qs = super(DiffusionAdmin, self).get_queryset(request)
 | 
			
		||||
        if '_changelist_filters' in request.GET or \
 | 
			
		||||
            'type__exact' in request.GET and \
 | 
			
		||||
                str(Diffusion.Type['unconfirmed']) in request.GET['type__exact']:
 | 
			
		||||
            return qs
 | 
			
		||||
        return qs.exclude(type = Diffusion.Type['unconfirmed'])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Log)
 | 
			
		||||
class LogAdmin (admin.ModelAdmin):
 | 
			
		||||
    list_display = ['id', 'date', 'source', 'comment', 'related_object']
 | 
			
		||||
    list_filter = ['date', 'related_type']
 | 
			
		||||
 | 
			
		||||
admin.site.register(Track)
 | 
			
		||||
admin.site.register(Schedule)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										0
									
								
								aircox/programs/management/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								aircox/programs/management/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										
											BIN
										
									
								
								aircox/programs/management/__pycache__/__init__.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								aircox/programs/management/__pycache__/__init__.cpython-34.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								aircox/programs/management/__pycache__/__init__.cpython-35.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								aircox/programs/management/__pycache__/__init__.cpython-35.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										0
									
								
								aircox/programs/management/commands/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								aircox/programs/management/commands/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								aircox/programs/management/commands/_private.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								aircox/programs/management/commands/_private.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										159
									
								
								aircox/programs/management/commands/diffusions_monitor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										159
									
								
								aircox/programs/management/commands/diffusions_monitor.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,159 @@
 | 
			
		||||
"""
 | 
			
		||||
Manage diffusions using schedules, to update, clean up or check diffusions.
 | 
			
		||||
 | 
			
		||||
A generated diffusion can be unconfirmed, that means that the user must confirm
 | 
			
		||||
it by changing its type to "normal". The behaviour is controlled using
 | 
			
		||||
--approval.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Different actions are available:
 | 
			
		||||
- "update" is the process that is used to generated them using programs
 | 
			
		||||
schedules for the (given) month.
 | 
			
		||||
 | 
			
		||||
- "clean" will remove all diffusions that are still unconfirmed and have been
 | 
			
		||||
planified before the (given) month.
 | 
			
		||||
 | 
			
		||||
- "check" will remove all diffusions that are unconfirmed and have been planified
 | 
			
		||||
from the (given) month and later.
 | 
			
		||||
"""
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
from aircox.programs.models import *
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Actions:
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def __check_conflicts (item, saved_items):
 | 
			
		||||
        """
 | 
			
		||||
        Check for conflicts, and update conflictual
 | 
			
		||||
        items if they have been generated during this
 | 
			
		||||
        update.
 | 
			
		||||
 | 
			
		||||
        Return the number of conflicts
 | 
			
		||||
        """
 | 
			
		||||
        conflicts = item.get_conflicts()
 | 
			
		||||
        if not conflicts:
 | 
			
		||||
            item.type = Diffusion.Type['normal']
 | 
			
		||||
            return 0
 | 
			
		||||
 | 
			
		||||
        item.type = Diffusion.Type['unconfirmed']
 | 
			
		||||
        for conflict in conflicts:
 | 
			
		||||
            if conflict.pk in saved_items and \
 | 
			
		||||
                    conflict.type != Diffusion.Type['unconfirmed']:
 | 
			
		||||
                conflict.type = Diffusion.Type['unconfirmed']
 | 
			
		||||
                conflict.save()
 | 
			
		||||
        return len(conflicts)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def update (cl, date, mode):
 | 
			
		||||
        manual = (mode == 'manual')
 | 
			
		||||
        if not manual:
 | 
			
		||||
            saved_items = set()
 | 
			
		||||
 | 
			
		||||
        count = [0, 0]
 | 
			
		||||
        for schedule in Schedule.objects.filter(program__active = True) \
 | 
			
		||||
                .order_by('initial'):
 | 
			
		||||
            # in order to allow rerun links between diffusions, we save items
 | 
			
		||||
            # by schedule;
 | 
			
		||||
            items = schedule.diffusions_of_month(date, exclude_saved = True)
 | 
			
		||||
            count[0] += len(items)
 | 
			
		||||
 | 
			
		||||
            if manual:
 | 
			
		||||
                Diffusion.objects.bulk_create(items)
 | 
			
		||||
            else:
 | 
			
		||||
                for item in items:
 | 
			
		||||
                    count[1] += cl.__check_conflicts(item, saved_items)
 | 
			
		||||
                    item.save()
 | 
			
		||||
                    saved_items.add(item)
 | 
			
		||||
 | 
			
		||||
            print('> {} new diffusions for schedule #{} ({})'.format(
 | 
			
		||||
                    len(items), schedule.id, str(schedule)
 | 
			
		||||
                 ))
 | 
			
		||||
 | 
			
		||||
        print('total of {} diffusions have been created,'.format(count[0]),
 | 
			
		||||
              'do not forget manual approval' if manual else
 | 
			
		||||
                '{} conflicts found'.format(count[1]))
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def clean (date):
 | 
			
		||||
        qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
 | 
			
		||||
                                      date__lt = date)
 | 
			
		||||
        print('{} diffusions will be removed'.format(qs.count()))
 | 
			
		||||
        qs.delete()
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def check (date):
 | 
			
		||||
        qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
 | 
			
		||||
                                      date__gt = date)
 | 
			
		||||
        items = []
 | 
			
		||||
        for diffusion in qs:
 | 
			
		||||
            schedules = Schedule.objects.filter(program = diffusion.program)
 | 
			
		||||
            for schedule in schedules:
 | 
			
		||||
                if schedule.match(diffusion.date):
 | 
			
		||||
                    break
 | 
			
		||||
            else:
 | 
			
		||||
                print('> #{}: {}'.format(diffusion.pk, str(diffusion)))
 | 
			
		||||
                items.append(diffusion.id)
 | 
			
		||||
 | 
			
		||||
        print('{} diffusions will be removed'.format(len(items)))
 | 
			
		||||
        if len(items):
 | 
			
		||||
            Diffusion.objects.filter(id__in = items).delete()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help= __doc__
 | 
			
		||||
 | 
			
		||||
    def add_arguments (self, parser):
 | 
			
		||||
        parser.formatter_class=RawTextHelpFormatter
 | 
			
		||||
        now = tz.datetime.today()
 | 
			
		||||
 | 
			
		||||
        group = parser.add_argument_group('action')
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--update', action='store_true',
 | 
			
		||||
            help='generate (unconfirmed) diffusions for the given month. '
 | 
			
		||||
                 'These diffusions must be confirmed manually by changing '
 | 
			
		||||
                 'their type to "normal"')
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--clean', action='store_true',
 | 
			
		||||
            help='remove unconfirmed diffusions older than the given month')
 | 
			
		||||
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--check', action='store_true',
 | 
			
		||||
            help='check future unconfirmed diffusions from the given date '
 | 
			
		||||
                 'agains\'t schedules and remove it if that do not match any '
 | 
			
		||||
                 'schedule')
 | 
			
		||||
 | 
			
		||||
        group = parser.add_argument_group('date')
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--year', type=int, default=now.year,
 | 
			
		||||
            help='used by update, default is today\'s year')
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--month', type=int, default=now.month,
 | 
			
		||||
            help='used by update, default is today\'s month')
 | 
			
		||||
 | 
			
		||||
        group = parser.add_argument_group('mode')
 | 
			
		||||
        group.add_argument(
 | 
			
		||||
            '--approval', type=str, choices=['manual', 'auto'],
 | 
			
		||||
            default='auto',
 | 
			
		||||
            help='manual means that all generated diffusions are unconfirmed, '
 | 
			
		||||
                 'thus must be approved manually; auto confirmes all '
 | 
			
		||||
                 'diffusions except those that conflicts with others'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        date = tz.datetime(year = options.get('year'),
 | 
			
		||||
                                 month = options.get('month'),
 | 
			
		||||
                                 day = 1)
 | 
			
		||||
        date = tz.make_aware(date)
 | 
			
		||||
 | 
			
		||||
        if options.get('update'):
 | 
			
		||||
            Actions.update(date, mode = options.get('mode'))
 | 
			
		||||
        elif options.get('clean'):
 | 
			
		||||
            Actions.clean(date)
 | 
			
		||||
        elif options.get('check'):
 | 
			
		||||
            Actions.check(date)
 | 
			
		||||
        else:
 | 
			
		||||
            raise CommandError('no action has been given')
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										228
									
								
								aircox/programs/management/commands/sounds_monitor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										228
									
								
								aircox/programs/management/commands/sounds_monitor.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,228 @@
 | 
			
		||||
"""
 | 
			
		||||
Monitor sound files; For each program, check for:
 | 
			
		||||
- new files;
 | 
			
		||||
- deleted files;
 | 
			
		||||
- differences between files and sound;
 | 
			
		||||
- quality of the files;
 | 
			
		||||
 | 
			
		||||
It tries to parse the file name to get the date of the diffusion of an
 | 
			
		||||
episode and associate the file with it; We use the following format:
 | 
			
		||||
    yyyymmdd[_n][_][name]
 | 
			
		||||
 | 
			
		||||
Where:
 | 
			
		||||
    'yyyy' the year of the episode's diffusion;
 | 
			
		||||
    'mm' the month of the episode's diffusion;
 | 
			
		||||
    'dd' the day of the episode's diffusion;
 | 
			
		||||
    'n' the number of the episode (if multiple episodes);
 | 
			
		||||
    'name' the title of the sound;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
To check quality of files, call the command sound_quality_check using the
 | 
			
		||||
parameters given by the setting AIRCOX_SOUND_QUALITY. This script requires
 | 
			
		||||
Sox (and soxi).
 | 
			
		||||
"""
 | 
			
		||||
import os
 | 
			
		||||
import re
 | 
			
		||||
import subprocess
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
 | 
			
		||||
from aircox.programs.models import *
 | 
			
		||||
import aircox.programs.settings as settings
 | 
			
		||||
import aircox.programs.utils as utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help= __doc__
 | 
			
		||||
 | 
			
		||||
    def report (self, program = None, component = None, *content):
 | 
			
		||||
        if not component:
 | 
			
		||||
            print('{}: '.format(program), *content)
 | 
			
		||||
        else:
 | 
			
		||||
            print('{}, {}: '.format(program, component), *content)
 | 
			
		||||
 | 
			
		||||
    def add_arguments (self, parser):
 | 
			
		||||
        parser.formatter_class=RawTextHelpFormatter
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-q', '--quality_check', action='store_true',
 | 
			
		||||
            help='Enable quality check using sound_quality_check on all ' \
 | 
			
		||||
                 'sounds marqued as not good'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-s', '--scan', action='store_true',
 | 
			
		||||
            help='Scan programs directories for changes, plus check for a '
 | 
			
		||||
                 ' matching episode on sounds that have not been yet assigned'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        if options.get('scan'):
 | 
			
		||||
            self.scan()
 | 
			
		||||
        if options.get('quality_check'):
 | 
			
		||||
            self.check_quality(check = (not options.get('scan')) )
 | 
			
		||||
 | 
			
		||||
    def _get_duration (self, path):
 | 
			
		||||
        p = subprocess.Popen(['soxi', '-D', path], stdout=subprocess.PIPE,
 | 
			
		||||
                             stderr=subprocess.PIPE)
 | 
			
		||||
        out, err = p.communicate()
 | 
			
		||||
        if not err:
 | 
			
		||||
            return utils.seconds_to_time(int(float(out)))
 | 
			
		||||
 | 
			
		||||
    def get_sound_info (self, program, path):
 | 
			
		||||
        """
 | 
			
		||||
        Parse file name to get info on the assumption it has the correct
 | 
			
		||||
        format (given in Command.help)
 | 
			
		||||
        """
 | 
			
		||||
        file_name = os.path.basename(path)
 | 
			
		||||
        file_name = os.path.splitext(file_name)[0]
 | 
			
		||||
        r = re.search('^(?P<year>[0-9]{4})'
 | 
			
		||||
                      '(?P<month>[0-9]{2})'
 | 
			
		||||
                      '(?P<day>[0-9]{2})'
 | 
			
		||||
                      '(_(?P<n>[0-9]+))?'
 | 
			
		||||
                      '_?(?P<name>.*)$',
 | 
			
		||||
                      file_name)
 | 
			
		||||
 | 
			
		||||
        if not (r and r.groupdict()):
 | 
			
		||||
            self.report(program, path, "file path is not correct, use defaults")
 | 
			
		||||
            r = {
 | 
			
		||||
                'name': file_name
 | 
			
		||||
            }
 | 
			
		||||
        else:
 | 
			
		||||
            r = r.groupdict()
 | 
			
		||||
 | 
			
		||||
        r['duration'] = self._get_duration(path)
 | 
			
		||||
        r['name'] = r['name'].replace('_', ' ').capitalize()
 | 
			
		||||
        r['path'] = path
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
    def find_initial (self, program, sound_info):
 | 
			
		||||
        """
 | 
			
		||||
        For a given program, and sound path check if there is an initial
 | 
			
		||||
        diffusion to associate to, using the diffusion's date.
 | 
			
		||||
 | 
			
		||||
        If there is no matching episode, return None.
 | 
			
		||||
        """
 | 
			
		||||
        # check on episodes
 | 
			
		||||
        diffusion = Diffusion.objects.filter(
 | 
			
		||||
            program = program,
 | 
			
		||||
            date__year = int(sound_info['year']),
 | 
			
		||||
            date__month = int(sound_info['month']),
 | 
			
		||||
            date__day = int(sound_info['day'])
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        if not diffusion.count():
 | 
			
		||||
            self.report(program, sound_info['path'],
 | 
			
		||||
                        'no diffusion found for the given date')
 | 
			
		||||
            return
 | 
			
		||||
        return diffusion[0]
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def check_sounds (qs):
 | 
			
		||||
        """
 | 
			
		||||
        Only check for the sound existence or update
 | 
			
		||||
        """
 | 
			
		||||
        # check files
 | 
			
		||||
        for sound in qs:
 | 
			
		||||
            if sound.check_on_file():
 | 
			
		||||
                sound.save(check = False)
 | 
			
		||||
 | 
			
		||||
    def scan (self):
 | 
			
		||||
        """
 | 
			
		||||
        For all programs, scan dirs
 | 
			
		||||
        """
 | 
			
		||||
        print('scan files for all programs...')
 | 
			
		||||
        programs = Program.objects.filter()
 | 
			
		||||
 | 
			
		||||
        for program in programs:
 | 
			
		||||
            print('- program', program.name)
 | 
			
		||||
            self.scan_for_program(
 | 
			
		||||
                program, settings.AIRCOX_SOUND_ARCHIVES_SUBDIR,
 | 
			
		||||
                type = Sound.Type['archive'],
 | 
			
		||||
            )
 | 
			
		||||
            self.scan_for_program(
 | 
			
		||||
                program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
 | 
			
		||||
                type = Sound.Type['excerpt'],
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    def scan_for_program (self, program, subdir, **sound_kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Scan a given directory that is associated to the given program, and
 | 
			
		||||
        update sounds information.
 | 
			
		||||
        """
 | 
			
		||||
        print(' - scan files in', subdir)
 | 
			
		||||
        if not program.ensure_dir(subdir):
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        subdir = os.path.join(program.path, subdir)
 | 
			
		||||
 | 
			
		||||
        # new/existing sounds
 | 
			
		||||
        for path in os.listdir(subdir):
 | 
			
		||||
            path = os.path.join(subdir, path)
 | 
			
		||||
            if not path.endswith(settings.AIRCOX_SOUND_FILE_EXT):
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            sound_info = self.get_sound_info(program, path)
 | 
			
		||||
            sound = Sound.objects.get_or_create(
 | 
			
		||||
                path = path,
 | 
			
		||||
                defaults = { 'name': sound_info['name'],
 | 
			
		||||
                             'duration': sound_info['duration'] or None }
 | 
			
		||||
            )[0]
 | 
			
		||||
            sound.__dict__.update(sound_kwargs)
 | 
			
		||||
            sound.save(check = False)
 | 
			
		||||
 | 
			
		||||
            # initial diffusion association
 | 
			
		||||
            if 'year' in sound_info:
 | 
			
		||||
                initial = self.find_initial(program, sound_info)
 | 
			
		||||
                if initial:
 | 
			
		||||
                    if initial.initial:
 | 
			
		||||
                        # FIXME: allow user to overwrite rerun info?
 | 
			
		||||
                        self.report(program, path,
 | 
			
		||||
                                    'the diffusion must be an initial diffusion')
 | 
			
		||||
                    else:
 | 
			
		||||
                        sound = initial.sounds.get_queryset() \
 | 
			
		||||
                                    .filter(path == sound.path)
 | 
			
		||||
                        if not sound:
 | 
			
		||||
                            self.report(program, path,
 | 
			
		||||
                                        'add sound to diffusion ', initial.id)
 | 
			
		||||
                            initial.sounds.add(sound)
 | 
			
		||||
                            initial.save()
 | 
			
		||||
 | 
			
		||||
        self.check_sounds(Sound.objects.filter(path__startswith = subdir))
 | 
			
		||||
 | 
			
		||||
    def check_quality (self, check = False):
 | 
			
		||||
        """
 | 
			
		||||
        Check all files where quality has been set to bad
 | 
			
		||||
        """
 | 
			
		||||
        import aircox.programs.management.commands.sounds_quality_check \
 | 
			
		||||
                as quality_check
 | 
			
		||||
 | 
			
		||||
        sounds = Sound.objects.filter(good_quality = False)
 | 
			
		||||
        if check:
 | 
			
		||||
            self.check_sounds(sounds)
 | 
			
		||||
            files = [ sound.path for sound in sounds if not sound.removed ]
 | 
			
		||||
        else:
 | 
			
		||||
            files = [ sound.path for sound in sounds.filter(removed = False) ]
 | 
			
		||||
 | 
			
		||||
        print('start quality check...')
 | 
			
		||||
        cmd = quality_check.Command()
 | 
			
		||||
        cmd.handle( files = files,
 | 
			
		||||
                    **settings.AIRCOX_SOUND_QUALITY )
 | 
			
		||||
 | 
			
		||||
        print('- update sounds in database')
 | 
			
		||||
        def update_stats(sound_info, sound):
 | 
			
		||||
            stats = sound_info.get_file_stats()
 | 
			
		||||
            if stats:
 | 
			
		||||
                duration = int(stats.get('length'))
 | 
			
		||||
                sound.duration = utils.seconds_to_time(duration)
 | 
			
		||||
 | 
			
		||||
        for sound_info in cmd.good:
 | 
			
		||||
            sound = Sound.objects.get(path = sound_info.path)
 | 
			
		||||
            sound.good_quality = True
 | 
			
		||||
            update_stats(sound_info, sound)
 | 
			
		||||
            sound.save(check = False)
 | 
			
		||||
 | 
			
		||||
        for sound_info in cmd.bad:
 | 
			
		||||
            sound = Sound.objects.get(path = sound_info.path)
 | 
			
		||||
            update_stats(sound_info, sound)
 | 
			
		||||
            sound.save(check = False)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										175
									
								
								aircox/programs/management/commands/sounds_quality_check.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								aircox/programs/management/commands/sounds_quality_check.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,175 @@
 | 
			
		||||
"""
 | 
			
		||||
Analyse and check files using Sox, prints good and bad files.
 | 
			
		||||
"""
 | 
			
		||||
import sys
 | 
			
		||||
import re
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
 | 
			
		||||
class Stats:
 | 
			
		||||
    attributes = [
 | 
			
		||||
        'DC offset', 'Min level', 'Max level',
 | 
			
		||||
        'Pk lev dB', 'RMS lev dB', 'RMS Pk dB',
 | 
			
		||||
        'RMS Tr dB', 'Flat factor', 'Length s',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, path, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        If path is given, call analyse with path and kwargs
 | 
			
		||||
        """
 | 
			
		||||
        self.values = {}
 | 
			
		||||
        if path:
 | 
			
		||||
            self.analyse(path, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get (self, attr):
 | 
			
		||||
        return self.values.get(attr)
 | 
			
		||||
 | 
			
		||||
    def parse (self, output):
 | 
			
		||||
        for attr in Stats.attributes:
 | 
			
		||||
            value = re.search(attr + r'\s+(?P<value>\S+)', output)
 | 
			
		||||
            value = value and value.groupdict()
 | 
			
		||||
            if value:
 | 
			
		||||
                try:
 | 
			
		||||
                    value = float(value.get('value'))
 | 
			
		||||
                except ValueError:
 | 
			
		||||
                    value = None
 | 
			
		||||
                self.values[attr] = value
 | 
			
		||||
        self.values['length'] = self.values['Length s']
 | 
			
		||||
 | 
			
		||||
    def analyse (self, path, at = None, length = None):
 | 
			
		||||
        """
 | 
			
		||||
        If at and length are given use them as excerpt to analyse.
 | 
			
		||||
        """
 | 
			
		||||
        args = ['sox', path, '-n']
 | 
			
		||||
 | 
			
		||||
        if at is not None and length is not None:
 | 
			
		||||
            args += ['trim', str(at), str(length) ]
 | 
			
		||||
 | 
			
		||||
        args.append('stats')
 | 
			
		||||
 | 
			
		||||
        p = subprocess.Popen(args, stdout=subprocess.PIPE,
 | 
			
		||||
                             stderr=subprocess.PIPE)
 | 
			
		||||
        # sox outputs to stderr (my god WHYYYY)
 | 
			
		||||
        out_, out = p.communicate()
 | 
			
		||||
        self.parse(str(out, encoding='utf-8'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Sound:
 | 
			
		||||
    path = None             # file path
 | 
			
		||||
    sample_length = 120     # default sample length in seconds
 | 
			
		||||
    stats = None            # list of samples statistics
 | 
			
		||||
    bad = None              # list of bad samples
 | 
			
		||||
    good = None             # list of good samples
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, path, sample_length = None):
 | 
			
		||||
        self.path = path
 | 
			
		||||
        self.sample_length = sample_length if sample_length is not None \
 | 
			
		||||
                                else self.sample_length
 | 
			
		||||
 | 
			
		||||
    def get_file_stats (self):
 | 
			
		||||
        return self.stats and self.stats[0]
 | 
			
		||||
 | 
			
		||||
    def analyse (self):
 | 
			
		||||
        print('- complete file analysis')
 | 
			
		||||
        self.stats = [ Stats(self.path) ]
 | 
			
		||||
        position = 0
 | 
			
		||||
        length = self.stats[0].get('length')
 | 
			
		||||
 | 
			
		||||
        if not self.sample_length:
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        print('- samples analysis: ', end=' ')
 | 
			
		||||
        while position < length:
 | 
			
		||||
            print(len(self.stats), end=' ')
 | 
			
		||||
            stats = Stats(self.path, at = position, length = self.sample_length)
 | 
			
		||||
            self.stats.append(stats)
 | 
			
		||||
            position += self.sample_length
 | 
			
		||||
        print()
 | 
			
		||||
 | 
			
		||||
    def check (self, name, min_val, max_val):
 | 
			
		||||
        self.good = [ index for index, stats in enumerate(self.stats)
 | 
			
		||||
                      if min_val <= stats.get(name) <= max_val ]
 | 
			
		||||
        self.bad = [ index for index, stats in enumerate(self.stats)
 | 
			
		||||
                      if index not in self.good ]
 | 
			
		||||
        self.resume()
 | 
			
		||||
 | 
			
		||||
    def resume (self):
 | 
			
		||||
        view = lambda array: [
 | 
			
		||||
            'file' if index is 0 else
 | 
			
		||||
            'sample {} (at {} seconds)'.format(index, (index-1) * self.sample_length)
 | 
			
		||||
            for index in array
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
        if self.good:
 | 
			
		||||
            print('- good:\033[92m', ', '.join( view(self.good) ), '\033[0m')
 | 
			
		||||
        if self.bad:
 | 
			
		||||
            print('- bad:\033[91m', ', '.join( view(self.bad) ), '\033[0m')
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help = __doc__
 | 
			
		||||
    sounds = None
 | 
			
		||||
 | 
			
		||||
    def add_arguments (self, parser):
 | 
			
		||||
        parser.formatter_class=RawTextHelpFormatter
 | 
			
		||||
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            'files', metavar='FILE', type=str, nargs='+',
 | 
			
		||||
            help='file(s) to analyse'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-s', '--sample_length', type=int, default=120,
 | 
			
		||||
            help='size of sample to analyse in seconds. If not set (or 0), does'
 | 
			
		||||
                 ' not analyse by sample',
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-a', '--attribute', type=str,
 | 
			
		||||
            help='attribute name to use to check, that can be:\n' + \
 | 
			
		||||
                 ', '.join([ '"{}"'.format(attr) for attr in Stats.attributes ])
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-r', '--range', type=float, nargs=2,
 | 
			
		||||
            help='range of minimal and maximal accepted value such as: ' \
 | 
			
		||||
                 '--range min max'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-i', '--resume', action='store_true',
 | 
			
		||||
            help='print a resume of good and bad files'
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        # parameters
 | 
			
		||||
        minmax = options.get('range')
 | 
			
		||||
        if not minmax:
 | 
			
		||||
            raise CommandError('no range specified')
 | 
			
		||||
 | 
			
		||||
        attr = options.get('attribute')
 | 
			
		||||
        if not attr:
 | 
			
		||||
            raise CommandError('no attribute specified')
 | 
			
		||||
 | 
			
		||||
        # sound analyse and checks
 | 
			
		||||
        self.sounds = [ Sound(path, options.get('sample_length'))
 | 
			
		||||
                        for path in options.get('files') ]
 | 
			
		||||
        self.bad = []
 | 
			
		||||
        self.good = []
 | 
			
		||||
        for sound in self.sounds:
 | 
			
		||||
            print(sound.path)
 | 
			
		||||
            sound.analyse()
 | 
			
		||||
            sound.check(attr, minmax[0], minmax[1])
 | 
			
		||||
            print()
 | 
			
		||||
            if sound.bad:
 | 
			
		||||
                self.bad.append(sound)
 | 
			
		||||
            else:
 | 
			
		||||
                self.good.append(sound)
 | 
			
		||||
 | 
			
		||||
        # resume
 | 
			
		||||
        if options.get('resume'):
 | 
			
		||||
            if self.good:
 | 
			
		||||
                print('files that did not failed the test:\033[92m\n   ',
 | 
			
		||||
                      '\n    '.join([sound.path for sound in self.good]), '\033[0m')
 | 
			
		||||
            if self.bad:
 | 
			
		||||
                # bad at the end for ergonomy
 | 
			
		||||
                print('files that failed the test:\033[91m\n   ',
 | 
			
		||||
                      '\n    '.join([sound.path for sound in self.bad]),'\033[0m')
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										680
									
								
								aircox/programs/models.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										680
									
								
								aircox/programs/models.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,680 @@
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from django.db import models
 | 
			
		||||
from django.template.defaultfilters import slugify
 | 
			
		||||
from django.utils.translation import ugettext as _, ugettext_lazy
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
from django.utils.html import strip_tags
 | 
			
		||||
from django.contrib.contenttypes.fields import GenericForeignKey
 | 
			
		||||
from django.contrib.contenttypes.models import ContentType
 | 
			
		||||
 | 
			
		||||
from taggit.managers import TaggableManager
 | 
			
		||||
 | 
			
		||||
import aircox.programs.utils as utils
 | 
			
		||||
import aircox.programs.settings as settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def date_or_default (date, date_only = False):
 | 
			
		||||
    """
 | 
			
		||||
    Return date or default value (now) if not defined, and remove time info
 | 
			
		||||
    if date_only is True
 | 
			
		||||
    """
 | 
			
		||||
    date = date or tz.datetime.today()
 | 
			
		||||
    if not tz.is_aware(date):
 | 
			
		||||
        date = tz.make_aware(date)
 | 
			
		||||
    if date_only:
 | 
			
		||||
        return date.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
 | 
			
		||||
    return date
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Nameable (models.Model):
 | 
			
		||||
    name = models.CharField (
 | 
			
		||||
        _('name'),
 | 
			
		||||
        max_length = 128,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def slug (self):
 | 
			
		||||
        """
 | 
			
		||||
        Slug based on the name. We replace '-' by '_'
 | 
			
		||||
        """
 | 
			
		||||
        return slugify(self.name).replace('-', '_')
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        #if self.pk:
 | 
			
		||||
        #    return '#{} {}'.format(self.pk, self.name)
 | 
			
		||||
        return '{}'.format(self.name)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        abstract = True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Track (Nameable):
 | 
			
		||||
    """
 | 
			
		||||
    Track of a playlist of a diffusion. The position can either be expressed
 | 
			
		||||
    as the position in the playlist or as the moment in seconds it started.
 | 
			
		||||
    """
 | 
			
		||||
    # There are no nice solution for M2M relations ship (even without
 | 
			
		||||
    # through) in django-admin. So we unfortunately need to make one-
 | 
			
		||||
    # to-one relations and add a position argument
 | 
			
		||||
    diffusion = models.ForeignKey(
 | 
			
		||||
        'Diffusion',
 | 
			
		||||
    )
 | 
			
		||||
    artist = models.CharField(
 | 
			
		||||
        _('artist'),
 | 
			
		||||
        max_length = 128,
 | 
			
		||||
    )
 | 
			
		||||
    # position can be used to specify a position in seconds for non-
 | 
			
		||||
    # stop programs or a position in the playlist
 | 
			
		||||
    position = models.SmallIntegerField(
 | 
			
		||||
        default = 0,
 | 
			
		||||
        help_text=_('position in the playlist'),
 | 
			
		||||
    )
 | 
			
		||||
    tags = TaggableManager(
 | 
			
		||||
        verbose_name=_('tags'),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return ' '.join([self.artist, ':', self.name ])
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Track')
 | 
			
		||||
        verbose_name_plural = _('Tracks')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Sound (Nameable):
 | 
			
		||||
    """
 | 
			
		||||
    A Sound is the representation of a sound file that can be either an excerpt
 | 
			
		||||
    or a complete archive of the related diffusion.
 | 
			
		||||
 | 
			
		||||
    The podcasting and public access permissions of a Sound are managed through
 | 
			
		||||
    the related program info.
 | 
			
		||||
    """
 | 
			
		||||
    Type = {
 | 
			
		||||
        'other': 0x00,
 | 
			
		||||
        'archive': 0x01,
 | 
			
		||||
        'excerpt': 0x02,
 | 
			
		||||
    }
 | 
			
		||||
    for key, value in Type.items():
 | 
			
		||||
        ugettext_lazy(key)
 | 
			
		||||
 | 
			
		||||
    type = models.SmallIntegerField(
 | 
			
		||||
        verbose_name = _('type'),
 | 
			
		||||
        choices = [ (y, x) for x,y in Type.items() ],
 | 
			
		||||
        blank = True, null = True
 | 
			
		||||
    )
 | 
			
		||||
    path = models.FilePathField(
 | 
			
		||||
        _('file'),
 | 
			
		||||
        path = settings.AIRCOX_PROGRAMS_DIR,
 | 
			
		||||
        match = r'(' + '|'.join(settings.AIRCOX_SOUND_FILE_EXT) \
 | 
			
		||||
                                    .replace('.', r'\.') + ')$',
 | 
			
		||||
        recursive = True,
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    embed = models.TextField(
 | 
			
		||||
        _('embed HTML code'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('HTML code used to embed a sound from external plateform'),
 | 
			
		||||
    )
 | 
			
		||||
    duration = models.TimeField(
 | 
			
		||||
        _('duration'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('duration of the sound'),
 | 
			
		||||
    )
 | 
			
		||||
    mtime = models.DateTimeField(
 | 
			
		||||
        _('modification time'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('last modification date and time'),
 | 
			
		||||
    )
 | 
			
		||||
    removed = models.BooleanField(
 | 
			
		||||
        _('removed'),
 | 
			
		||||
        default = False,
 | 
			
		||||
        help_text = _('this sound has been removed from filesystem'),
 | 
			
		||||
    )
 | 
			
		||||
    good_quality = models.BooleanField(
 | 
			
		||||
        _('good quality'),
 | 
			
		||||
        default = False,
 | 
			
		||||
        help_text = _('sound\'s quality is okay')
 | 
			
		||||
    )
 | 
			
		||||
    public = models.BooleanField(
 | 
			
		||||
        _('public'),
 | 
			
		||||
        default = False,
 | 
			
		||||
        help_text = _('sound\'s is accessible through the website')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def get_mtime (self):
 | 
			
		||||
        """
 | 
			
		||||
        Get the last modification date from file
 | 
			
		||||
        """
 | 
			
		||||
        mtime = os.stat(self.path).st_mtime
 | 
			
		||||
        mtime = tz.datetime.fromtimestamp(mtime)
 | 
			
		||||
        # db does not store microseconds
 | 
			
		||||
        mtime = mtime.replace(microsecond = 0)
 | 
			
		||||
        return tz.make_aware(mtime, tz.get_current_timezone())
 | 
			
		||||
 | 
			
		||||
    def file_exists (self):
 | 
			
		||||
        """
 | 
			
		||||
        Return true if the file still exists
 | 
			
		||||
        """
 | 
			
		||||
        return os.path.exists(self.path)
 | 
			
		||||
 | 
			
		||||
    def check_on_file (self):
 | 
			
		||||
        """
 | 
			
		||||
        Check sound file info again'st self, and update informations if
 | 
			
		||||
        needed (do not save). Return True if there was changes.
 | 
			
		||||
        """
 | 
			
		||||
        if not self.file_exists():
 | 
			
		||||
            if self.removed:
 | 
			
		||||
                return
 | 
			
		||||
            self.removed = True
 | 
			
		||||
            return True
 | 
			
		||||
 | 
			
		||||
        old_removed = self.removed
 | 
			
		||||
        self.removed = False
 | 
			
		||||
 | 
			
		||||
        mtime = self.get_mtime()
 | 
			
		||||
        if self.mtime != mtime:
 | 
			
		||||
            self.mtime = mtime
 | 
			
		||||
            self.good_quality = False
 | 
			
		||||
            return True
 | 
			
		||||
        return old_removed != self.removed
 | 
			
		||||
 | 
			
		||||
    def save (self, check = True, *args, **kwargs):
 | 
			
		||||
        if check:
 | 
			
		||||
            self.check_on_file()
 | 
			
		||||
 | 
			
		||||
        if not self.name and self.path:
 | 
			
		||||
            self.name = os.path.basename(self.path) \
 | 
			
		||||
                            .splitext() \
 | 
			
		||||
                            .replace('_', ' ')
 | 
			
		||||
        super().save(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        return '/'.join(self.path.split('/')[-3:])
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Sound')
 | 
			
		||||
        verbose_name_plural = _('Sounds')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Stream (models.Model):
 | 
			
		||||
    """
 | 
			
		||||
    When there are no program scheduled, it is possible to play sounds
 | 
			
		||||
    in order to avoid blanks. A Stream is a Program that plays this role,
 | 
			
		||||
    and whose linked to a Stream.
 | 
			
		||||
 | 
			
		||||
    All sounds that are marked as good and that are under the related
 | 
			
		||||
    program's archive dir are elligible for the sound's selection.
 | 
			
		||||
    """
 | 
			
		||||
    program = models.ForeignKey(
 | 
			
		||||
        'Program',
 | 
			
		||||
        verbose_name = _('related program'),
 | 
			
		||||
    )
 | 
			
		||||
    delay = models.TimeField(
 | 
			
		||||
        _('delay'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('plays this playlist at least every delay')
 | 
			
		||||
    )
 | 
			
		||||
    begin = models.TimeField(
 | 
			
		||||
        _('begin'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('used to define a time range this stream is'
 | 
			
		||||
                      'played')
 | 
			
		||||
    )
 | 
			
		||||
    end = models.TimeField(
 | 
			
		||||
        _('end'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('used to define a time range this stream is'
 | 
			
		||||
                      'played')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Schedule (models.Model):
 | 
			
		||||
    """
 | 
			
		||||
    A Schedule defines time slots of programs' diffusions. It can be an initial
 | 
			
		||||
    run or a rerun (in such case it is linked to the related schedule).
 | 
			
		||||
    """
 | 
			
		||||
    # Frequency for schedules. Basically, it is a mask of bits where each bit is
 | 
			
		||||
    # a week. Bits > rank 5 are used for special schedules.
 | 
			
		||||
    # Important: the first week is always the first week where the weekday of
 | 
			
		||||
    # the schedule is present.
 | 
			
		||||
    # For ponctual programs, there is no need for a schedule, only a diffusion
 | 
			
		||||
    Frequency = {
 | 
			
		||||
        'first':            (0b000001, _('first week of the month')),
 | 
			
		||||
        'second':           (0b000010, _('second week of the month')),
 | 
			
		||||
        'third':            (0b000100, _('third week of the month')),
 | 
			
		||||
        'fourth':           (0b001000, _('fourth week of the month')),
 | 
			
		||||
        'last':             (0b010000, _('last week of the month')),
 | 
			
		||||
        'first and third':  (0b000101, _('first and third weeks of the month')),
 | 
			
		||||
        'second and fourth': (0b001010, _('second and fourth weeks of the month')),
 | 
			
		||||
        'every':            (0b011111, _('once a week')),
 | 
			
		||||
        'one on two':       (0b100000, _('one week on two')),
 | 
			
		||||
    }
 | 
			
		||||
    VerboseFrequency = { value[0]: value[1] for key, value in Frequency.items() }
 | 
			
		||||
    Frequency = { key: value[0] for key, value in Frequency.items() }
 | 
			
		||||
 | 
			
		||||
    program = models.ForeignKey(
 | 
			
		||||
        'Program',
 | 
			
		||||
        verbose_name = _('related program'),
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField(_('date'))
 | 
			
		||||
    duration = models.TimeField(
 | 
			
		||||
        _('duration'),
 | 
			
		||||
        help_text = _('regular duration'),
 | 
			
		||||
    )
 | 
			
		||||
    frequency = models.SmallIntegerField(
 | 
			
		||||
        _('frequency'),
 | 
			
		||||
        choices = VerboseFrequency.items(),
 | 
			
		||||
    )
 | 
			
		||||
    initial = models.ForeignKey(
 | 
			
		||||
        'self',
 | 
			
		||||
        verbose_name = _('initial'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = 'this schedule is a rerun of this one',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def match (self, date = None, check_time = True):
 | 
			
		||||
        """
 | 
			
		||||
        Return True if the given datetime matches the schedule
 | 
			
		||||
        """
 | 
			
		||||
        date = date_or_default(date)
 | 
			
		||||
 | 
			
		||||
        if self.date.weekday() == date.weekday() and self.match_week(date):
 | 
			
		||||
            return self.date.time() == date.time() if check_time else True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def match_week (self, date = None):
 | 
			
		||||
        """
 | 
			
		||||
        Return True if the given week number matches the schedule, False
 | 
			
		||||
        otherwise.
 | 
			
		||||
        If the schedule is ponctual, return None.
 | 
			
		||||
        """
 | 
			
		||||
        # FIXME: does not work if first_day > date_day
 | 
			
		||||
        date = date_or_default(date)
 | 
			
		||||
        if self.frequency == Schedule.Frequency['one on two']:
 | 
			
		||||
            week = date.isocalendar()[1]
 | 
			
		||||
            return (week % 2) == (self.date.isocalendar()[1] % 2)
 | 
			
		||||
 | 
			
		||||
        first_of_month = date.replace(day = 1)
 | 
			
		||||
        week = date.isocalendar()[1] - first_of_month.isocalendar()[1]
 | 
			
		||||
 | 
			
		||||
        # weeks of month
 | 
			
		||||
        if week == 4:
 | 
			
		||||
            # fifth week: return if for every week
 | 
			
		||||
            return self.frequency == 0b1111
 | 
			
		||||
        return (self.frequency & (0b0001 << week) > 0)
 | 
			
		||||
 | 
			
		||||
    def normalize (self, date):
 | 
			
		||||
        """
 | 
			
		||||
        Set the time of a datetime to the schedule's one
 | 
			
		||||
        """
 | 
			
		||||
        return date.replace(hour = self.date.hour, minute = self.date.minute)
 | 
			
		||||
 | 
			
		||||
    def dates_of_month (self, date = None):
 | 
			
		||||
        """
 | 
			
		||||
        Return a list with all matching dates of date.month (=today)
 | 
			
		||||
        """
 | 
			
		||||
        date = date_or_default(date, True).replace(day=1)
 | 
			
		||||
        fwday = date.weekday()
 | 
			
		||||
        wday = self.date.weekday()
 | 
			
		||||
 | 
			
		||||
        # move date to the date weekday of the schedule
 | 
			
		||||
        # check on SO#3284452 for the formula
 | 
			
		||||
        date += tz.timedelta(days = (7 if fwday > wday else 0) - fwday + wday)
 | 
			
		||||
        fwday = date.weekday()
 | 
			
		||||
 | 
			
		||||
        # special frequency case
 | 
			
		||||
        weeks = self.frequency
 | 
			
		||||
        if self.frequency == Schedule.Frequency['last']:
 | 
			
		||||
            date += tz.timedelta(month = 1, days = -7)
 | 
			
		||||
            return self.normalize([date])
 | 
			
		||||
        if weeks == Schedule.Frequency['one on two']:
 | 
			
		||||
            # if both week are the same, then the date week of the month
 | 
			
		||||
            # matches. Note: wday % 2 + fwday % 2 => (wday + fwday) % 2
 | 
			
		||||
            fweek = date.isocalendar()[1]
 | 
			
		||||
 | 
			
		||||
            if date.month == 1 and fweek >= 50:
 | 
			
		||||
                # isocalendar can think we are on the last week of the
 | 
			
		||||
                # previous year
 | 
			
		||||
                fweek = 0
 | 
			
		||||
            week = self.date.isocalendar()[1]
 | 
			
		||||
            weeks = 0b010101 if not (fweek + week) % 2 else 0b001010
 | 
			
		||||
 | 
			
		||||
        dates = []
 | 
			
		||||
        for week in range(0,5):
 | 
			
		||||
            # there can be five weeks in a month
 | 
			
		||||
            if not weeks & (0b1 << week):
 | 
			
		||||
                continue
 | 
			
		||||
            wdate = date + tz.timedelta(days = week * 7)
 | 
			
		||||
            if wdate.month == date.month:
 | 
			
		||||
                dates.append(self.normalize(wdate))
 | 
			
		||||
        return dates
 | 
			
		||||
 | 
			
		||||
    def diffusions_of_month (self, date, exclude_saved = False):
 | 
			
		||||
        """
 | 
			
		||||
        Return a list of Diffusion instances, from month of the given date, that
 | 
			
		||||
        can be not in the database.
 | 
			
		||||
 | 
			
		||||
        If exclude_saved, exclude all diffusions that are yet in the database.
 | 
			
		||||
        """
 | 
			
		||||
        dates = self.dates_of_month(date)
 | 
			
		||||
        saved = Diffusion.objects.filter(date__in = dates,
 | 
			
		||||
                                         program = self.program)
 | 
			
		||||
        diffusions = []
 | 
			
		||||
 | 
			
		||||
        # existing diffusions
 | 
			
		||||
        for item in saved:
 | 
			
		||||
            if item.date in dates:
 | 
			
		||||
                dates.remove(item.date)
 | 
			
		||||
            if not exclude_saved:
 | 
			
		||||
                diffusions.append(item)
 | 
			
		||||
 | 
			
		||||
        # others
 | 
			
		||||
        for date in dates:
 | 
			
		||||
            first_date = date
 | 
			
		||||
            if self.initial:
 | 
			
		||||
                first_date -= self.date - self.initial.date
 | 
			
		||||
 | 
			
		||||
            first_diffusion = Diffusion.objects.filter(date = first_date,
 | 
			
		||||
                                                       program = self.program)
 | 
			
		||||
            first_diffusion = first_diffusion[0] if first_diffusion.count() \
 | 
			
		||||
                              else None
 | 
			
		||||
            diffusions.append(Diffusion(
 | 
			
		||||
                                 program = self.program,
 | 
			
		||||
                                 type = Diffusion.Type['unconfirmed'],
 | 
			
		||||
                                 initial = first_diffusion if self.initial else None,
 | 
			
		||||
                                 date = date,
 | 
			
		||||
                                 duration = self.duration,
 | 
			
		||||
                             ))
 | 
			
		||||
        return diffusions
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        frequency = [ x for x,y in Schedule.Frequency.items()
 | 
			
		||||
                        if y == self.frequency ]
 | 
			
		||||
        return self.program.name + ': ' + frequency[0] + ' (' + str(self.date) + ')'
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Schedule')
 | 
			
		||||
        verbose_name_plural = _('Schedules')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Station (Nameable):
 | 
			
		||||
    """
 | 
			
		||||
    A Station regroup one or more programs (stream and normal), and is the top
 | 
			
		||||
    element used to generate streams outputs and configuration.
 | 
			
		||||
    """
 | 
			
		||||
    active = models.BooleanField(
 | 
			
		||||
        _('active'),
 | 
			
		||||
        default = True,
 | 
			
		||||
        help_text = _('this station is active')
 | 
			
		||||
    )
 | 
			
		||||
    public = models.BooleanField(
 | 
			
		||||
        _('public'),
 | 
			
		||||
        default = True,
 | 
			
		||||
        help_text = _('information are available to the public'),
 | 
			
		||||
    )
 | 
			
		||||
    fallback = models.FilePathField(
 | 
			
		||||
        _('fallback song'),
 | 
			
		||||
        match = r'(' + '|'.join(settings.AIRCOX_SOUND_FILE_EXT) \
 | 
			
		||||
                                    .replace('.', r'\.') + ')$',
 | 
			
		||||
        recursive = True,
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('use this song file if there is a problem and nothing is '
 | 
			
		||||
                      'played')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Program (Nameable):
 | 
			
		||||
    """
 | 
			
		||||
    A Program can either be a Streamed or a Scheduled program.
 | 
			
		||||
 | 
			
		||||
    A Streamed program is used to generate non-stop random playlists when there
 | 
			
		||||
    is not scheduled diffusion. In such a case, a Stream is used to describe
 | 
			
		||||
    diffusion informations.
 | 
			
		||||
 | 
			
		||||
    A Scheduled program has a schedule and is the one with a normal use case.
 | 
			
		||||
    """
 | 
			
		||||
    station = models.ForeignKey(
 | 
			
		||||
        Station,
 | 
			
		||||
        verbose_name = _('station')
 | 
			
		||||
    )
 | 
			
		||||
    active = models.BooleanField(
 | 
			
		||||
        _('active'),
 | 
			
		||||
        default = True,
 | 
			
		||||
        help_text = _('if not set this program is no longer active')
 | 
			
		||||
    )
 | 
			
		||||
    public = models.BooleanField(
 | 
			
		||||
        _('public'),
 | 
			
		||||
        default = True,
 | 
			
		||||
        help_text = _('information are available to the public')
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def path (self):
 | 
			
		||||
        """
 | 
			
		||||
        Return the path to the programs directory
 | 
			
		||||
        """
 | 
			
		||||
        return os.path.join(settings.AIRCOX_PROGRAMS_DIR,
 | 
			
		||||
                            self.slug + '_' + str(self.id) )
 | 
			
		||||
 | 
			
		||||
    def ensure_dir (self, subdir = None):
 | 
			
		||||
        """
 | 
			
		||||
        Make sur the program's dir exists (and optionally subdir). Return True
 | 
			
		||||
        if the dir (or subdir) exists.
 | 
			
		||||
        """
 | 
			
		||||
        path = self.path
 | 
			
		||||
        if not os.path.exists(path):
 | 
			
		||||
            os.mkdir(path)
 | 
			
		||||
 | 
			
		||||
        if subdir:
 | 
			
		||||
            path = os.path.join(path, subdir)
 | 
			
		||||
            if not os.path.exists(path):
 | 
			
		||||
                os.mkdir(path)
 | 
			
		||||
        return os.path.exists(path)
 | 
			
		||||
 | 
			
		||||
    def find_schedule (self, date):
 | 
			
		||||
        """
 | 
			
		||||
        Return the first schedule that matches a given date.
 | 
			
		||||
        """
 | 
			
		||||
        schedules = Schedule.objects.filter(program = self)
 | 
			
		||||
        for schedule in schedules:
 | 
			
		||||
            if schedule.match(date, check_time = False):
 | 
			
		||||
                return schedule
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Diffusion (models.Model):
 | 
			
		||||
    """
 | 
			
		||||
    A Diffusion is an occurrence of a Program that is scheduled on the
 | 
			
		||||
    station's timetable. It can be a rerun of a previous diffusion. In such
 | 
			
		||||
    a case, use rerun's info instead of its own.
 | 
			
		||||
 | 
			
		||||
    A Diffusion without any rerun is named Episode (previously, a
 | 
			
		||||
    Diffusion was different from an Episode, but in the end, an
 | 
			
		||||
    episode only has a name, a linked program, and a list of sounds, so we
 | 
			
		||||
    finally merge theme).
 | 
			
		||||
 | 
			
		||||
    A Diffusion can have different types:
 | 
			
		||||
    - default: simple diffusion that is planified / did occurred
 | 
			
		||||
    - unconfirmed: a generated diffusion that has not been confirmed and thus
 | 
			
		||||
        is not yet planified
 | 
			
		||||
    - cancel: the diffusion has been canceled
 | 
			
		||||
    - stop: the diffusion has been manually stopped
 | 
			
		||||
    """
 | 
			
		||||
    Type = {
 | 
			
		||||
        'normal':       0x00,   # diffusion is planified
 | 
			
		||||
        'unconfirmed':  0x01,   # scheduled by the generator but not confirmed for diffusion
 | 
			
		||||
        'cancel':       0x02,   # diffusion canceled
 | 
			
		||||
    }
 | 
			
		||||
    for key, value in Type.items():
 | 
			
		||||
        ugettext_lazy(key)
 | 
			
		||||
 | 
			
		||||
    # common
 | 
			
		||||
    program = models.ForeignKey (
 | 
			
		||||
        'Program',
 | 
			
		||||
        verbose_name = _('program'),
 | 
			
		||||
    )
 | 
			
		||||
    sounds = models.ManyToManyField(
 | 
			
		||||
        Sound,
 | 
			
		||||
        blank = True,
 | 
			
		||||
        verbose_name = _('sounds'),
 | 
			
		||||
    )
 | 
			
		||||
    # specific
 | 
			
		||||
    type = models.SmallIntegerField(
 | 
			
		||||
        verbose_name = _('type'),
 | 
			
		||||
        choices = [ (y, x) for x,y in Type.items() ],
 | 
			
		||||
    )
 | 
			
		||||
    initial = models.ForeignKey (
 | 
			
		||||
        'self',
 | 
			
		||||
        verbose_name = _('initial'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('the diffusion is a rerun of this one')
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField( _('start of the diffusion') )
 | 
			
		||||
    duration = models.TimeField(
 | 
			
		||||
        _('duration'),
 | 
			
		||||
        help_text = _('regular duration'),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def archives_duration (self):
 | 
			
		||||
        """
 | 
			
		||||
        Get total duration of the archives. May differ from the schedule
 | 
			
		||||
        duration.
 | 
			
		||||
        """
 | 
			
		||||
        sounds = self.initial.sounds if self.initial else self.sounds
 | 
			
		||||
        r = [ sound.duration
 | 
			
		||||
                for sound in sounds.filter(type = Sound.Type['archive'])
 | 
			
		||||
                if sound.duration ]
 | 
			
		||||
        return utils.time_sum(r) if r else self.duration
 | 
			
		||||
 | 
			
		||||
    def get_archives (self):
 | 
			
		||||
        """
 | 
			
		||||
        Return an ordered list of archives sounds for the given episode.
 | 
			
		||||
        """
 | 
			
		||||
        sounds = self.initial.sounds if self.initial else self.sounds
 | 
			
		||||
        r = [ sound for sound in sounds.all().order_by('path')
 | 
			
		||||
              if sound.type == Sound.Type['archive'] ]
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_next (cl, station = None, date = None, **filter_args):
 | 
			
		||||
        """
 | 
			
		||||
        Return a queryset with the upcoming diffusions, ordered by
 | 
			
		||||
        +date
 | 
			
		||||
        """
 | 
			
		||||
        filter_args['date__gte'] = date_or_default(date)
 | 
			
		||||
        if station:
 | 
			
		||||
            filter_args['program__station'] = station
 | 
			
		||||
        return cl.objects.filter(**filter_args).order_by('date')
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_prev (cl, station = None, date = None, **filter_args):
 | 
			
		||||
        """
 | 
			
		||||
        Return a queryset with the previous diffusion, ordered by
 | 
			
		||||
        -date
 | 
			
		||||
        """
 | 
			
		||||
        filter_args['date__lte'] = date_or_default(date)
 | 
			
		||||
        if station:
 | 
			
		||||
            filter_args['program__station'] = station
 | 
			
		||||
        return cl.objects.filter(**filter_args).order_by('-date')
 | 
			
		||||
 | 
			
		||||
    def get_conflicts (self):
 | 
			
		||||
        """
 | 
			
		||||
        Return a list of conflictual diffusions, based on the scheduled duration.
 | 
			
		||||
 | 
			
		||||
        Note: for performance reason, check next and prev are limited to a
 | 
			
		||||
        certain amount of diffusions.
 | 
			
		||||
        """
 | 
			
		||||
        r = []
 | 
			
		||||
        # prev
 | 
			
		||||
        qs = self.get_prev(self.program.station, self.date)
 | 
			
		||||
        count = 0
 | 
			
		||||
        for diff in qs:
 | 
			
		||||
            if diff.pk == self.pk:
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            end = diff.date + utils.to_timedelta(diff.duration)
 | 
			
		||||
            if end > self.date:
 | 
			
		||||
                r.append(diff)
 | 
			
		||||
                continue
 | 
			
		||||
            count+=1
 | 
			
		||||
            if count > 5: break
 | 
			
		||||
 | 
			
		||||
        # next
 | 
			
		||||
        end = self.date + utils.to_timedelta(self.duration)
 | 
			
		||||
        qs = self.get_next(self.program.station, self.date)
 | 
			
		||||
        count = 0
 | 
			
		||||
        for diff in qs:
 | 
			
		||||
            if diff.pk == self.pk:
 | 
			
		||||
                continue
 | 
			
		||||
 | 
			
		||||
            if diff.date < end:
 | 
			
		||||
                r.append(diff)
 | 
			
		||||
                continue
 | 
			
		||||
            count+=1
 | 
			
		||||
            if count > 5: break
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
    def save (self, *args, **kwargs):
 | 
			
		||||
        if self.initial:
 | 
			
		||||
            if self.initial.initial:
 | 
			
		||||
                self.initial = self.initial.initial
 | 
			
		||||
            self.program = self.initial.program
 | 
			
		||||
        super(Diffusion, self).save(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        return self.program.name + ', ' + \
 | 
			
		||||
                self.date.strftime('%Y-%m-%d %H:%M') +\
 | 
			
		||||
                '' # FIXME str(self.type_display)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Diffusion')
 | 
			
		||||
        verbose_name_plural = _('Diffusions')
 | 
			
		||||
 | 
			
		||||
        permissions = (
 | 
			
		||||
            ('programming', _('edit the diffusion\'s planification')),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
class Log (models.Model):
 | 
			
		||||
    """
 | 
			
		||||
    Log a played sound start and stop, or a single message
 | 
			
		||||
    """
 | 
			
		||||
    source = models.CharField(
 | 
			
		||||
        _('source'),
 | 
			
		||||
        max_length = 64,
 | 
			
		||||
        help_text = 'source information',
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    sound = models.ForeignKey(
 | 
			
		||||
        'Sound',
 | 
			
		||||
        help_text = _('played sound'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField(
 | 
			
		||||
        'date',
 | 
			
		||||
    )
 | 
			
		||||
    comment = models.CharField(
 | 
			
		||||
        max_length = 512,
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    related_type = models.ForeignKey(
 | 
			
		||||
        ContentType,
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    related_id = models.PositiveIntegerField(
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
    )
 | 
			
		||||
    related_object = GenericForeignKey(
 | 
			
		||||
        'related_type', 'related_id',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def print (self):
 | 
			
		||||
        print(str(self), ':', self.comment or '')
 | 
			
		||||
        if self.diffusion:
 | 
			
		||||
            print(' - diffusion #' + str(self.diffusion.id))
 | 
			
		||||
        if self.sound:
 | 
			
		||||
            print(' - sound #' + str(self.sound.id), self.sound.path)
 | 
			
		||||
 | 
			
		||||
    def __str__ (self):
 | 
			
		||||
        return self.date.strftime('%Y-%m-%d %H:%M') + ', ' + self.source
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										35
									
								
								aircox/programs/settings.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										35
									
								
								aircox/programs/settings.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
def ensure (key, default):
 | 
			
		||||
    globals()[key] = getattr(settings, key, default)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Directory for the programs data
 | 
			
		||||
ensure('AIRCOX_PROGRAMS_DIR',
 | 
			
		||||
       os.path.join(settings.MEDIA_ROOT, 'programs'))
 | 
			
		||||
 | 
			
		||||
# Default directory for the sounds that not linked to a program
 | 
			
		||||
ensure('AIRCOX_SOUND_DEFAULT_DIR',
 | 
			
		||||
       os.path.join(AIRCOX_PROGRAMS_DIR, 'defaults'))
 | 
			
		||||
# Sub directory used for the complete episode sounds
 | 
			
		||||
ensure('AIRCOX_SOUND_ARCHIVES_SUBDIR', 'archives')
 | 
			
		||||
# Sub directory used for the excerpts of the episode
 | 
			
		||||
ensure('AIRCOX_SOUND_EXCERPTS_SUBDIR', 'excerpts')
 | 
			
		||||
 | 
			
		||||
# Quality attributes passed to sound_quality_check from sounds_monitor
 | 
			
		||||
ensure('AIRCOX_SOUND_QUALITY', {
 | 
			
		||||
        'attribute': 'RMS lev dB',
 | 
			
		||||
        'range': (-18.0, -8.0),
 | 
			
		||||
        'sample_length': 120,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Extension of sound files
 | 
			
		||||
ensure('AIRCOX_SOUND_FILE_EXT',
 | 
			
		||||
        ('.ogg','.flac','.wav','.mp3','.opus'))
 | 
			
		||||
 | 
			
		||||
# Stream for the scheduled diffusions
 | 
			
		||||
ensure('AIRCOX_SCHEDULED_STREAM', 0)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										79
									
								
								aircox/programs/tests.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										79
									
								
								aircox/programs/tests.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,79 @@
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
from django.utils import timezone as tz
 | 
			
		||||
 | 
			
		||||
from aircox.programs.models import *
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Programs (TestCase):
 | 
			
		||||
    def setUp (self):
 | 
			
		||||
        stream = Stream.objects.get_or_create(
 | 
			
		||||
            name = 'diffusions',
 | 
			
		||||
            defaults = { 'type': Stream.Type['schedule'] }
 | 
			
		||||
        )[0]
 | 
			
		||||
        Program.objects.create(name = 'source', stream = stream)
 | 
			
		||||
        Program.objects.create(name = 'microouvert', stream = stream)
 | 
			
		||||
 | 
			
		||||
        self.schedules = {}
 | 
			
		||||
        self.programs = {}
 | 
			
		||||
 | 
			
		||||
    def test_create_programs_schedules (self):
 | 
			
		||||
        program = Program.objects.get(name = 'source')
 | 
			
		||||
 | 
			
		||||
        sched_0 = self.create_schedule(program, 'one on two', [
 | 
			
		||||
                tz.datetime(2015, 10, 2, 18),
 | 
			
		||||
                tz.datetime(2015, 10, 16, 18),
 | 
			
		||||
                tz.datetime(2015, 10, 30, 18),
 | 
			
		||||
            ]
 | 
			
		||||
        )
 | 
			
		||||
        sched_1 = self.create_schedule(program, 'one on two', [
 | 
			
		||||
                tz.datetime(2015, 10, 5, 18),
 | 
			
		||||
                tz.datetime(2015, 10, 19, 18),
 | 
			
		||||
            ],
 | 
			
		||||
            rerun = sched_0
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.programs[program.pk] = program
 | 
			
		||||
 | 
			
		||||
        program = Program.objects.get(name = 'microouvert')
 | 
			
		||||
        # special case with november first week starting on sunday
 | 
			
		||||
        sched_2 = self.create_schedule(program, 'first and third', [
 | 
			
		||||
                tz.datetime(2015, 11, 6, 18),
 | 
			
		||||
                tz.datetime(2015, 11, 20, 18),
 | 
			
		||||
            ],
 | 
			
		||||
            date = tz.datetime(2015, 10, 23, 18),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def create_schedule (self, program, frequency, dates, date = None, rerun = None):
 | 
			
		||||
        frequency = Schedule.Frequency[frequency]
 | 
			
		||||
        schedule = Schedule(
 | 
			
		||||
            program = program,
 | 
			
		||||
            frequency = frequency,
 | 
			
		||||
            date = date or dates[0],
 | 
			
		||||
            rerun = rerun,
 | 
			
		||||
            duration = datetime.time(1, 30)
 | 
			
		||||
        )
 | 
			
		||||
        print(schedule.__dict__)
 | 
			
		||||
        schedule.save()
 | 
			
		||||
 | 
			
		||||
        self.schedules[schedule.pk] = (schedule, dates)
 | 
			
		||||
        return schedule
 | 
			
		||||
 | 
			
		||||
    def test_check_schedule (self):
 | 
			
		||||
        for schedule, dates in self.schedules:
 | 
			
		||||
            dates = [ tz.make_aware(date) for date in dates ]
 | 
			
		||||
            dates.sort()
 | 
			
		||||
 | 
			
		||||
            # dates
 | 
			
		||||
            dates_ = schedule.dates_of_month(dates[0])
 | 
			
		||||
            dates_.sort()
 | 
			
		||||
            self.assertEqual(dates_, dates)
 | 
			
		||||
 | 
			
		||||
            # diffusions
 | 
			
		||||
            dates_ = schedule.diffusions_of_month(dates[0])
 | 
			
		||||
            dates_ = [date_.date for date_ in dates_]
 | 
			
		||||
            dates_.sort()
 | 
			
		||||
            self.assertEqual(dates_, dates)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										30
									
								
								aircox/programs/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								aircox/programs/utils.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,30 @@
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
def to_timedelta (time):
 | 
			
		||||
    """
 | 
			
		||||
    Transform a datetime or a time instance to a timedelta,
 | 
			
		||||
    only using time info
 | 
			
		||||
    """
 | 
			
		||||
    return datetime.timedelta(
 | 
			
		||||
        hours = time.hour,
 | 
			
		||||
        minutes = time.minute,
 | 
			
		||||
        seconds = time.second
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
def seconds_to_time (seconds):
 | 
			
		||||
    """
 | 
			
		||||
    Seconds to datetime.time
 | 
			
		||||
    """
 | 
			
		||||
    minutes, seconds = divmod(seconds, 60)
 | 
			
		||||
    hours, minutes = divmod(minutes, 60)
 | 
			
		||||
    return datetime.time(hour = hours, minute = minutes, second = seconds)
 | 
			
		||||
 | 
			
		||||
def time_sum (times):
 | 
			
		||||
    """
 | 
			
		||||
    Sum up a list of time elements
 | 
			
		||||
    """
 | 
			
		||||
    seconds = sum([ time.hour * 3600 + time.minute * 60 + time.second
 | 
			
		||||
                    for time in times ])
 | 
			
		||||
    return seconds_to_time(seconds)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								aircox/programs/views.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										16
									
								
								aircox/programs/views.py
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
from django.db                      import models
 | 
			
		||||
from django.shortcuts               import render
 | 
			
		||||
from django.core.serializers.json   import DjangoJSONEncoder
 | 
			
		||||
from django.utils                   import timezone, dateformat
 | 
			
		||||
 | 
			
		||||
from django.views.generic           import ListView
 | 
			
		||||
from django.views.generic           import DetailView
 | 
			
		||||
from django.utils.translation       import ugettext as _, ugettext_lazy
 | 
			
		||||
 | 
			
		||||
from aircox.programs.models                import *
 | 
			
		||||
import aircox.programs.settings
 | 
			
		||||
import aircox.programs.utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user