~jonobacon/ubuntu-accomplishments-system/accomplishments-web-editor

1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
1
import os
2
import ConfigParser
3
4 by Jono Bacon
A bunch of other changes, including adding the submission of new items.
4
from editor.models import Application, Accomplishment, AccomplishmentDiff, Icon, Category, AccomplishmentForm
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
5
from django.shortcuts import render_to_response
2 by Jono Bacon
Added support for multiple accomplishments sets.
6
from django.http import HttpResponseRedirect, HttpResponse
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
7
from django.template import RequestContext
8
from django.core.exceptions import ObjectDoesNotExist
4 by Jono Bacon
A bunch of other changes, including adding the submission of new items.
9
from django.core.urlresolvers import reverse
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
10
11
# Create your views here.
12
13
def index(request):
14
    # get accomplishments files
2 by Jono Bacon
Added support for multiple accomplishments sets.
15
    path = "/home/jono/accomplishments"
16
    accompath = os.path.join(path, "accomplishments")
17
    accomdirs = os.walk(accompath).next()[1]
18
    print accomdirs
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
19
    accomfiles = []
20
2 by Jono Bacon
Added support for multiple accomplishments sets.
21
    for ad in accomdirs:
22
        for r,d,f in os.walk(os.path.join(accompath, ad)):
23
            for i in f:
24
                if i.endswith(".accomplishment"):
25
                    res = os.path.join(r,i)
26
                    accomfiles.append(res)
27
28
    print accomfiles
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
29
30
    # get accomplishmets from database
31
    accoms = Accomplishment.objects.all()
32
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
33
    data = []
34
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
35
    # do we need to scan data from files and upload the database?
36
    if len(accomfiles) > len(accoms):
37
        print "need to scan"
38
39
        config = ConfigParser.ConfigParser()
40
41
        section = "accomplishment"
42
        appsfinal = []
43
        iconsfinal = []
44
        catsfinal = []
45
46
        # build a collection of data from the files
47
        for a in accomfiles:
48
            temp = {}
49
            config.read(a)
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
50
            temp["descriptor"] = os.path.split(a)[1].split(".")[0]
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
51
            temp["title"] = config.get(section, "title")
52
            temp["description"] = config.get(section, "description")            
53
            temp["application"] = config.get(section, "application")
54
55
            if temp["application"] not in appsfinal:
56
                appsfinal.append(temp["application"])          
57
            
58
            temp["category"] = config.get(section, "category")
59
60
            catsdict = {}
61
            catsdict["application"] = temp["application"]
62
            catsdict["category"] = temp["category"]
63
            catsfinal.append(catsdict)           
64
            
65
            temp["icon"] = config.get(section, "icon")
66
67
            icondict = {}
68
            icondict["application"] = temp["application"]
69
            icondict["icon"] = temp["icon"]
70
            iconsfinal.append(icondict)
71
            
72
            temp["depends"] = config.get(section, "depends")
73
            temp["needs-signing"] = config.getboolean(section, "needs-signing")
74
            temp["needs-information"] = config.get(section, "needs-information")
75
            temp["summary"] = config.get(section, "summary")
76
            temp["steps"] = config.get(section, "steps")
77
            temp["links"] = config.get(section, "links")
78
            temp["help"] = config.get(section, "help")
79
            
80
            data.append(temp)
81
82
        # first update the applications
83
84
        for a in appsfinal:
85
            try:
86
                Application.objects.get(descriptor=a)
87
            except ObjectDoesNotExist:
88
                newapp = Application(descriptor=a, label=a)
89
                newapp.save()
90
91
        # now update the icons
92
93
        iconsfinalf = [dict(y) for y in set(tuple(x.items()) for x in iconsfinal)]
94
95
        for a in iconsfinalf:
96
            try:
2 by Jono Bacon
Added support for multiple accomplishments sets.
97
                iconapp = Application.objects.get(descriptor=a["application"])
98
                Icon.objects.get(application=iconapp, filename=a["icon"])
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
99
            except ObjectDoesNotExist:
100
                iconapp = Application.objects.get(descriptor=a["application"])
101
                newicon = Icon(application=iconapp, filename=a["icon"])
102
                newicon.save()      
103
104
        # update the categories
105
106
        catsfinalf = [dict(y) for y in set(tuple(x.items()) for x in catsfinal)]
107
108
        for a in catsfinalf:
109
            try:
2 by Jono Bacon
Added support for multiple accomplishments sets.
110
                categoryapp = Application.objects.get(descriptor=a["application"])
111
                Category.objects.get(application=categoryapp, label=a["category"])
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
112
            except ObjectDoesNotExist:
113
                categoryapp = Application.objects.get(descriptor=a["application"])
114
                newcat = Category(application=categoryapp, label=a["category"])
115
                newcat.save()
116
117
        # add the data to the database
118
        for d in data:
119
            app = Application.objects.get(descriptor=d["application"])
120
            i = Icon.objects.get(application=app, filename=d["icon"])
121
            cat = Category.objects.get(application=app, label=d["category"])
122
            
123
            a = Accomplishment(application = app,
124
                title = d["title"],
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
125
                descriptor = d["descriptor"],
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
126
                description = d["description"],
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
127
                needsinformation = d["needs-information"],
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
128
                needssigning = True,
129
                depends = None,
130
                summary = d["summary"],
131
                steps = d["steps"],
132
                links = d["links"],
133
                helpres = d["help"])
134
135
            a.save()
136
            
137
            a.categories.add(cat)
138
            a.icon.add(i)
139
            
140
            a.save()
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
141
142
        print data
143
        # update the accomplishments that depend on others
144
        for d in data:
145
            if d["depends"] is not None:
146
                dep = d["depends"].split("/")[1]
147
                print dep
148
                app = Application.objects.get(descriptor=d["application"])
149
                targetacc = Accomplishment.objects.get(application=app, descriptor=d["descriptor"])
150
                depacc = Accomplishment.objects.get(application=app, descriptor=dep)
151
                targetacc.depends = depacc
152
                targetacc.save()
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
153
    
2 by Jono Bacon
Added support for multiple accomplishments sets.
154
    apps = Application.objects.all()
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
155
    output = ', '.join([p.title for p in accoms])
2 by Jono Bacon
Added support for multiple accomplishments sets.
156
    return render_to_response('index.html', {'accoms': accoms, 'apps': apps}, RequestContext(request))
1 by Jono Bacon
First cut. Reads in accomplishments and renders them.
157
158
def detail(request, accom_id):
159
    a = Accomplishment.objects.get(id=accom_id)
160
161
    a_form = AccomplishmentForm(instance=a)
2 by Jono Bacon
Added support for multiple accomplishments sets.
162
    a_form.fields["categories"].queryset = Category.objects.filter(application=a.application)
163
    a_form.fields["icon"].queryset = Icon.objects.filter(application=a.application)
3 by Jono Bacon
Added support all remaining fields (inc. extrainformation and depends).
164
    a_form.fields["depends"].queryset = Accomplishment.objects.filter(application=a.application)
2 by Jono Bacon
Added support for multiple accomplishments sets.
165
166
    return render_to_response('detail.html', {'AccomplishmentForm': a_form, 'accom': a}, RequestContext(request))                
167
168
def edit(request, accom_id):
4 by Jono Bacon
A bunch of other changes, including adding the submission of new items.
169
    print "submitting data"
170
    print request.POST["description"]
171
    print request.POST["depends"]
172
    print request.POST["summary"]
173
    print request.POST["steps"]
174
    print request.POST["links"]
175
    print request.POST["helpres"]
176
    print request.POST["categories"]
177
    print request.POST["icon"]
178
    print accom_id
179
    #print request.POST["application"]
180
181
    targetacc = Accomplishment.objects.get(id=accom_id)
182
    diffcat = Category.objects.get(id=request.POST["categories"])
183
    difficon = Icon.objects.get(id=request.POST["icon"])
184
    print difficon
185
    print targetacc.title
186
187
    a = AccomplishmentDiff(accomplishment = targetacc,
188
        user = "Jono Bacon",
189
        description = request.POST["description"],
190
        summary = request.POST["summary"],
191
        steps = request.POST["steps"],
192
        links = request.POST["links"],
193
        helpres = request.POST["helpres"])    
194
195
    a.save()
196
    return HttpResponseRedirect(reverse('editor.views.results', args=(a.id,)))
2 by Jono Bacon
Added support for multiple accomplishments sets.
197
198
def results():
199
    print "results"
4 by Jono Bacon
A bunch of other changes, including adding the submission of new items.
200
201
def queue(request):
202
    print "queue"
203
    accoms = Accomplishment.objects.all()
204
    apps = Application.objects.all()
205
    diffs = AccomplishmentDiff.objects.all()
206
    return render_to_response('queue.html', {'accoms': accoms, 'apps': apps, 'diffs' : diffs }, RequestContext(request))