Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/controllers/default.py
blob: d9f9a6dff2a21271a9b770ecbceb2cd270cf2ba0 (plain)
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
import os

def sort_by_views(L):
    if len(L) <= 1: return L
    return sort_by_views( [ lt for lt in L[1:] if lt.views < L[0].views ] )  +  [ L[0] ]  +  sort_by_views( [ ge for ge in L[1:] if ge.views >= L[0].views ] )

def sort_by_id(L):
    if len(L) <= 1: return L
    return sort_by_id( [ lt for lt in L[1:] if lt.id < L[0].id ] )  +  [ L[0] ]  +  sort_by_id( [ ge for ge in L[1:] if ge.id >= L[0].id ] )

def index():
  sorts = ["newest", "views"]
  if "page" in request.vars.keys(): page = int(request.vars["page"]) - 1
  else: page = 0
  if "sort" in request.vars.keys(): sort = request.vars["sort"]
  else: sort = 'newest'
  if sort not in sorts:
      sort = "newest"
  if "profile" in request.vars.keys(): profile = request.vars["profile"]
  else: profile = None
  table = []
  raw_images = db().select(db.image.ALL)
  i = 0
  row = []
  count = 0
  images = []
  for image in raw_images:
      if profile != None:
          if image.creator == profile:
              images.append(image)
      else: images.append(image)
  if sort == 'views':
      #images = sort_by_views(images)
      images.reverse()
  if sort == 'newest':
      images.sort(cmp=lambda x,y:cmp(x.id,y.id))
      images.reverse()
  for image in images:
      if count >= page * 12 and page*12+12 > count:
          if i == 4:
              table.append(row)
              row = []
              i = 0
          i += 1
          row.append(image)
      count += 1
  table.append(row)
  pages = count / 12
  if count%12 > 0: pages += 1
  page += 1
  return dict(images=table, pages=pages, page=page, sort=sort, profile=profile)

def upload():
    form = SQLFORM(db.image, fields = ['title', 'file', 'newimage', 'description'])
    if form.accepts(request.vars, session):
      session.new_image_title = form.vars.title
      db(db.image.title==session.new_image_title).update(creator = auth.user.username)
      db(db.image.title==session.new_image_title).update(views = 0)
      redirect(URL(r=request, f='index')) 
    return dict(form=form)

"""@service.xmlrpc
def upload_remote(username,password,image,ta_file,title,description):
   logged_in = auth.login_bare(username,password)
   if logged_in:
      db.image.insert(file=ta_file,
                      image=image,
                      title=title,
                      description=description,
                      creator=auth.user.username,
                      views=0)"""
   


def image():
   images = db().select(db.image.ALL)
   image_id = int(request.vars["image"])
   for image in images:
       if image.id == image_id:
           real_image = image
   title=real_image.title
   if real_image == None:
       redirect(URL(r=request, f='index'))
   if auth.is_logged_in():
       if auth.user.username != real_image.creator:
           views = int(real_image.views) + 1
           db(db.image.title==real_image.title).update(views = views)
   form = SQLFORM(db.comment, fields = ['body'])
   if form.accepts(request.vars, session):
       db(db.comment.id==form.vars.id).update(author = auth.user.username)
       db(db.comment.id==form.vars.id).update(image_id = real_image.id)
   comments=db().select(db.comment.ALL)
   image_comments = []
   for comment in comments:
       if comment.image_id == real_image.id:
            image_comments.append(comment)
   return dict(image = real_image, form= form,comments=image_comments)
   
  
def testimage():
	return db(db.image.title=="hellotest")

def user(): 
  return dict(form=auth())

def delete():
   images = db().select(db.image.ALL)
   image_id = int(request.vars["id"])
   for image in images:
       if image.id == image_id:
           real_image = image
   if auth.is_logged_in():
       if auth.user.username == real_image.creator:
           query=(db.image.id==real_image.id)
           db(query).delete()
   redirect(URL(r=request, f='index')) 
  
  
def download():
  return response.download(request, db)