ManualCleanupForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_task_kwargs() 0 6 1
A cleanup() 0 4 1
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
from collections import OrderedDict
22
23
from django import forms
24
from django.forms import widgets, ValidationError
25
26
from django_ace import AceWidget
27
from suit.widgets import LinkedSelect
28
from tinymce.widgets import TinyMCE
29
from celery import signature
30
31
from omaha.models import Application, Version, Action, Data
32
33
34
__all__ = ['ApplicationAdminForm', 'VersionAdminForm', 'ActionAdminForm']
35
36
37
class ApplicationAdminForm(forms.ModelForm):
38
    def clean_id(self):
39
        return self.cleaned_data["id"].upper()
40
41
    class Meta:
42
        model = Application
43
        exclude = []
44
45
46
class DataAdminForm(forms.ModelForm):
47
    class Meta:
48
        model = Data
49
        exclude = []
50
        widgets = {
51
            'value': AceWidget(mode='json', theme='monokai', width='600px', height='300px'),
52
        }
53
54
55
class VersionAdminForm(forms.ModelForm):
56
    class Meta:
57
        model = Version
58
        exclude = []
59
        widgets = {
60
            'app': LinkedSelect,
61
            'release_notes': TinyMCE(),
62
            'file_size': widgets.TextInput(attrs=dict(disabled='disabled')),
63
        }
64
65
    def clean_file_size(self):
66
        if 'file' not in self.cleaned_data:
67
            raise ValidationError('')
68
        _file = self.cleaned_data["file"]
69
        return _file.size
70
71
72
class ActionAdminForm(forms.ModelForm):
73
    SUCCESSSACTION_CHOICES = (
74
        ('default', 'default'),
75
        ('exitsilently', 'exitsilently'),
76
        ('exitsilentlyonlaunchcmd', 'exitsilentlyonlaunchcmd')
77
    )
78
    successsaction = forms.ChoiceField(choices=SUCCESSSACTION_CHOICES)
79
80
    class Meta:
81
        model = Action
82
        exclude = []
83
84
85
class ManualCleanupForm(forms.Form):
86
    limit_days = forms.IntegerField(min_value=1, required=False, label='Maximum age (days)',
87
                                    help_text=' - remove objects older than X days')
88
    limit_size = forms.IntegerField(min_value=1, required=False, label='Purge used space (Gb)',
89
                                    help_text=" - remove old objects until total size won't reach X GB")
90
91
    def cleanup(self):
92
        model = self.initial['type'].split('__')
93
        task_kwargs = self.get_task_kwargs()
94
        signature("tasks.deferred_manual_cleanup", args=(model,), kwargs=task_kwargs).apply_async(queue='limitation')
95
96
    def get_task_kwargs(self):
97
        task_kwargs = dict(
98
            limit_size=self.cleaned_data['limit_size'],
99
            limit_days=self.cleaned_data['limit_days'],
100
        )
101
        return task_kwargs
102
103
104
class CrashManualCleanupForm(ManualCleanupForm):
105
    limit_duplicated = forms.IntegerField(min_value=1, required=False, label='Maximum amount of duplicates',
106
                                          help_text=" - remove old duplicate crashes until their number won't equal X ")
107
108
    def __init__(self, *args, **kwargs):
109
        super(CrashManualCleanupForm, self).__init__(*args, **kwargs)
110
        fields = OrderedDict()
111
        for key in ("limit_duplicated", "limit_days", "limit_size"):
112
            fields[key] = self.fields.pop(key)
113
        for key, value in self.fields.items():
114
            fields[key] = value
115
        self.fields = fields
116
117
    def get_task_kwargs(self):
118
        task_kwargs = super(CrashManualCleanupForm, self).get_task_kwargs()
119
        task_kwargs.update(dict(limit_duplicated=self.cleaned_data['limit_duplicated']))
120
        return task_kwargs
121