src/p4l/api/views.py
author ymh <ymh.work@gmail.com>
Tue, 01 Oct 2013 02:14:08 +0200
changeset 126 a345f1a67bf1
parent 119 ece69ca3ac24
child 131 f1854630734f
permissions -rw-r--r--
Python licence headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
126
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     2
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     3
# Copyright IRI (2013)
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     4
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     5
# contact@iri.centrepompidou.fr
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     6
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     7
# This software is governed by the CeCILL-B license under French law and
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     8
# abiding by the rules of distribution of free software.  You can  use, 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
     9
# modify and/ or redistribute the software under the terms of the CeCILL-B
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    10
# license as circulated by CEA, CNRS and INRIA at the following URL
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    11
# "http://www.cecill.info". 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    12
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    13
# As a counterpart to the access to the source code and  rights to copy,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    14
# modify and redistribute granted by the license, users are provided only
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    15
# with a limited warranty  and the software's author,  the holder of the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    16
# economic rights,  and the successive licensors  have only  limited
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    17
# liability. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    18
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    19
# In this respect, the user's attention is drawn to the risks associated
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    20
# with loading,  using,  modifying and/or developing or reproducing the
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    21
# software by the user in light of its specific status of free software,
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    22
# that may mean  that it is complicated to manipulate,  and  that  also
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    23
# therefore means  that it is reserved for developers  and  experienced
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    24
# professionals having in-depth computer knowledge. Users are therefore
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    25
# encouraged to load and test the software's suitability as regards their
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    26
# requirements in conditions enabling the security of their systems and/or 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    27
# data to be ensured and,  more generally, to use and operate it in the 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    28
# same conditions as regards security. 
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    29
#
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    30
# The fact that you are presently reading this means that you have had
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    31
# knowledge of the CeCILL-B license and that you accept its terms.
a345f1a67bf1 Python licence headers
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    32
#
16
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
from p4l.api.serializers import RecordSerializer
119
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    35
from p4l.models import Record, signals
16
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
from rest_framework import viewsets
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
class RecordViewSet(viewsets.ModelViewSet):
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    '''
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    API endpoint that allow Record to be viewed or edited
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    '''
104
b66ca6275115 add creation + modification fields
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    42
    def pre_save(self, obj):
119
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    43
        super(RecordViewSet, self).pre_save(obj)
104
b66ca6275115 add creation + modification fields
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    44
        if self.request and hasattr(self.request, 'user') and self.request.user:
108
c08f9b46a6c5 use PEP8 convention on system fields for Records
ymh <ymh.work@gmail.com>
parents: 104
diff changeset
    45
            obj.modified_by =  self.request.user
16
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
119
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    47
    def post_save(self, obj, created=False):
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    48
        super(RecordViewSet, self).post_save(obj, created=created)
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    49
        signals.record_saved.send(Record, instance=obj, created=created)
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    50
        
ece69ca3ac24 - correct import pour indexation
ymh <ymh.work@gmail.com>
parents: 108
diff changeset
    51
16
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    queryset = Record.objects.all()
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    serializer_class = RecordSerializer
19fe06edb58d Add api for records
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    lookup_field = 'identifier'