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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import ConfigParser

from editor.models import Application, Accomplishment, AccomplishmentDiff, Icon, Category, AccomplishmentForm
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse

# Create your views here.

def index(request):
    # get accomplishments files
    path = "/home/jono/accomplishments"
    accompath = os.path.join(path, "accomplishments")
    accomdirs = os.walk(accompath).next()[1]
    print accomdirs
    accomfiles = []

    for ad in accomdirs:
        for r,d,f in os.walk(os.path.join(accompath, ad)):
            for i in f:
                if i.endswith(".accomplishment"):
                    res = os.path.join(r,i)
                    accomfiles.append(res)

    print accomfiles

    # get accomplishmets from database
    accoms = Accomplishment.objects.all()

    data = []

    # do we need to scan data from files and upload the database?
    if len(accomfiles) > len(accoms):
        print "need to scan"

        config = ConfigParser.ConfigParser()

        section = "accomplishment"
        appsfinal = []
        iconsfinal = []
        catsfinal = []

        # build a collection of data from the files
        for a in accomfiles:
            temp = {}
            config.read(a)
            temp["descriptor"] = os.path.split(a)[1].split(".")[0]
            temp["title"] = config.get(section, "title")
            temp["description"] = config.get(section, "description")            
            temp["application"] = config.get(section, "application")

            if temp["application"] not in appsfinal:
                appsfinal.append(temp["application"])          
            
            temp["category"] = config.get(section, "category")

            catsdict = {}
            catsdict["application"] = temp["application"]
            catsdict["category"] = temp["category"]
            catsfinal.append(catsdict)           
            
            temp["icon"] = config.get(section, "icon")

            icondict = {}
            icondict["application"] = temp["application"]
            icondict["icon"] = temp["icon"]
            iconsfinal.append(icondict)
            
            temp["depends"] = config.get(section, "depends")
            temp["needs-signing"] = config.getboolean(section, "needs-signing")
            temp["needs-information"] = config.get(section, "needs-information")
            temp["summary"] = config.get(section, "summary")
            temp["steps"] = config.get(section, "steps")
            temp["links"] = config.get(section, "links")
            temp["help"] = config.get(section, "help")
            
            data.append(temp)

        # first update the applications

        for a in appsfinal:
            try:
                Application.objects.get(descriptor=a)
            except ObjectDoesNotExist:
                newapp = Application(descriptor=a, label=a)
                newapp.save()

        # now update the icons

        iconsfinalf = [dict(y) for y in set(tuple(x.items()) for x in iconsfinal)]

        for a in iconsfinalf:
            try:
                iconapp = Application.objects.get(descriptor=a["application"])
                Icon.objects.get(application=iconapp, filename=a["icon"])
            except ObjectDoesNotExist:
                iconapp = Application.objects.get(descriptor=a["application"])
                newicon = Icon(application=iconapp, filename=a["icon"])
                newicon.save()      

        # update the categories

        catsfinalf = [dict(y) for y in set(tuple(x.items()) for x in catsfinal)]

        for a in catsfinalf:
            try:
                categoryapp = Application.objects.get(descriptor=a["application"])
                Category.objects.get(application=categoryapp, label=a["category"])
            except ObjectDoesNotExist:
                categoryapp = Application.objects.get(descriptor=a["application"])
                newcat = Category(application=categoryapp, label=a["category"])
                newcat.save()

        # add the data to the database
        for d in data:
            app = Application.objects.get(descriptor=d["application"])
            i = Icon.objects.get(application=app, filename=d["icon"])
            cat = Category.objects.get(application=app, label=d["category"])
            
            a = Accomplishment(application = app,
                title = d["title"],
                descriptor = d["descriptor"],
                description = d["description"],
                needsinformation = d["needs-information"],
                needssigning = True,
                depends = None,
                summary = d["summary"],
                steps = d["steps"],
                links = d["links"],
                helpres = d["help"])

            a.save()
            
            a.categories.add(cat)
            a.icon.add(i)
            
            a.save()

        print data
        # update the accomplishments that depend on others
        for d in data:
            if d["depends"] is not None:
                dep = d["depends"].split("/")[1]
                print dep
                app = Application.objects.get(descriptor=d["application"])
                targetacc = Accomplishment.objects.get(application=app, descriptor=d["descriptor"])
                depacc = Accomplishment.objects.get(application=app, descriptor=dep)
                targetacc.depends = depacc
                targetacc.save()
    
    apps = Application.objects.all()
    output = ', '.join([p.title for p in accoms])
    return render_to_response('index.html', {'accoms': accoms, 'apps': apps}, RequestContext(request))

def detail(request, accom_id):
    a = Accomplishment.objects.get(id=accom_id)

    a_form = AccomplishmentForm(instance=a)
    a_form.fields["categories"].queryset = Category.objects.filter(application=a.application)
    a_form.fields["icon"].queryset = Icon.objects.filter(application=a.application)
    a_form.fields["depends"].queryset = Accomplishment.objects.filter(application=a.application)

    return render_to_response('detail.html', {'AccomplishmentForm': a_form, 'accom': a}, RequestContext(request))                

def edit(request, accom_id):
    print "submitting data"
    print request.POST["description"]
    print request.POST["depends"]
    print request.POST["summary"]
    print request.POST["steps"]
    print request.POST["links"]
    print request.POST["helpres"]
    print request.POST["categories"]
    print request.POST["icon"]
    print accom_id
    #print request.POST["application"]

    targetacc = Accomplishment.objects.get(id=accom_id)
    diffcat = Category.objects.get(id=request.POST["categories"])
    difficon = Icon.objects.get(id=request.POST["icon"])
    print difficon
    print targetacc.title

    a = AccomplishmentDiff(accomplishment = targetacc,
        user = "Jono Bacon",
        description = request.POST["description"],
        summary = request.POST["summary"],
        steps = request.POST["steps"],
        links = request.POST["links"],
        helpres = request.POST["helpres"])    

    a.save()
    return HttpResponseRedirect(reverse('editor.views.results', args=(a.id,)))

def results():
    print "results"

def queue(request):
    print "queue"
    accoms = Accomplishment.objects.all()
    apps = Application.objects.all()
    diffs = AccomplishmentDiff.objects.all()
    return render_to_response('queue.html', {'accoms': accoms, 'apps': apps, 'diffs' : diffs }, RequestContext(request))