GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cc823b...3d112a )
by PyJobs
9s
created

Permission   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 100 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __unicode__() 2 2 1
A __repr__() 2 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
"""
3
Auth* related model.
4
5
This is where the models used by the authentication stack are defined.
6
7
It's perfectly fine to re-use this definition in the pyjobsweb application,
8
though.
9
10
"""
11
import os
12
from datetime import datetime
13
from hashlib import sha256
14
__all__ = ['User', 'Group', 'Permission']
15
16
from sqlalchemy import Table, ForeignKey, Column
17
from sqlalchemy.types import Unicode, Integer, DateTime
18
from sqlalchemy.orm import relation, synonym
19
20
from pyjobsweb.model import DeclarativeBase, metadata, DBSession
21
22
23
# This is the association table for the many-to-many relationship between
24
# groups and permissions.
25
group_permission_table = Table('tg_group_permission', metadata,
26
                               Column('group_id', Integer,
27
                                      ForeignKey('tg_group.group_id',
28
                                                 onupdate="CASCADE",
29
                                                 ondelete="CASCADE"),
30
                                      primary_key=True),
31
                               Column('permission_id', Integer,
32
                                      ForeignKey('tg_permission.permission_id',
33
                                                 onupdate="CASCADE",
34
                                                 ondelete="CASCADE"),
35
                                      primary_key=True))
36
37
38
# This is the association table for the many-to-many relationship between
39
# groups and members - this is, the memberships.
40
user_group_table = Table('tg_user_group', metadata,
41
                         Column('user_id', Integer,
42
                                ForeignKey('tg_user.user_id',
43
                                           onupdate="CASCADE",
44
                                           ondelete="CASCADE"),
45
                                primary_key=True),
46
                         Column('group_id', Integer,
47
                                ForeignKey('tg_group.group_id',
48
                                           onupdate="CASCADE",
49
                                           ondelete="CASCADE"),
50
                                primary_key=True))
51
52
53 View Code Duplication
class Group(DeclarativeBase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
54
    """
55
    Group definition
56
57
    Only the ``group_name`` column is required.
58
59
    """
60
61
    __tablename__ = 'tg_group'
62
63
    group_id = Column(Integer, autoincrement=True, primary_key=True)
64
    group_name = Column(Unicode(16), unique=True, nullable=False)
65
    display_name = Column(Unicode(255))
66
    created = Column(DateTime, default=datetime.now)
67
    users = relation('User', secondary=user_group_table, backref='groups')
68
69
    def __repr__(self):
70
        return '<Group: name=%s>' % repr(self.group_name)
71
72
    def __unicode__(self):
73
        return self.group_name
74
75
76
class User(DeclarativeBase):
77
    """
78
    User definition.
79
80
    This is the user definition used by :mod:`repoze.who`, which requires at
81
    least the ``user_name`` column.
82
83
    """
84
    __tablename__ = 'tg_user'
85
86
    user_id = Column(Integer, autoincrement=True, primary_key=True)
87
    user_name = Column(Unicode(16), unique=True, nullable=False)
88
    email_address = Column(Unicode(255), unique=True, nullable=False)
89
    display_name = Column(Unicode(255))
90
    _password = Column('password', Unicode(128))
91
    created = Column(DateTime, default=datetime.now)
92
93
    def __repr__(self):
94
        return '<User: name=%s, email=%s, display=%s>' % (
95
            repr(self.user_name),
96
            repr(self.email_address),
97
            repr(self.display_name)
98
        )
99
100
    def __unicode__(self):
101
        return self.display_name or self.user_name
102
103
    @property
104
    def permissions(self):
105
        """Return a set with all permissions granted to the user."""
106
        perms = set()
107
        for g in self.groups:
108
            perms = perms | set(g.permissions)
109
        return perms
110
111
    @classmethod
112
    def by_email_address(cls, email):
113
        """Return the user object whose email address is ``email``."""
114
        return DBSession.query(cls).filter_by(email_address=email).first()
115
116
    @classmethod
117
    def by_user_name(cls, username):
118
        """Return the user object whose user name is ``username``."""
119
        return DBSession.query(cls).filter_by(user_name=username).first()
120
121
    @classmethod
122
    def _hash_password(cls, password):
123
        salt = sha256()
124
        salt.update(os.urandom(60))
125
        salt = salt.hexdigest()
126
127
        hash = sha256()
128
        # Make sure password is a str because we cannot hash unicode objects
129
        hash.update((password + salt).encode('utf-8'))
130
        hash = hash.hexdigest()
131
132
        password = salt + hash
133
134
        # Make sure the hashed password is a unicode object at the end of the
135
        # process because SQLAlchemy _wants_ unicode objects for Unicode cols
136
        password = password.decode('utf-8')
137
138
        return password
139
140
    def _set_password(self, password):
141
        """Hash ``password`` on the fly and store its hashed version."""
142
        self._password = self._hash_password(password)
143
144
    def _get_password(self):
145
        """Return the hashed version of the password."""
146
        return self._password
147
148
    password = synonym('_password', descriptor=property(_get_password,
149
                                                        _set_password))
150
151
    def validate_password(self, password):
152
        """
153
        Check the password against existing credentials.
154
155
        :param password: the password that was provided by the user to
156
            try and authenticate. This is the clear text version that we will
157
            need to match against the hashed one in the database.
158
        :type password: unicode object.
159
        :return: Whether the password is valid.
160
        :rtype: bool
161
162
        """
163
        hash = sha256()
164
        hash.update((password + self.password[:64]).encode('utf-8'))
165
        return self.password[64:] == hash.hexdigest()
166
167
168 View Code Duplication
class Permission(DeclarativeBase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
169
    """
170
    Permission definition.
171
172
    Only the ``permission_name`` column is required.
173
174
    """
175
176
    __tablename__ = 'tg_permission'
177
178
    permission_id = Column(Integer, autoincrement=True, primary_key=True)
179
    permission_name = Column(Unicode(63), unique=True, nullable=False)
180
    description = Column(Unicode(255))
181
182
    groups = relation(Group, secondary=group_permission_table,
183
                      backref='permissions')
184
185
    def __repr__(self):
186
        return '<Permission: name=%s>' % repr(self.permission_name)
187
188
    def __unicode__(self):
189
        return self.permission_name
190