Exemple #1
0
    def create_manual(self):
        self.form = forms.PaperForm(self.request.POST, self.request.FILES)
        if self.form.is_valid():
            data = self.form.cleaned_data
            #print data
            
            p = models.Paper(user = self.request.user, title = data['title'], 
                    authors = data['authors'], journal = data['journal'], 
                    year = data['year'], volume = data['volume'], 
                    issue = data['issue'], pages = data['pages'], 
                    url = data['url'])
            #We save first in order to have a hash automatically generated
            p.save()

            if data['file']:
                #Save file. data.file is an UploadedFile object.
                path = os.path.join(settings.UPLOAD_ROOT, self.request.user.username,
                        p.hash)
                helpers.save_uploaded_file(data['file'], path)

                p.file = data['file'].name
                p.save()

            #Redirect to dashboard.
            messages.success(self.request, 'Paper was successfully added.')
            return redirect('Paper#index')

        #Otherwise, invalid form. Re-render the new_manual page with form errors.
        #Try to detect if the paper is a preprint/ASAP article and warn user
        #to check the ASAP box if so.
        if not self.request.POST.get('is_asap', False) and \
           not self.request.POST.get('year', False):
            self.is_asap_detected = True
        return self.new_manual.render()
Exemple #2
0
    def update(self, paper_id):
        self.paper = get_object_or_404(models.Paper, user = self.request.user, local_id = paper_id)
        p_file = self.paper.file #Need to make a copy of this since the ModelForm overwrites it.
        self.form = forms.PaperForm(self.request.POST, self.request.FILES, instance = self.paper)

        if self.form.is_valid():
            data = self.form.cleaned_data
            #print data

            #NOTE: Checking for unicode type is an ugly hack.
            if data['file'] and type(data['file']) != unicode:
                path = os.path.join(settings.UPLOAD_ROOT, self.request.user.username,
                        self.paper.hash)

                #If there is an existing file, delete that first. Also delete
                #the associated thumbnail.
                if p_file:
                    try:
                        os.unlink(os.path.join(path, p_file))
                        os.unlink(os.path.join(path, settings.THUMBNAIL_FILENAME % self.paper.hash))
                        self.paper.has_thumbnail = False
                        self.paper.save()

                        #Also delete the existing crocodoc upload
                        self.paper.crocodoc.delete()
                    except OSError:
                        #The file is already missing.
                        pass

                #Save file. data.file is an UploadedFile object.
                helpers.save_uploaded_file(data['file'], path)

                self.form.file = data['file'].name
                self.paper = self.form.save() #save first so that we can generate thumbnail

            self.form.save()

            #Redirect to individual paper.
            messages.success(self.request, 'Paper was successfully updated.')
            return redirect('Paper#show', paper_id)

        #Otherwise, invalid form. Re-render the edit page with form errors.
        #Try to detect if the paper is a preprint/ASAP article and warn user
        #to check the ASAP box if so.
        if not self.request.POST.get('is_asap', False) and \
           not self.request.POST.get('year', False):
            self.is_asap_detected = True
        return self.edit.render()